为什么这个特定的代码会抛出异常?
Why Does this particular code throw an exeption?
检测到严重错误 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 使用 size_t = 10 调用函数 new_nodes,所以它不能太大。
Additional Info 一次连堆"broke".
感谢您的帮助。
这个:
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 澄清后)使用 'headless' 链表
void print_nodes(node* list)
{
node * current = list;
while (current)
{
printf("%i\n", current->value);
current = current->next;
}
}
检测到严重错误 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 使用 size_t = 10 调用函数 new_nodes,所以它不能太大。
Additional Info 一次连堆"broke".
感谢您的帮助。
这个:
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 澄清后)使用 'headless' 链表
void print_nodes(node* list)
{
node * current = list;
while (current)
{
printf("%i\n", current->value);
current = current->next;
}
}