Re: [C] problem with free()
Does
Code:
while (p != NULL) {
app = p;
p = p->next;
free( app );
}
fix it?
Re: [C] problem with free()
It looks like you used free(app) a little too early. It should probably be:
Code:
while(p!=NULL){
app=p;
p=p->next;
free(app);
}
Re: [C] problem with free()
thanks a lot i was going crazy :wave:
Re: [C] problem with free()
may be freeing it reverse help
Re: [C] problem with free()
Quote:
Originally Posted by Navimel
Code:
while(p!=NULL){
app=p;
free(app);
p=p->next;
}
Idea is when app=p; both are pointing to the same memory location.
and now you do free(app) hence memory pointed by app will be gone.
Since p also points to the same memory, hence memory pointed by p is also gone making it a dangling pointer and then you are trying to access the memory which no longer exists through the dangling pointer p.. Which lead to an UB and most likely a crash..