2012-12-21 54 views
1

鏈表有這樣我如何釋放在Xcode

struct list 
{ 
    struct list *next; 

    int temp; 
}; 

我用下面的方法的結構來釋放

...。 ...。 ...。

// free linked list 
struct list *head_list = NULL; 
struct list *current_list = NULL; 
struct list *prev_list = NULL; 

current_list = head_list; 
while (current_file_info_arr != NULL) 
{ 
    prev_list = current_list; 
    current_list = current_list->next; 
    free(prev_list); 
} 

我得到警告

Memory error 
Use of memory after it is freed 

請問有什麼好的解決辦法嗎?

+0

你是怎麼分配的? – samfisher

回答

1

我想你只需要與

while (current_list != NULL) 

更換

while (current_file_info_arr != NULL) 

但是,這是假設你確實有一個適當的清單 - 分配/建造之前 - 和head_list點,它的開始。如果head_listNULL,就像在你的代碼片段:

struct list *head_list = NULL; 

那麼Memory error並不意外。你試圖釋放NULL,這確實是一個錯誤。