2016-10-03 55 views
1

嗨,我需要幫助釋放我的鏈接列表C.當我運行此代碼時,我得到一個seg故障11.一切都取決於釋放完美的作品。感謝您的幫助!在C中的自由鏈接列表

#include <stdio.h> 
#include <stdlib.h> 

struct node{ 
    int x; 
    struct node *next; 
}; 

int main(void){ 
    struct node *root, *next; 

    root = malloc(sizeof(struct node)); 
    root -> x = 0; 
    root -> next = NULL; 

    next = root; 

    for (int i = 0; i <=10; i++){ 
     next -> next = malloc(sizeof(struct node)); 
     next -> x = i; 
     next = next -> next; 
    } 

    next = root; 

    for (int j = 0; j <= 10; j ++){ 
     printf("%i", next -> x); 
     next = next -> next; 
    } 
     next = root; 

    while(next != NULL){ 
     next = root; 
     root = root -> next; 
     free(next); 
    } 
    free(root); 
} 

回答

0

你的循環釋放的節點檢查是否next是它叫free(next)NULL

+0

我做循環的第一線釋放是相同的循環的最後一行,仍然收到了同樣的錯誤。你能幫忙嗎? – DMop

+0

沒有看到你是如何改變代碼的,也不知道哪個'free'有問題,不是真的。 –

0

正如其他答案所述,free()位於錯誤的地方,因爲它釋放了內存,然後在while循環的條件下進行檢查。

while(next != NULL){ 
    root = root->next; 
    next = root; 
    free(next); 
} 

但是同時,如果移動該塊的第一個語句塊的結束,你在你的評論所說,您的問題可能是,這個循環本身就完全足以釋放整個列表,所以在循環後面的語句free(root)可能是雙重空閒的,這通常是一個錯誤。你可以刪除它。

+0

我刪除了第二個免費的,並移動了我的評論中建議的代碼,但仍然有同樣的錯誤。你知道這可能是爲什麼嗎? – DMop

1

原代碼有幾個問題:

1)while(next != NULL)循環試圖使用已釋放的節點。

2)該循環已經照顧了釋放rootnext = root;),所以不需要單獨發佈root

3)爲使while環路正常工作,列表中的tail/head必須正確地以NULL終止。 (我在第一個for循環中添加了終止)

4)第二個循環應該打印所有的x值。它沒。櫃檯只有一個號碼。

該程序的改進變體如下。請檢查程序輸出。

#include <stdio.h> 
#include <stdlib.h> 

struct node{ 
    int x; 
    struct node *next; 
}; 

int main(void){ 
    struct node *root, *next, *to_free; 

    root = malloc(sizeof(struct node));     // 1 malloc 

    root->x = 777; 
    root->next = NULL; 

    printf("allocating memory for root. root->x = %i\n\n", root-> x); 

    next = root; // next points to "head" (root) 

    for (int i = 0; i <= 10; i++){ 
     next->next = malloc(sizeof(struct node));  // 11 mallocs 

     printf("allocating memory for next. next->x = %i\n", i+1); 

     next->next->x = i+1;       // 

     next = next->next; 
     next->next = NULL;        // list termination is needed! 
    } 
    printf("\n"); 

    next = root; // next points to "head" (root) 

    for (int j = 0; j <= 11; j ++){     // 12 nodes has to be printed! 
     printf("Printing x = %i\n", next->x); 
     next = next->next; 
    } 
    printf("\n"); 

    next = root; // next points to "head" (root) 

    while(next != NULL)   
    { 
     to_free = next;   // we will free `next` as `to_free` soon 

     next = next->next;  // move to the next node 

     printf("deallocating node with x = %i\n", to_free->x); 

     free(to_free);   // now free the remembered `next` 
    } 
return 0; 
} 

輸出:

allocating memory for root. root->x = 777 

allocating memory for next. next->x = 1 
allocating memory for next. next->x = 2 
allocating memory for next. next->x = 3 
allocating memory for next. next->x = 4 
allocating memory for next. next->x = 5 
allocating memory for next. next->x = 6 
allocating memory for next. next->x = 7 
allocating memory for next. next->x = 8 
allocating memory for next. next->x = 9 
allocating memory for next. next->x = 10 
allocating memory for next. next->x = 11 

Printing x = 777 
Printing x = 1 
Printing x = 2 
Printing x = 3 
Printing x = 4 
Printing x = 5 
Printing x = 6 
Printing x = 7 
Printing x = 8 
Printing x = 9 
Printing x = 10 
Printing x = 11 

deallocating node with x = 777 
deallocating node with x = 1 
deallocating node with x = 2 
deallocating node with x = 3 
deallocating node with x = 4 
deallocating node with x = 5 
deallocating node with x = 6 
deallocating node with x = 7 
deallocating node with x = 8 
deallocating node with x = 9 
deallocating node with x = 10 
deallocating node with x = 11 
+0

@DMop如果您重視我的幫助,您可以提出我的答案並接受它。我會很感激。如果你需要更多的解釋讓我知道。謝謝! – sg7