I have a structure like this
struct list
{
struct list *next;
int temp;
};
I use the following method to free
…. …. ….
// 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);
}
I get the warning
Memory error
Use of memory after it is freed
Is there any good solution?
I think you just need to replace
with
But that’s assuming you actually have a list in place – allocated/constructed previously – and
head_listpoints to beginning of it. Ifhead_listisNULL, like in your snippet:then the
Memory erroris not a surprise. You are trying to freeNULL, which is an error, indeed.