for some reason calling my function ‘delAll’ more than once will cause a invalid free error from Valgrind. I don’t understand why if I call this function the second time would cause the program to go into the while loop again even though it just “delAll” of the node
//p is a linked list with call
struct node{
char *str, int data, struct node *next;
}
//here’s the function I am having trouble with:
void delAll()
{
struct node *temp,*temp2;
temp=p;
while(temp!=NULL)
{
temp2=temp;
temp= temp->next;
free(temp2->str);
free(temp2);
}
}
pis the pointer to your list, and right now it will still after the delAll call point to the (free’d) start of the list. I’d just do;…right after your while loop to set p to null (ie have the list properly cleared). That will prevent your delAll from trying to free all elements again.
Of course that would depend on
pnot just being a temporary variable, I’m assuming it’s the real “start of the list” pointer.