Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中棘手的seg故障_C_Pointers_Segmentation Fault_Gdb_Fault - Fatal编程技术网

C语言中棘手的seg故障

C语言中棘手的seg故障,c,pointers,segmentation-fault,gdb,fault,C,Pointers,Segmentation Fault,Gdb,Fault,我正试图为我的大学作业运行一个C项目,但我在以下代码段的“while(current->next!=NULL){”行中遇到了一个seg错误: FILE* f = fileOpen("test.txt"); if (f != NULL){ functionList = fileReadToMemory(f, &graphParams);//functionList is a pointer to the first value of the linked list it creat

我正试图为我的大学作业运行一个C项目,但我在以下代码段的“while(current->next!=NULL){”行中遇到了一个seg错误:

FILE* f = fileOpen("test.txt");
if (f != NULL){
    functionList = fileReadToMemory(f, &graphParams);//functionList is a pointer to the first value of the linked list it creates
    current = functionList;

    while (current->next != NULL) {
        printf("%d %d %d %s", current->red, current->green, current->blue, current->expression);//Prints value of linked list
        current = current -> next;
    }
}
gdb给我的错误如下:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000003a
0x0000000100000b30 in main () at main.c:23
23          while (current->next != NULL) {
我做错了什么

提前感谢!

您需要做些什么

while (current != NULL) 
而不是

current->next != NULL 

因为列表中的最后一个元素将导致segfault。

。这几乎是一个副作用,如果返回的
functionList
恰好是
NULL
,那么它也将正常工作。是的,在这种情况下,链接列表的头是NULL,因此while(当前!=NULL)可以解释为“链表还有其他元素吗?”是的,在这种情况下,链表的头是空的。因此,空值仍然是有效的链表,只是它是一个空链表。因此while(current!=null)可以解释为“链表还有其他元素吗?”?,并且它适用于任何大小的链接列表,甚至0。