memory management - Invalid free Valgrind -
for reason calling function 'delall' more once cause invalid free error valgrind. don't understand why if call function second time cause program go while loop again though "delall" of node
//p linked list call
struct node{ char *str, int data, struct node *next; } //here's function having trouble with:
void delall() { struct node *temp,*temp2; temp=p; while(temp!=null) { temp2=temp; temp= temp->next; free(temp2->str); free(temp2); } }
p pointer list, , right still after delall call point (free'd) start of list. i'd do;
p=null; ...right after while loop set p null (ie have list cleared). prevent delall trying free elements again.
of course depend on p not being temporary variable, i'm assuming it's real "start of list" pointer.
Comments
Post a Comment