Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Can we implement free using malloc/realloc passig size zero to them?

In the following code blocks does realloc functions same as free?
C++
int *x = NULL;
x = (int*)malloc(7*sizeof(int));
memset(x,0,7);
printf("%u  %u  %d\n",&x, x,*x);
free(x);



C++
int *x = NULL;
x = (int*)malloc(7*sizeof(int));
memset(x,0,7);
printf("%u  %u  %d\n",&x, x,*x);

x = (int*)realloc(x,0*sizeof(int));
printf("%u  %u\n",&x, x);
Posted
Updated 13-Nov-11 7:50am
v2
Comments
Albert Holguin 13-Nov-11 18:00pm    
my first thought... "why the heck would you do that!?" ...but I guess it is "doable", although doable doesn't imply you should do it. +5 for the question since it sort of points out something that's not commonly known.

Not necessarily - a zero length block is not the same as a released block, it has a valid address and (probably) takes up space both for the block over/under run checking and in the allocation tables. An intelligent system might detect the zero length as re-use the same zero length block, but it doesn't have to and may not if the original coder didn't think it worth the time overhead on all non-zero blocks (i.e. nearly all of them) to do the checks.
 
Share this answer
 
Comments
rupeshkp728 13-Nov-11 14:36pm    
Thanks Griff for the reply.
For realloc() the msdn states "If size is zero, then the block pointed to by memblock is freed; the return value is NULL, and memblock is left pointing at a freed block."

So will not free be equivalent to realloc?
OriginalGriff 13-Nov-11 14:56pm    
In that specific case, yes, it is equivalent.
But you should not rely on that! It you port your code to a different C++ compiler, it is not guaranteed to do the same thing. Even a different MS C++ compiler does not have to do that...if someone else takes over and decides that the performance improvement by not doing a specific check for an empty block is more important than the memory space saved by do it, then it could be changed.

Plus, seeing "free(myBlock)" is a lot more obvious in your code than an zero-length re-alloc.
Chuck O'Toole 13-Nov-11 15:45pm    
FYI - it's in the standard so every compiler / RTL has to allow it. That doesn't mean it's a good thing to do.
Albert Holguin 13-Nov-11 17:55pm    
I didn't know this was part of the standard either... weird... but should also point out that compilers aren't necessarily standard compliant all the time
rupeshkp728 13-Nov-11 15:46pm    
Thanks Griff for the clarification.
Well, there are many strange conditions allowed for in the C++ standard. http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/[^]
but I wouldn't recommend adopting them as a coding style.

For example, you are allowed to free() a NULL pointer.

You can realloc() a NULL pointer and realloc() then acts as malloc()

You can realloc() a non-NULL pointer but specify the size as 0 so realloc() then acts as free()

They are allowed but if you decide to use those quirks instead of making reasonable calls to the usually library functions, you will end up causing lots of problems for anyone who tries to follow / modify your code in the future.

"Tricks are for Kids" (tm) Real Programmers write readable and maintainable code.
 
Share this answer
 
Comments
rupeshkp728 13-Nov-11 15:50pm    
Thanks Chuck for the inputs.
You see I have been with all strange questions today and all these were asked to me recently causing unnecessary confusion.
I too have never ever seen such techniques being used in real life projects.
Sergey Alexandrovich Kryukov 13-Nov-11 16:18pm    
Good points, a 5.
--SA
Albert Holguin 13-Nov-11 17:54pm    
great answer, +5... I also wouldn't recommend using "tricks" that have absolutely no benefit... no point and makes it harder to support for someone else.
Stefan_Lang 14-Nov-11 12:17pm    
I knew the first, and while I don't exactly see the necessity apart from the fact it's always been this way, I suppose it doesn't really hurt.

I was surprised by the second, but then it's only consequential when you consider the first, since realloc will (eventually) free the original pointer.

The last, then, is only consequential as well, because if you can realloc 'from 0', then you should be able to realloc 'to 0' as well.

I do agree, though, that I wouldn't know why you would want to use either of the realloc 'tricks' (I know the free trick is useful to save yourself the hassle of checking for a NULL pointer yourself).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900