PDA

View Full Version : What is the Difference



peter.devon
January 15th, 2009, 07:08 AM
hi all,

What is the Difference between realloc() and free()? Please reply soon.

Thanks
-----------------------------
http://www.infysolutions.com

jpkotta
January 15th, 2009, 08:27 AM
sudo aptitude install manpages-dev
man realloc # they're on the same manpage

realloc() doesn't free memory (well, it could, but after it returns there is still allocated memory). It's functionally the same as free()ing and then malloc()ing again.

eye208
January 15th, 2009, 08:40 AM
It's functionally the same as free()ing and then malloc()ing again.
No, it's not.

realloc() will resize an allocated block without losing its content. This is very different from free() + malloc(). Once you call free(), the content is gone.

jpkotta
January 16th, 2009, 02:37 AM
No, it's not.

realloc() will resize an allocated block without losing its content. This is very different from free() + malloc(). Once you call free(), the content is gone.

Very true. I need to stop posting so late at night.

snova
January 16th, 2009, 04:15 AM
Their entire purpose. ;)

malloc() allocates memory.
calloc() allocates memory and initializes it to zero.
free() releases memory.
realloc() changes the size of an allocated memory block.

CptPicard
January 16th, 2009, 02:05 PM
realloc() changes the size of an allocated memory block.

With the gotcha that the pointer that comes back from realloc may or may not be the same pointer that went in. Content is not lost, but there can be copying going on in the background.