gpt4 book ai didi

c - 第二次重新分配后 VS 发疯

转载 作者:行者123 更新时间:2023-11-30 19:03:28 27 4
gpt4 key购买 nike

所以我有一个输入文件,其中包含以下文本(每行=用户):

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 个用户(每个用户)重新分配空间。问题是,一切都很顺利,直到第二次重新分配,然后它向我显示错误“Exercise 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[*menArraySize];

您指向新位置,但您也覆盖(并丢失)head原始值(除非您减去)。当你这样做时:

head = &head[0];

认为您正在恢复原始值,但这没有任何效果。您只需引用/取消引用相同的值即可。

解决方案:使用另一个User *temp 值来引用新位置。使用realloc后保持head不变。

关于c - 第二次重新分配后 VS 发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53973145/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com