|
-
June 28th, 2008, 09:47 AM
#1
[C] problem with free()
Hi,
i have a list which is my p, this list could have lot of elements.
I use this code below to free the memory at the end of my program.
A strange things happen, at certain point i dont know why, the execution stop at the free(app);
I have this problem just on Windows! On linux every thing is fine
Does someone know what can cause this stop problem?
Code:
while(p!=NULL){
app=p;
free(app);
p=p->next;
}
thanks
-
June 28th, 2008, 10:03 AM
#2
Re: [C] problem with free()
Does
Code:
while (p != NULL) {
app = p;
p = p->next;
free( app );
}
fix it?
-
June 28th, 2008, 10:04 AM
#3
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);
}
-
June 28th, 2008, 10:08 AM
#4
Re: [C] problem with free()
thanks a lot i was going crazy
-
July 2nd, 2008, 06:42 AM
#5
Re: [C] problem with free()
may be freeing it reverse help
-
July 2nd, 2008, 07:32 AM
#6
Re: [C] problem with free()
 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..
Dont forget to rate my post if you find it useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|