不确定为什么我的 C 程序在处理链表时给出错误代码 "Process exited after 2.55 seconds with return value 3221225477"

Not sure why my C program is giving the error code "Process exited after 2.55 seconds with return value 3221225477" when dealing with linked lists

我正在构建一个学生管理系统,它必须使用链表。 其中一个要求是每个学生都必须有一个他们注册的课程列表。为了处理这个问题,我实现了 2 个单独的链表,然后在必要时加入它们。但是执行printList函数时,只有最后一个输入的学生得到输出,然后出现上述错误代码。此外,我在在线编译器中尝试了 运行 代码,它运行良好,我们将不胜感激。

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

//Structures creation
typedef struct course {
    char courseName[40];
    int credits;
    struct course *nextCourse;
} course;

typedef struct student {
    char firstName[50];
    char lastName[50];
    int age;
    char address[100];
    char progName[60];
    course *courseList;
    struct student *next;
} student;

void *checkMalloc(size_t num_bytes);
void addStudent(student **headStd, char *fName, char *lName, int age, char *address, char *progName);
void addCourse(student *currStd, char *cName, int cred);
void printList(student *first);

int main() {
    student *headStd = NULL;
    
    addStudent(&headStd, "John", "Doe", 19, "Lot 123", "Comp Sci");
    addCourse(headStd, "CSE2100", 4);
    addCourse(headStd, "CSE2101", 4);
    
    addStudent(&headStd, "Jane", "Smith", 20, "Lot 3 East Street", "Info Sys");
    addCourse(headStd, "ISY2100", 4);
    addCourse(headStd, "ITE2101", 3);
    
    printList(headStd);
    
    return EXIT_SUCCESS;    
}

void addStudent(student **headStd, char *fName, char *lName, int age, char *address, char *progName) {
    student *newStd = checkMalloc(sizeof(*newStd));

    strcpy(newStd->firstName, fName);
    strcpy(newStd->lastName, lName);
    newStd->age = age;
    strcpy(newStd->address, address);
    strcpy(newStd->progName, progName);

    // This line was missing in the original code posted
    // newStd->courseList = NULL;

    newStd->next = (*headStd);
    (*headStd) = newStd;
}

void addCourse(student *currStd, char *cName, int cred) {
    //Memory allocation
    course *newCourse = checkMalloc(sizeof(*newCourse));

    strcpy(newCourse->courseName, cName);
    newCourse->credits = cred;
    
    newCourse->nextCourse = currStd->courseList;
    currStd->courseList = newCourse;
}

void printList(student *first) {
    while (first != NULL) {
        printf("Name: %s %s\n", first->firstName, first->lastName);
        printf("Age: %d\n", first->age);
        printf("Address: %s\n", first->address);
        printf("Program Enrolled: %s\n", first->progName);
        
        course *currCourse = first->courseList;
        if (currCourse == NULL) {
            printf("\tNo Courses Present\n");
        } else {
            while (currCourse != NULL) {
                printf("\tCourse Name: %s\n", currCourse->courseName);
                printf("\tCourse Credits: %d\n", currCourse->credits);
                currCourse = currCourse->nextCourse; 
            }
        }
        first = first->next;
    }
}

void *checkMalloc(size_t num_bytes) {
    void *memory = malloc(num_bytes);
    if (memory == NULL) {
        printf("Error, couldn't allocate memory.\n");
        exit(-1);
    }
    return memory;
}

在您发布的代码中,成员 newStd->courseList 没有被函数 addStudent 初始化。由于内存是用 malloc 分配的,它的内容是不确定的,这意味着指针 courseList 可能是一个无效指针。

然后您在此无效指针之前插入课程列表... 第二个学生也是如此……那里的无效指针还没有被取消引用,但正等着像毒蛇一样咬人。

当您打印学生详细信息和课程列表时,在最后一门课程之后,行 currCourse = currCourse->nextCourse; 将无效指针读入 currCourse 并且下一次迭代将在取消引用该指针时崩溃。

这解释了观察到的行为,但是这种未定义的行为不能保证是可观察的,因为 malloc 也可能 return 内存恰好被所有位填充为零,因此初始值newStd->courseList 可能是空指针。

修复很简单:在 addStudent().

中添加 newStd->courseList = NULL;