The following program causes mtrace.pl to report memory leaks. > cat test.c void main() { int* i; mtrace(); i=malloc(sizeof(int)); i=realloc(i,0); muntrace(); } mtrace ./t.dat Memory not freed: ----------------- Address Size Caller 0x0000000000a96460 0x4 at 0x400678 It looks like mtrace.pl is not considering realloc(x,0) equivalent to a free.
This program has a memory leak. On glibc, malloc(0) and realloc(i, 0) both return a unique pointer that cannot be dereferenced. Failure to free this pointer is a memory leak. realloc(i, 0) is only equivalent to free(i) on implementations where malloc(0) returns 0. I suspect you're confused by the language in POSIX that says "If size is 0 and ptr is not a null pointer, the object pointed to is freed." This does not preclude a memory leak. Note that POSIX later says "If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned." The confusion stems from the fact that POSIX has misleadingly reworded the specification of free to refer to "changing the size of an object", a concept which ISO C intentionally avoids. In the ISO C language, a successful realloc _always_ frees the original object, and returns a pointer to a new object with the same contents (up to the minimum of the old and new size).
In glibc realloc(0,s) == malloc(s), and realloc(i,0) == (free(i),0) for i != 0.
Regarding realloc with size 0, see also <http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_400.htm>. Some of the problems with its semantics arose out of a change in C99 to the wording that was used in C90.
Fixed in 2.18.
*** Bug 260998 has been marked as a duplicate of this bug. *** Seen from the domain http://volichat.com Page where seen: http://volichat.com/adult-chat-rooms Marked for reference. Resolved as fixed @bugzilla.