【发布时间】:2016-02-08 18:51:40
【问题描述】:
我正在尝试通过 C 中的链接列表。
列表项定义为
struct list_element {
struct list_element *next;
int value;
};
列表头定义为
struct list_head {
struct list_element *front;
struct list_element *end;
};
我正在尝试打印这样的项目
void printList(struct list_head* head) {
if(head == NULL|| head->front == NULL) {
printf("List is empty \n");
return 0;
}
struct list_element* elm = head-> front;
int numberOfElements = 0;
while(elm != NULL) {
printf("%i", elm -> value);
printf(" ");
elm = elm -> next;
}
printf("\n");
}
这在我的 Mac 上的 XCode 和 https://ideone.com 上完美运行,但在 Windows 和 http://codepad.org 上会导致“分段错误”。好像
while(elm != NULL) {
printf("%i", elm -> value);
printf(" ");
elm = elm -> next;
}
导致一些问题。 elm 似乎没有为最后一项指向 NULL,尽管它应该指向 NULL。
我正在添加这样的项目
struct list_element* list_push(struct list_head* head) {
//List head is null
if(!head) {
return NULL;
}
//Initialize list element
struct list_element* elm = malloc(sizeof(struct list_element));
if(elm == NULL) {
//Couldn't alloc memory
return NULL;
}
if(head->front) {
head->front = elm;
head->end = elm;
} else {
//List head is not null, set next elm to point to current elm
elm -> next = head -> front;
head->front = elm;
}
return elm;
}
我很困惑为什么相同的代码在某些地方可以工作,而在其他地方却不行。 (它适用于 IDEone 和 XCode,它不适用于具有相同代码的 Windows 上的 Codepad 和 Code::blocks)
【问题讨论】:
-
在 XCode 中调试不会显示任何错误,valgrind 也不会。但是运行编译后的二进制文件会导致分段错误
-
通常,如果您的程序在一台计算机上出现段错误并且似乎在另一台计算机上“工作”,那么它会表现出“未定义的行为”。谷歌“未定义行为 C”以了解更多信息。
标签: c xcode list linked-list segmentation-fault