检测到严重错误c0000374

#pragma once

typedef struct node
{
int value;
node* next;
node* before;
}   node;

void print_nodes(node* list) {
node *current = (node*)malloc(sizeof(node));
//current->value = 0;
current->next = list;

while (current->next != nullptr) {
    printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
    current->next = current->next->next;
}
free(current);
}

void add_node(node* list) {

}

inline void new_nodes(node* list, size_t anzahl) {
list[0].before = NULL;
for (int i = 0; i <= anzahl; i++) {
    list[i].value = i + 1;
    list[i].next = &list[i + 1];
    list[i + 1].next = &list[i - 1];
}
list[anzahl].next = NULL;
}

printf语句引发异常。。。但只是有时候。
我的cpp调用函数new_nodes,其大小为10,因此不能太大。
额外的信息一次甚至堆“坏了”。
谢谢你的帮助。

最佳答案

这:

void print_nodes(node* list)
{
    node *current = (node*)malloc(sizeof(node));
    //current->value = 0;
    current->next = list;

    while (current->next != nullptr)
    {
        printf("%i\n", current->next->value); <-THROW an Exception in the Fist loop
        current->next = current->next->next;
    }
    free(current);
}

需要大量修改:建议:
修改(经OP澄清后)使用“无头”链表
void print_nodes(node* list)
{
    node * current = list;

    while (current)
    {
        printf("%i\n", current->value);
        current = current->next;
    }
}

关于c - 为什么此特定代码引发异常?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53794568/

10-09 07:26