使用 "Free()" 命令后程序崩溃 (C)
Program crashing after using "Free()" command (C)
我有以下结构:
typedef struct employee
{
int name_length;
char* name;
float salary;
} Employee;
以及以下函数:
Employee** makeArr(char* fileName, int* size)
{
FILE* f = fopen(fileName, "rb");
checkOpenFile(f);
Employee** arr = (Employee**)malloc(sizeof(Employee*));
checkAllocation(&arr);
int counter = 0;
while (!feof(f))
{
int employeeNameSize;
float employeeSalary;
fread(&employeeNameSize, sizeof(int), 1, f);
if (feof(f))
break;
char* employeeName = (char*)malloc(sizeof(char) * employeeNameSize+1);
checkAllocation(&employeeName);
fread(employeeName, sizeof(char), employeeNameSize, f);
employeeName[employeeNameSize] = '[=12=]';
fread(&employeeSalary, sizeof(float), 1, f);
arr[counter] = makeEmployee(employeeNameSize, employeeName, employeeSalary);
counter++;
realloc(arr, sizeof(Employee*)*(counter));
}
*size = counter;
fclose(f);
return arr;
}
和:
void freeEmployeeArr(Employee** arr, int size)
{
for(int i = 0; i < size; i++)
{
free(arr[i]->name);
free(arr[i]);
}
free(arr);
}
还有这个:
Employee* makeEmployee(int nameLength, char* name, float salary)
{
Employee* tmp = (Employee*)malloc(sizeof(Employee));
checkAllocation(&tmp);
tmp->name_length = nameLength;
tmp->name = name;
tmp->salary = salary;
return tmp;
}
我的主要:
void main(int argc, char* argv[])
{
char* name1 = argv[1];
char* name2 = argv[2];
int size;
Employee** ans = makeArr(name1, &size);
freeEmployeeArr(ans, size);
}
我遇到的问题:
free(arr);
使我的程序崩溃。如果我删除这一行,一切正常。
但这个具体问题给我带来了一些问题。
如果我试图在不调试的情况下开始,我会收到以下消息:
如果我正在调试,我会收到以下消息:
我真的不知道我的程序出了什么问题。我读到当我 realloc/malloc 的值为零时可能会出现此类问题。但事实并非如此。
任何人都可以在这里发现我的错误吗?
提前致谢。
行
realloc(arr, sizeof(Employee*)*(counter));
不好。您必须将返回的新指针分配给 arr
.
应该是这样的:
Employee** newArr = realloc(arr, sizeof(Employee*)*(counter));
if (newArr == NULL) {
/* handle error, clean up and return from the function */
}
arr = newArr;
我有以下结构:
typedef struct employee
{
int name_length;
char* name;
float salary;
} Employee;
以及以下函数:
Employee** makeArr(char* fileName, int* size)
{
FILE* f = fopen(fileName, "rb");
checkOpenFile(f);
Employee** arr = (Employee**)malloc(sizeof(Employee*));
checkAllocation(&arr);
int counter = 0;
while (!feof(f))
{
int employeeNameSize;
float employeeSalary;
fread(&employeeNameSize, sizeof(int), 1, f);
if (feof(f))
break;
char* employeeName = (char*)malloc(sizeof(char) * employeeNameSize+1);
checkAllocation(&employeeName);
fread(employeeName, sizeof(char), employeeNameSize, f);
employeeName[employeeNameSize] = '[=12=]';
fread(&employeeSalary, sizeof(float), 1, f);
arr[counter] = makeEmployee(employeeNameSize, employeeName, employeeSalary);
counter++;
realloc(arr, sizeof(Employee*)*(counter));
}
*size = counter;
fclose(f);
return arr;
}
和:
void freeEmployeeArr(Employee** arr, int size)
{
for(int i = 0; i < size; i++)
{
free(arr[i]->name);
free(arr[i]);
}
free(arr);
}
还有这个:
Employee* makeEmployee(int nameLength, char* name, float salary)
{
Employee* tmp = (Employee*)malloc(sizeof(Employee));
checkAllocation(&tmp);
tmp->name_length = nameLength;
tmp->name = name;
tmp->salary = salary;
return tmp;
}
我的主要:
void main(int argc, char* argv[])
{
char* name1 = argv[1];
char* name2 = argv[2];
int size;
Employee** ans = makeArr(name1, &size);
freeEmployeeArr(ans, size);
}
我遇到的问题:
free(arr);
使我的程序崩溃。如果我删除这一行,一切正常。
但这个具体问题给我带来了一些问题。
如果我试图在不调试的情况下开始,我会收到以下消息:
如果我正在调试,我会收到以下消息:
我真的不知道我的程序出了什么问题。我读到当我 realloc/malloc 的值为零时可能会出现此类问题。但事实并非如此。
任何人都可以在这里发现我的错误吗? 提前致谢。
行
realloc(arr, sizeof(Employee*)*(counter));
不好。您必须将返回的新指针分配给 arr
.
应该是这样的:
Employee** newArr = realloc(arr, sizeof(Employee*)*(counter));
if (newArr == NULL) {
/* handle error, clean up and return from the function */
}
arr = newArr;