2012-12-21 51 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,这确实是一个错误。