简体   繁体   English

malloc链表时出现分段错误

[英]segmentation fault while malloc linked list

I've created two structs: 我创建了两个结构:

typedef struct Student{
    int id; 
    char* name;
    int birthYear;
    int finishedCourses;
    int courseCredits;
    double avarage;
    int coursesNow;
    NodeCourses* courses;

}Student;

typedef struct NodeS{
    Student student;
    struct NodeS* next;
}NodeS;

So I can now create a linked list of students. 现在,我可以创建一个学生的链接列表。

one of the things I want to do is to ask the user to put details about each student. 我想做的一件事就是要求用户输入有关每个学生的详细信息。

so Ive created a function that called "addNewStudent" 所以我创建了一个名为“ addNewStudent”的函数

first in the function it does this: 首先在函数中执行以下操作:

NodeS* newStudent;
    newStudent=(NodeS*)malloc(sizeof(NodeS));

and than, when I am asking the user to insert a name I put it in a string called "name" and I want to malloc again the name string. 并且,当我要求用户插入名称时,我将其放在一个名为“ name”的字符串中,并且我想再次分配该名称字符串。 so I do this: 所以我这样做:

newStudent->student.name=(char*)malloc(sizeof(char)*strlen(name));

and that line gives me segmentation fault. 那条线给我分段错误。

ps: Ive checked the string is in the write size and that i get is correctly. ps:我已经检查了字符串的大小,并且我得到的是正确的。

what should I do? 我该怎么办?

 newStudent->student.name=(char*)malloc(sizeof(char)*strlen(name));

I am taking you at your word that this exact line of code causes a segmentation fault. 我相信您,这行确切的代码会导致分段错误。 There are three things this line of code does, any one of which could be your cause: 此代码行执行的三项操作,可能是您的原因之一:

1) You do strlen(name) . 1)你做strlen(name) If name does not point to a valid C-style string with a proper nul terminating byte, this could segfault. 如果name未指向带有适当nul终止字节的有效C样式字符串,则可能发生段错误。 You can test this theory by adding this before the line of code: 您可以通过在代码行之前添加以下内容来测试该理论:

printf("About to call strlen\n");
printf("Got: %d\n", strlen(name));

If this is the problem, you will see the first line of output but not the second since the segfault will terminate the program before it gets a chance to execute. 如果这是问题所在,您将看到输出的第一行,但看不到第二行,因为段错误会在程序有机会执行之前终止程序。

2) You call malloc . 2)您调用malloc It's possible that the heap has become corrupt due to a previous double free, use after free, buffer overrun, or similar problem. 堆可能由于先前的双重释放,释放后使用,缓冲区溢出或类似问题而损坏。 So the call to malloc may be the victim of previous damage. 因此,调用malloc可能是先前损坏的受害者。 You can test this by adding this code: 您可以通过添加以下代码进行测试:

printf("About to call strlen\n");
printf("Got: %d\n", strlen(name));
printf("About to call malloc\n");
char *o = malloc(sizeof(char) * strlen(name));
printf("Malloc didn't segfault\n");
newStudent->student.name=o;

3) You assign to newStudent->student.name . 3)您分配给newStudent->student.name If, for example, newStudent is NULL or otherwise doesn't point to valid contents, that could be your issue. 例如,如果newStudentNULL或否则未指向有效内容,则可能是您的问题。 You can test this by adding this code: 您可以通过添加以下代码进行测试:

printf("About to call strlen\n");
printf("Got: %d\n", strlen(name));
printf("About to call malloc\n");
char *o = malloc(sizeof(char) * strlen(name));
printf("Malloc didn't segfault\n");
printf("Trying to access newStudent->student.name\n");
printf("newStudent->student.name=%p\n", newStudent->student.name);
newStudent->student.name=o;
printf("Actually, the whole statement worked\n");

If you see all the printf s but your program still crashes, then you were wrong about the statement causing the segfault. 如果看到所有的printf但是程序仍然崩溃,则说明导致段错误的语句是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM