You are accessing an unitialized memory-area, which is denoted by the value 0xcdcdcdcd
. You must not simply free
your list by deleting the first element, but by deleting all elements, iterating through your list and freeing every node, because you would create a memory-leak otherwise
void free_list(list* anchor){
list* temp = anchor->next_list;
free(anchor);
while(temp->next_list){
list* l = temp->next_list;
free(temp);
temp = l->next_list;
}
}
Additionally, set the next element explicitly to NULL
, when adding a node and you are having a weird cross-reference in your function
list* insert_list(list* current_position,list_data x)
{
list* temp;
temp=(list*)malloc(sizeof(list));
temp->number=x;
//temp->next_list=current_position->next_list; -> doesn't make sense
current_position->next_list=temp;
temp->next_list = NULL; //set to NULL
return temp; //inserts a new block with a data of x
}
I think four you have not explicitly told that the next
item is NULL
you are iterating past the actual end of the list.