首页 > 解决方案 > 链表中的访问冲突读取位置 0xCCCCCCCC

问题描述

我试图用以下代码创建链表 typedef 字符串是 char*

void insert_at_front(CourseList* self, String *course) {//inserting to the front

void insert_at_front(CourseList* self, String *course) {//inserting to the front
CourseNodePtr new = malloc(sizeof * new);
new->course=malloc(sizeof course+1);
strncpy_s(new->course, sizeof course+1, course, sizeof course+1);
new->students = new_bst();
new->next = self->head;
self->head = new;

}

当我尝试在课程中输入任何字符或字符串时,我如何在 Project.exe 中的 0x66C5FF80 (ucrtbased.dll) 处引发错误异常:0xC0000005:访问冲突读取位置 0xCCCCCCCC。只是不知道代码有什么问题。

更新:我已经编辑了代码,它运行但如果我输入数字,课程中没有存储任何内容。如果我输入任何字符,它会循环并再次循环。

void insert_at_front(CourseList* self, String* course) {//inserting to the front
CourseNodePtr new_node;


if (!(new_node = malloc(sizeof * new_node))) {
    perror("malloc");
    exit(1);

    if (!(new_node->course = malloc(strlen(course) + 1))) {
        perror("malloc");
        exit(1);
    }
    strncpy_s(new_node->course, strlen(course) + 1, course, strlen(course) + 1);
    new_node->students = new_bst();
    new_node->next = self->head;
    self->head = new_node;
}

标签: visual-c++

解决方案


推荐阅读