I found that some people and references like books state that if p != NULL
and p
origins from previous allocation (e.g. by malloc
), then realloc(p, 0)
is equivalent to free(p)
on GNU/Linux. To support this thesis man realloc
states exactly in that manner (emphasis mine going forward):
The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
As you may find in this question, the C Standard does not define precisely what should happen and actual behavior is implementation-defined. More specifically:
The C11 §7.22.3/p1 Memory management functions says:
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
and C11 §7.22.3.5 The realloc function contains:
3) (...) If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.
4) The
realloc
function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.
I wrote some basic code to find out actual behavior with help of mcheck
, memory checker, that is supplied with glibc
:
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a = 5;
int *p, *q;
mtrace();
p = malloc(sizeof(int));
q = &a;
printf("%p\n", (void *) p);
printf("%p\n", (void *) q);
q = realloc(p, 0);
printf("%p\n", (void *) p);
printf("%p\n", (void *) q);
return 0;
}
and results are:
$ gcc -g check.c
$ export MALLOC_TRACE=report
$ ./a.out
0xfd3460
0x7ffffbc955cc
0xfd3460
(nil)
[grzegorz@centos workspace]$ mtrace a.out report
Memory not freed:
-----------------
Address Size Caller
0x0000000000fd3460 0x4 at /home/grzegorz/workspace/check.c:12
As you may see q
was set to NULL
. It seems that free()
was not really called. In fact it can't be unless my interpretation is incorrect: since realloc
has returned NULL
pointer, the new object could not have been allocated, which implies that:
the old object is not deallocated and its value is unchanged
Is this correct?
realloc(ptr, 0)
being equivalent tofree(ptr)
used to be in POSIX, but the official POSIXrealloc
reference now uses the language from the C standard. The POSIX standard does however say "ifrealloc()
returns a null pointer, the space pointed to by p has not been freed". – Some programmer dude