Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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语言中的空指针比较_C_Pointers_Null_Linked List_Segmentation Fault - Fatal编程技术网

C语言中的空指针比较

C语言中的空指针比较,c,pointers,null,linked-list,segmentation-fault,C,Pointers,Null,Linked List,Segmentation Fault,当(temp->next)->next为0时,我不知道为什么不输入此if语句。我还尝试了将(temp->next)->next转换为(void*),但没有成功 void Mem_Coalesce(){ list_t* temp; temp = freep; if( free_node_count == 2){ if( ((char*)temp + (temp->size) ) == (char*)(temp->next)){

(temp->next)->next
为0时,我不知道为什么不输入此
if语句。我还尝试了将
(temp->next)->next
转换为
(void*)
,但没有成功

    void Mem_Coalesce(){


    list_t* temp;
    temp = freep;
    if( free_node_count == 2){

        if( ((char*)temp + (temp->size) )  == (char*)(temp->next)){
                printf("Entered if loop\n");
                        temp->size += (temp->next)->size;
                temp->next = NULL;
                free_node_count--;
        }



    }
    else {
         printf("\n coalescing the case that free list has 3+ nodes\n");
        while(temp->next != NULL  ){

            if( ((char*)temp + (temp->size)) == (char*)(temp->next)){

                    if( (temp->next)->next == NULL){

                        temp->size += (temp->next)->size;
                        temp->next = NULL; //seg fault happens here 
                                                break;


                    }else{

                    temp->size += (temp->next)->size;
                                        temp->next = (temp->next)->next;
                    ((temp->next)->next)->prev = temp;
                    }
                    free_node_count--;

            }
            temp=temp->next;

        }

    }



}
以下是我收到的带有seg故障的输出:

Addr of temp->next is  7f4a836ae2e8

Addr of temp->next->next is  0

./cmd.sh: line 5: 31230 Segmentation fault      (core dumped) myprogram

我是否错误地将指针值与NULL进行了比较?

我认为问题是由于没有中断
而引起的。在
temp->next=NULL
之后没有
break
。现在它已修复。

第5行是什么?是if语句吗?@KristerAndersson:该消息与C代码无关,它只是告诉我们崩溃发生在从
cmd.sh
shell脚本第5行开始的程序中。将
-g
添加到编译标志中,然后使用
gdb
。在
gdb
中,使用
run
,然后使用
backtrace
。它应该能告诉你车祸发生在哪一行。这将帮助我们找出程序的错误。我希望在'if'块(第5行)之后的第一行尝试使用(temp->next)->next执行某些操作,可能会取消引用它。它为空,因此这将导致seg故障。请将整个代码粘贴到问题中。你所展示的没有错。