【问题标题】:How to fix segmentation fault when printing linked list?打印链表时如何解决分段错误?
【发布时间】:2019-11-06 14:59:36
【问题描述】:

在尝试打印我的学生链接列表时,我收到“分段错误(核心转储)”。当我通过硬编码打印列表时:

`

for (i = 0; i < 3; i++){
    printf("%s, %s\n", head->pStudent->last, head->pStudent->first);
    head = head->next;
}

`

(创建了 3 个节点。)它有效!当我尝试这样打印时:

`

while (head != NULL){
    printf("%s, %s\n", head->pStudent->last, head->pStudent->first);
    head = head->next;
}

`

它给了我分割错误。

我试过while (head-&gt;next != NULL){...}

我尝试在 main 中执行此操作,而不是调用 print_list(head);


    #include<stdio.h>
    #include<stdlib.h>

    typedef struct student{
        char *first;
        char *last;
    } student_t;

    typedef struct node{
        student_t *pStudent;
        struct node *next;
    } node_t;

    void addToStart(node_t ** head, student_t *student);
    void print_list (node_t * head);
    student_t *newStudent();

    int main(){
        node_t *head;
        student_t *studentOne = (student_t *) malloc(sizeof(student_t));
        student_t *studentTwo = (student_t *) malloc(sizeof(student_t));
        student_t *studentThree = (student_t *) malloc(sizeof(student_t));

        studentOne->first = (char *) malloc(sizeof(char));
        studentOne->last = (char *) malloc(sizeof(char));

        studentTwo->first = (char *) malloc(sizeof(char));
        studentTwo->last = (char *) malloc(sizeof(char));

        studentThree->first = (char *) malloc(sizeof(char));
        studentThree->last = (char *) malloc(sizeof(char));

        studentOne = newStudent();
        addToStart(&head, studentOne);

        studentTwo = newStudent();
        addToStart(&head, studentTwo);

        studentThree = newStudent();
        addToStart(&head, studentThree);

        print_list(head);

        printf("Then you will enter 3 students names that will be added to the end of the list\n");

        return 0;
    }

    void addToStart(node_t ** head, student_t *student){
        node_t *new = (node_t *) malloc(sizeof(node_t));

        new->pStudent = student;

        new->next = *(head);
        *(head) = new;
    }

    void print_list (node_t * head){
        while (head != NULL){
            printf("%s, %s\n", head->pStudent->last, head->pStudent->first);

            head = head->next;
        }

    }

    student_t *newStudent(){
        student_t *new = (student_t *) malloc(sizeof(student_t));
        new->first = (char *) malloc(sizeof(char));
        new->last = (char *) malloc(sizeof(char));

        printf("Enter first name of student: ");
        scanf("%s", new->first);
        printf("Enter last name of student: ");
        scanf("%s", new->last);

        return new;
    }

预期结果:


    Enter first name of student: 1
    Enter last name of student: 1
    Enter first name of student: 2
    Enter last name of student: 2
    Enter first name of student: 3
    Enter last name of student: 3
    3, 3
    2, 2
    1, 1

实际结果:


    Enter first name of student: 1
    Enter last name of student: 1
    Enter first name of student: 2
    Enter last name of student: 2
    Enter first name of student: 3
    Enter last name of student: 3
    3, 3
    2, 2
    1, 1
    Segmentation fault (core dumped)

【问题讨论】:

  • 您正在为学生姓名分配 一个 字符,但您将其视为字符串。一个字符串有一个终止空字符,所以"1"的名称仍然需要两个字符。
  • 用studentOne = newStudent(); 你覆盖 studentOne。但是您刚刚使用malloc 为其分配了内存。那段记忆现在丢失了。
  • 在这种情况下,我是否可以省略 main 中的所有 malloc,因为它在 newStudent() 方法中分配了内存?
  • 是的,您可以将它们全部放在main 中。

标签: c printing linked-list segmentation-fault


【解决方案1】:

按照您拥有此代码的方式,列表中的最后一个节点将指向您拥有的 head 变量,就像您第一次执行 addToStart 时一样。但是head没有初始化:

node_t *head;

正确初始化它以便循环正确终止:

node_t *head = NULL;

您的字符串也只有 1 char 的内存。除了空字符串之外,这还不够。试试这个:

new->first = malloc(32); // memory for 31 chars plus null terminator
new->last = malloc(32);

printf("Enter first name of student: ");
scanf("%31s", new->first); // read a maximum of 31 chars
printf("Enter last name of student: ");
scanf("%31s", new->last);

此外,您为学生和字符串分配了两次内存,在此过程中泄漏了内存。删除这些冗余并改为这样做:

student_t *studentOne = newStudent();
student_t *studentTwo = newStudent();
student_t *studentThree = newStudent();

addToStart(&head, studentOne);
addToStart(&head, studentTwo);
addToStart(&head, studentThree);

您也没有在任何地方释放任何内存,因此我假设您稍后将编写一个函数来清理列表。通过这些更改,程序如下所示:

#include<stdio.h>
#include<stdlib.h>

typedef struct student {
    char *first;
    char *last;
} student_t;

typedef struct node {
    student_t *pStudent;
    struct node *next;
} node_t;

void addToStart(node_t ** head, student_t *student);
void print_list(node_t * head);
student_t *newStudent();

int main() {
    node_t *head = NULL;
    student_t *studentOne = newStudent();
    student_t *studentTwo = newStudent();
    student_t *studentThree = newStudent();

    addToStart(&head, studentOne);
    addToStart(&head, studentTwo);
    addToStart(&head, studentThree);

    print_list(head);

    printf("Then you will enter 3 students names that will be added to the end of the list\n");

    return 0;
}

void addToStart(node_t ** head, student_t *student) {
    node_t *new = (node_t *)malloc(sizeof(node_t));

    new->pStudent = student;

    new->next = *(head);
    *(head) = new;
}

void print_list(node_t * head) {
    while (head != NULL) {
        printf("%s, %s\n", head->pStudent->last, head->pStudent->first);

        head = head->next;
    }

}

student_t *newStudent() {
    student_t *new = (student_t *)malloc(sizeof(student_t));
    new->first = (char *)malloc(32);
    new->last = (char *)malloc(32);

    printf("Enter first name of student: ");
    scanf("%31s", new->first);
    printf("Enter last name of student: ");
    scanf("%31s", new->last);

    return new;
}

【讨论】:

  • 谢谢!这很有意义。我已经进行了必要的更改,但不幸的是我仍然遇到分段错误。它会读取整个列表,然后启动。非常感谢您的帮助!
  • 这很奇怪。我发布的版本在我运行时运行良好。
  • 是的,实际上当我第二次编译时,它成功了!谢谢!
猜你喜欢
  • 2021-11-02
  • 2015-05-18
  • 2015-12-28
  • 1970-01-01
  • 2013-01-01
  • 2017-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多