C语言 Realloc导致程序崩溃



目前正在尝试编写基于菜单的程序,要求用户提供学生记录,输入初始记录后,我希望用户可以选择添加更多记录槽,但是当我尝试重新分配我的 2d 指针数组时,我的程序崩溃了,特别是在调用第二个函数之后。

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#define LENGTH 21
void printRecords(int* size, char **fistName, char **lastName, float *grade);
void addRecord(int* size, char** firstName, char** lastName, float *grade);
int  main(void) { /* Minimum measure check */
    int size = 0, *check = (int*)malloc(sizeof(int));
    *check = 1;
    while (check) {
        printf("Please indicate number of records you want to enter (min 5): ");
        scanf("%d", &size);
        if (size < 5)
            printf("Size entered was below the minimum.n");
        else
            check = 0; }
    free(check);
                                        /* Dynamic Memory Allocation */
    char **firstName = (char**)malloc(size * sizeof(char*)), **lastName = (char**)malloc(size * sizeof(char*)); 
    float *grade = (float*)malloc(size * sizeof(float));
    int i;
    for (i = 0; i < size; i++) {
        firstName[i] = (char*)malloc(LENGTH *  sizeof(char));
        lastName[i] = (char*)malloc(LENGTH * sizeof(char)); }
    printf("Please input records of students (enter a new line after each record),nwith following format first name last name score:n");
    for (i = 0; i < size; (i)++)
        scanf("%s %s %f", &firstName[i][0], &lastName[i][0], &grade[i]);
    int option = 1;
    while (option != 0) { /* Option Menu */
        printf("Print records (press 1)nAdd new record(press 2)nDelete record(s)(press 3)nSeach by last name(press 4)nSort by score(press 5)nSort by last name(press 6)nFind the median score(press 7)nExit the program(press 0)n");
        scanf("%d", &option);
        switch (option) {
        case 1:
            printRecords(&size, firstName, lastName, grade);
            break;
        case 2:
            addRecord(&size, firstName, lastName, grade);
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            break;
        }
    }
    return 0;
}
void printRecords(int* size, char **firstName, char **lastName, float grade[]) { /* Option 1 */
            int i;
            for (i = 0; i < *size; i++)
                printf("First Name : %s, Last Name : %s, Score : %.2fn", firstName[i], lastName[i], grade[i]); 
    }
void addRecord(int* size, char** firstName, char** lastName, float* grade) { /* Option 2 */
    char **a;
    float *c;
    (*size) += 1;
    a = (char **)realloc(firstName, *size * sizeof(char*)); /* Error */
    if (a != NULL) //realloc was successful
        firstName = a;
    else //there was an error
    a = (char **)realloc(lastName, *size * sizeof(char*));
    if (a != NULL) //realloc was successful
        lastName = a;
    else; //there was an error 
    c = (float *)realloc(grade, *size * sizeof(float*));
    grade = c;
    firstName[*size - 1] = (char*)malloc(LENGTH * sizeof(char));
    lastName[*size - 1] = (char*)malloc(LENGTH * sizeof(char));
    scanf("%s %s %f", &firstName[*size][0], &lastName[*size][0], &grade[*size]);
    }

编辑 缺少;错误,导致lastName未重新分配

if (a != NULL) //realloc was successful
    firstName = a;
else //there was an error                                  <<-- missing ;
a = (char **)realloc(lastName, *size * sizeof(char*));  // <<-- executed under "else"

实际上,lastName被分配的值与firstName相同,

如下所示
if (a != NULL) //realloc was successful
    lastName = a;

另外,关于您使用check的评论。您的输入循环存在一些问题。为简单的int分配内存是完全没有必要的.您甚至没有使用 check 作为指针(check = 0;会导致free()失败和内存泄漏)。

int size = 0, *check = (int*)malloc(sizeof(int));
*check = 1;
while (check) {         // <==== error should be *check
    printf("Please indicate number of records you want to enter (min 5): ");
    scanf("%d", &size);
    if (size < 5)
        printf("Size entered was below the minimum.n");
    else
        check = 0;      // <==== error should be *check
    }
free(check);

这会容易得多,您甚至不需要check.

int size;
do {
    printf("Please indicate number of records you want to enter (min 5): ");
    scanf("%d", &size);
    if (size < 5)
        printf("Size entered was below the minimum.n");
    }
while (size < 5);

C 是按值传递的。

应用于addREcord()中的firstNamelastName的更改在函数返回后丢失,因为这两个 vairbales 携带作为参数传递给 addRecord() 调用的变量的 cope。

addRecord()的定义从

void addRecord(int* size, char** firstName, char** lastName, float* grade);

要成为

void addRecord(int* size, char*** pfirstName, char*** plastName, float* grade);

在内部addRecord()将所有firstName替换为(*pfirstName),将所有lastName替换为(*plastName)

(足够多的刺激,你为size做对了。

像这样呼叫addRecord()

addRecord(&size, &firstName, &lastName, grade);

相关内容

  • 没有找到相关文章

最新更新