C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2009, 03:34 PM   #1
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,183
no double free for win XP?

t014y says in the concurrent "link list math" thread that this compiles and executes without error on win XP:
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
	void *ptr = malloc(666);
	free(ptr);
	puts("okay!");
	free(ptr);
	puts("okay!");
	return 0;
}
What's the deal? I thought that kind of mistake would be Universally Impermissible?
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Old 02-20-2009, 03:38 PM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
It's undefined behavior. It may or may not crash or error or destroy your computer.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-20-2009, 03:38 PM   #3
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,280
Maybe in debug mode free() checks a list of allocated pointers and only tries to free it if it's on that list?
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
cpjust is offline   Reply With Quote
Old 02-20-2009, 06:02 PM   #4
Registered User
 
Join Date: Sep 2008
Posts: 33
Question

i was told that if the pointer was pointing to 'NULL' then free() did nothing... not make the program crash. but i don't know that much.

but if that is true then you could free the same thing any amount of times without crashing the program... right?
t014y is offline   Reply With Quote
Old 02-20-2009, 06:11 PM   #5
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,183
Quote:
Originally Posted by t014y View Post
i was told that if the pointer was pointing to 'NULL' then free()
Does ptr==NULL in that program? Never! I think my point is it's not your fault, t014y, that this twist has slipped by you if the compiler/OS, etc. let it.

What will happen is that the memory pointed to by ptr is freed, returned back to a pool used by the compiler. But the actual value of ptr, which is that memory address, has not changed (it would if you set it to NULL). You, yourself, and I are free to use that address (since we have it in a variable, ptr) however we like. The problem will arise if you are freeing and allocating in a loop like this, because the compiler will probably stick the next instance of x into that (same size) block, then you "double free" it because you have TWO pointer variables containing the same address. Or -- if you try to realloc one of them when the other just freed -- you will have some similar complication.

Basically, double-freeing occurs when you've lost control of the memory and that is guaranteed to cause a problem sometime soon. In the example I gave, it's easy to get away with, because no more memory is ever allocated so the space is safe. But it shows you're not getting told what you should have been.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Old 02-20-2009, 06:11 PM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by t014y View Post
i was told that if the pointer was pointing to 'NULL' then free() did nothing... not make the program crash. but i don't know that much.

but if that is true then you could free the same thing any amount of times without crashing the program... right?
The only way ptr would be NULL in the above code is if malloc failed (or one of the standard functions in the code has been replaced by a macro or something is REALLY b0rkened somewhere in the C runtime library).

It is most likely that whatever C runtime this is, it happens to work. Double free, after all, is undefined behaviour, not a "defined to crash".

Setting the pointer to NULL after free is definitely a good thing to do.

Double free can also cause problems later. For example, it may be that free is inserting into a linked list, and if the object has already been freed, it gets inserted twice. Guess what happens when you next ALLOCATE two blocks of that size? You get two pointers to the same bit of memory. Now try to debug why when you update ptr1, the values referenced by ptr2 that you allocated in a completely different place, is changing.... Nasty to figure that one out.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 02-20-2009, 06:15 PM   #7
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,183
Quote:
Originally Posted by matsp View Post
It is most likely that whatever C runtime this is, it happens to work. Double free, after all, is undefined behaviour, not a "defined to crash".
I actually spit an edit about this:
Quote:
In the example I gave, it's easy to get away with, because no more memory is ever allocated so the space is safe. But it shows you're not getting told what you should have been.
quick like a bunny!
Quote:
Setting the pointer to NULL after free is definitely a good thing to do.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is online now   Reply With Quote
Old 02-21-2009, 02:43 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,763
Quote:
Originally Posted by cpjust View Post
Maybe in debug mode free() checks a list of allocated pointers and only tries to free it if it's on that list?
That's the opposite of what should happen in debug mode. In debug mode, such things should be checked for, and the program should crash if it happens.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-21-2009, 10:10 PM   #9
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,280
Quote:
Originally Posted by brewbuck View Post
That's the opposite of what should happen in debug mode. In debug mode, such things should be checked for, and the program should crash if it happens.
You should also crash if you write past the end of an array, but since VC++ (and maybe other compilers) add extra padding before and after stack variables in debug mode, you don't crash unless you write past the end of the array and the padding.
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
cpjust is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
double free or corruption (out) Mouser58907 C Programming 5 02-25-2009 12:20 AM
double free or corruption (fasttop) yougene C Programming 6 01-17-2009 06:44 PM
[Inheritance Hierarchy] User Input on program with constructors. How ? chandreu C++ Programming 8 04-25-2008 02:45 PM
Copying 2-d arrays Holtzy C++ Programming 11 03-14-2008 03:44 PM
double free or corruption??? hwttdz C++ Programming 2 07-22-2006 03:02 PM


All times are GMT -6. The time now is 03:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22