尝试加载链接列表时Malloc崩溃
我正在尝试从文本文件初始化链接列表,以下是我的结构:尝试加载链接列表时Malloc崩溃,c,string,linked-list,malloc,fgets,C,String,Linked List,Malloc,Fgets,我正在尝试从文本文件初始化链接列表,以下是我的结构: typedef struct Diagnostic { char* disease; int priority; }Diagnostic; typedef struct Fiche Fiche; struct Fiche { char* name; int age; Diagnostic diagnostic; Fiche* next; // because this is a linked
typedef struct Diagnostic
{
char* disease;
int priority;
}Diagnostic;
typedef struct Fiche Fiche;
struct Fiche
{
char* name;
int age;
Diagnostic diagnostic;
Fiche* next; // because this is a linked list
};
这是我的加载函数:
void loadFiches()
{
int i;
char tmp1[100], tmp2[100];
Fiche* current;
FILE* file = fopen("fiches.txt", "r");
if(file != NULL)
{
while(!feof(file))
{
printf("malloc:");
current = malloc(sizeof(Fiche)); // allocate memory for a new fiche
printf("%p\n", current);
fgets(tmp1, 100, file); // get the name
cleanChar(tmp1); // remove '\n'
fscanf(file, "%d\n", ¤t->age); // get the age
fgets(tmp2, 100, file); // get the disease
cleanChar(tmp2); // remove '\n'
fscanf(file, "%d\n", ¤t->diagnostic.priority); // get the priority
current->diagnostic.disease = malloc(strlen(tmp2) * sizeof(char)); // allocate memory for the disease
strcpy(current->diagnostic.disease, tmp2); // copy the disease in the corresponding field
// Then I add this fiche to my linked list
}
}
else printf("error");
fclose(file);
}
void cleanChar(char string[100])
{
int i;
for(i = 0; i < strlen(string); i++)
if(string[i] == '\n') string[i] = '\0';
}
void saveFiches(List list)
{
int i;
Fiche* current = list.first;
FILE* file;
file = fopen("fiches.txt", "w+");
if(file != NULL)
{
for(i = 0; i < list.size; i++)
{
fprintf(file, "%s\n%d\n%s\n%d\n", current->name, current->age, current->diagnostic.disease, current->diagnostic.priority);
current = current->next;
}
}
else printf("error");
fclose(file);
}
这个的输出是
malloc:00350FD8
malloc:00350FF8
malloc:
所以它在第三个malloc崩溃了。请注意,我只初始化了disease字段,因为它是导致崩溃的字段,其他一切都正常工作,所以它不会出现在这段代码中。
还要注意的是,在调试模式下,一切都很好
如果我删除cleanChar(tmp2)代码>或strcpy(当前->诊断疾病,tmp2)代码>,则它也可以正常工作(但在第一种情况下,我有一个不需要的\n),是这两行的组合导致了崩溃
以下是我的cleanChar函数:
void loadFiches()
{
int i;
char tmp1[100], tmp2[100];
Fiche* current;
FILE* file = fopen("fiches.txt", "r");
if(file != NULL)
{
while(!feof(file))
{
printf("malloc:");
current = malloc(sizeof(Fiche)); // allocate memory for a new fiche
printf("%p\n", current);
fgets(tmp1, 100, file); // get the name
cleanChar(tmp1); // remove '\n'
fscanf(file, "%d\n", ¤t->age); // get the age
fgets(tmp2, 100, file); // get the disease
cleanChar(tmp2); // remove '\n'
fscanf(file, "%d\n", ¤t->diagnostic.priority); // get the priority
current->diagnostic.disease = malloc(strlen(tmp2) * sizeof(char)); // allocate memory for the disease
strcpy(current->diagnostic.disease, tmp2); // copy the disease in the corresponding field
// Then I add this fiche to my linked list
}
}
else printf("error");
fclose(file);
}
void cleanChar(char string[100])
{
int i;
for(i = 0; i < strlen(string); i++)
if(string[i] == '\n') string[i] = '\0';
}
void saveFiches(List list)
{
int i;
Fiche* current = list.first;
FILE* file;
file = fopen("fiches.txt", "w+");
if(file != NULL)
{
for(i = 0; i < list.size; i++)
{
fprintf(file, "%s\n%d\n%s\n%d\n", current->name, current->age, current->diagnostic.disease, current->diagnostic.priority);
current = current->next;
}
}
else printf("error");
fclose(file);
}
void cleanChar(字符字符串[100])
{
int i;
对于(i=0;i
有人知道是什么导致了这次车祸吗?我很确定这与我将fiches保存到文本文件的方式无关,但下面是保存功能:
void loadFiches()
{
int i;
char tmp1[100], tmp2[100];
Fiche* current;
FILE* file = fopen("fiches.txt", "r");
if(file != NULL)
{
while(!feof(file))
{
printf("malloc:");
current = malloc(sizeof(Fiche)); // allocate memory for a new fiche
printf("%p\n", current);
fgets(tmp1, 100, file); // get the name
cleanChar(tmp1); // remove '\n'
fscanf(file, "%d\n", ¤t->age); // get the age
fgets(tmp2, 100, file); // get the disease
cleanChar(tmp2); // remove '\n'
fscanf(file, "%d\n", ¤t->diagnostic.priority); // get the priority
current->diagnostic.disease = malloc(strlen(tmp2) * sizeof(char)); // allocate memory for the disease
strcpy(current->diagnostic.disease, tmp2); // copy the disease in the corresponding field
// Then I add this fiche to my linked list
}
}
else printf("error");
fclose(file);
}
void cleanChar(char string[100])
{
int i;
for(i = 0; i < strlen(string); i++)
if(string[i] == '\n') string[i] = '\0';
}
void saveFiches(List list)
{
int i;
Fiche* current = list.first;
FILE* file;
file = fopen("fiches.txt", "w+");
if(file != NULL)
{
for(i = 0; i < list.size; i++)
{
fprintf(file, "%s\n%d\n%s\n%d\n", current->name, current->age, current->diagnostic.disease, current->diagnostic.priority);
current = current->next;
}
}
else printf("error");
fclose(file);
}
void saveFiches(列表)
{
int i;
Fiche*current=list.first;
文件*文件;
file=fopen(“fiches.txt”,“w+”);
如果(文件!=NULL)
{
对于(i=0;i姓名,当前->年龄,当前->诊断.disease,当前->诊断.priority);
当前=当前->下一步;
}
}
else printf(“错误”);
fclose(文件);
}
<代码>清单是一个包含链表第一元素的结构。
< p>您的字符串<代码> MalCube()/代码>逐个关闭(您不考虑终止<代码> \ 0”< /代码>:
current->diagnostic.disease = malloc(strlen(tmp2) * sizeof(char));
应该是:
current->diagnostic.disease = malloc((strlen(tmp2) + 1) * sizeof(char));
而且,由于sizeof(char)
总是1
,这可能是:
current->diagnostic.disease = malloc(strlen(tmp2) + 1);
除非您希望通过取消引用分配给它的指针来确定适当的大小,从而使malloc()
更加健壮:
current->diagnostic.disease = malloc((strlen(tmp2) + 1) *
sizeof(*(current->diagnostic.disease)));
您还可以复制字符串:
current->diagnostic.disease = strdup(tmp2);
无论采用哪种方式,都不要忘记检查NULL
malloc(strlen(tmp2)*sizeof(char))
->malloc(strlen(tmp2)+1)
(您需要空间来终止'\0'
)。请注意,您的标题有误导性,您可能会认为它是malloc()
的错误,但不是。问题出在其他地方,可能是上面的注释指出的地方,但不是在malloc()
上。您的输入文件是什么样子的?@Drakalex注意到sizeof(char)
必须是1,并且是c标准强制要求的。使用strdup()
可以帮助避免将来出现类似的问题(并使代码更易于阅读)。