2013-10-18 81 views
0

我不是爲什麼我不輸入這個if statement(temp->next)->next是0.我也試過鑄(temp->next)->next(void*)但它沒有工作!C空指針比較

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; 

     } 

    } 



} 

這是我收到具有賽格故障輸出:

Addr of temp->next is 7f4a836ae2e8 

Addr of temp->next->next is 0 

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

有我比較不正確對NULL指針值?

+1

什麼是第5行?這是if語句嗎? – luiscubal

+0

它只是運行第5行中的程序'#!/ bin/bash gcc -c -fpic mem.c -Wall -Werror gcc -shared -o libmem.so mem.o gcc -lmem -L。 -o myprogram mymain.c -Wall -Werror myprogram ' –

+0

@MonaJalal:如果這是輸出,那些行看起來不錯;你確定這次事故發生在這裏嗎?你是否試過做一些調試(例如,只是添加一些'printf'來查看程序運行的位置而不會崩潰)?你能展示一些更多的上下文嗎? –

回答

0

我認爲問題引起沒有breaktemp->next=NULL之後沒有break。現在它是固定的。