【发布时间】:2018-12-29 20:33:27
【问题描述】:
所以我有一个输入文件,其中包含以下文本(每行 = 用户):
012345678;danny;cohen;22;M;danny1993;123;1,2,4,8;Nice person
223325222;or;dan;25;M;ordan10;1234;3,5,6,7;Singer and dancer
349950234;nadav;cohen;50;M;nd50;nadav;3,6,7,8;Engineer very smart
首先代码为一个用户分配空间,然后它为另外 1 个用户重新分配空间(为每个用户)。问题是,在第二次重新分配之前一切都很好,然后它向我显示错误“练习 6.exe 已触发断点”。我还要提到最初的错误是: ““wntdll.pdb 未加载”,但我尝试执行 VS 建议的操作——使用符号。然后出现“触发断点”错误。
我尝试在文本文件中切换行,但没关系,第二次尝试重新分配后出现问题。
让我给你看一下代码的相关部分:
int main()
{
//intiallizing variables
int menArraySize = 0;
//creating a NULL array pointer (array) for men
User *head = NULL;
readInputFile(head, &menArraySize, list);
}
void readInputFile(User *head, int *menArraySize, WomenList *list)
{
//temporary data types for the the stracture we're creating
char id[10];
char *firstName = NULL;
char *lastName = NULL;
char age[4] = { 0 };
char gender = '\0';
char *userName = NULL;
char *password = NULL;
char hobbies = 0;
char *description = NULL;
//regular function data types
int i = 0, j = 0, k = 0, currentDetail = 1, size;
char currentString[212] = { 0 };
int currentChar;
//opening the input file
FILE *input = fopen("input.txt", "r");
...
//long code for allocating room for each string (firstName, lastName, etc...)- irelevant
...
head = addMale(head, menArraySize, id, firstName, lastName, age,
gender, userName, password, hobbies, description);
...
//rest of the function isn't relevant
}
User* addMale(User *head ,int *menArraySize, char id[], char *firstName,
char *lastName,char age[], char gender, char *userName,
char *password, char hobbies, char *description)
{
//if the array is empty, allocate room for one user
if (*menArraySize == 0)
{
head = (User *)malloc(1 * sizeof(User));
}
//if the array isn't empty, reallocate room for one more user
else
{
**this is the line in which the error occurs (second time reallocating,
third dynamic allocation total for this pointer)**
head = (User *)realloc(head, (*menArraySize + 1) * sizeof(User));
}
//if the allocation failed
if (head == NULL)
exit(1);
//pointing to the new user we created
head = &head[*menArraySize];
//apply all details to the user
strcpy(head->id, id);
head->firstName = firstName;
head->lastName = lastName;
strcpy(head->age, age);
head->gender = gender;
head->userName = userName;
head->password = password;
head->hobbies = hobbies;
head->description = description;
//applying an index number to the user
head->index = *menArraySize;
//pointing back to the head of the array
head = &head[0];
//updating the variable showing the size of the array
*menArraySize = *menArraySize + 1;
return head;
}
为什么会这样?我能做些什么来解决它?谢谢!
【问题讨论】:
-
head = &head[0];什么都不做。当你做head = &head[*menArraySize];时,你已经失去了head。使用另一个变量 -
尽管存在技术问题——很好地解决了问题标题...在进行了建议的更改之后,您将永远不会看到
main()中的head的分配,因为readInputFile收到了一份副本head和在main()之外进行的任何分配都将丢失(以及内存泄漏)。相反,您需要传递head的地址(例如&head),并且您的参数需要为User **head。 -
作为关于
addMale的一般说明——如果你传递的参数超过 4 个——你应该考虑重构你的代码。 -
即使在修复之后,您也没有正确使用
realloc- 您需要将指针保存到临时地址,然后检查,然后才将临时地址中的值分配给您的head。如果没有分配数组,则可以通过将指针设置为 NULL 来删除if-realloc将正确处理它。
标签: c dynamic-memory-allocation realloc