Realloc导致程序崩溃
目前正在尝试编写基于菜单的程序,要求用户输入学生记录,在输入初始记录后,我希望用户可以选择添加更多记录槽,但当我尝试重新定位2d指针数组时,我的程序崩溃,特别是在调用第二个函数后Realloc导致程序崩溃,c,malloc,realloc,C,Malloc,Realloc,目前正在尝试编写基于菜单的程序,要求用户输入学生记录,在输入初始记录后,我希望用户可以选择添加更多记录槽,但当我尝试重新定位2d指针数组时,我的程序崩溃,特别是在调用第二个函数后 #include <stdio.h> #include <string.h> #include <stdlib.h> #define LENGTH 21 void printRecords(int* size, char **fistName, char **lastName, f
#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 : %.2f\n", 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]);
}
#包括
#包括
#包括
#定义长度21
作废打印记录(整数*大小,字符**fistName,字符**姓氏,浮点*等级);
void addRecord(int*size,char**firstName,char**lastName,float*grade);
int main(void){/*最小测量检查*/
int size=0,*check=(int*)malloc(sizeof(int));
*检查=1;
while(检查){
printf(“请注明您要输入的记录数(最少5条):”;
scanf(“%d”,大小(&S);
如果(尺寸<5)
printf(“输入的大小低于最小值。\n”);
其他的
检查=0;}
免费(支票);
/*动态内存分配*/
char**firstName=(char**)malloc(size*sizeof(char*),**lastName=(char**)malloc(size*sizeof(char*));
浮点数*等级=(浮点数*)malloc(尺寸*浮点数);
int i;
对于(i=0;i
编辑缺少的中存在错误代码>导致未重新分配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"
此外,还提供了有关您使用检查的评论。您的输入循环有一些问题。为一个简单的int
分配内存是完全没有必要的。您甚至没有使用check
作为指针(check=0;
将导致free()
失败和内存泄漏)
C是传递值
函数返回后,应用于addREcord()
中的firstName
和lastName
的更改将丢失,因为这两个VAIRBALE包含作为参数传递给调用addREcord()
的变量的范围
从更改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)
(有趣的是,您正确地选择了大小
)
像这样调用addRecord()
:
addRecord(&size, &firstName, &lastName, grade);
不要在c中强制转换malloc()
的返回值void*
会自动转换为任何指针类型,请以可读的样式格式化代码。firstName=a代码>未分配原始名称(主名称中的姓氏未更改)。否则//出现错误
需要句子。scanf(“%20s%20s%f”、姓氏[*size-1]、姓氏[*size-1]、&grade[*size-1])代码>@0123是的,是的。但是C没有pass-by-reference。您忽略了这样一个事实,即对realloc()
的这两个调用返回的地址从未传递到addRecord()
之外,而是在离开函数时丢失。
void addRecord(int* size, char** firstName, char** lastName, float* grade);
void addRecord(int* size, char*** pfirstName, char*** plastName, float* grade);
addRecord(&size, &firstName, &lastName, grade);