Subject: standards/1806: realloc() does not appear to free correctly.
To: None <email@example.com>
From: Peter Seebach <firstname.lastname@example.org>
Date: 12/03/1995 03:47:38
>Synopsis: realloc(ptr, 0) does not appear to behave correctly
>Responsible: gnats-admin (GNATS administrator)
>Arrival-Date: Sun Dec 3 05:05:00 1995
>Originator: Peter Seebach
Usenet Fact Police (Undercover)
>Release: November 20 or so
System: NetBSD taniemarie 1.1 NetBSD 1.1 (SEEBS) #3: Tue Nov 28 18:59:30 CST 1995 seebs@taniemarie:/usr/src/sys/arch/amiga/compile/SEEBS amiga
realloc(ptr, 0) is documented as freeing the given pointer. It
does not appear to do so correctly; in any event, repeated
realloc'ing produces a memory leak. The code shows no signs of
checking to see if the desired space is 0.
Arguably, it should also return NULL for such arguments; after all,
it returns NULL, or a pointer to the allocated space. Since
no space is allocated, there is no allocated space to return a
pointer to. The only pointer available which points to no object
is the null pointer. :)
This is a standards bug, because ANSI asserts that realloc(ptr, 0)
will simply free ptr, and also a practical bug; it is a memory
leak, if the user (correctly) chooses to ignore the return of
realloc, which can be a "valid" pointer to 0 bytes of memory.
(Which is also what you get from malloc(0). Which is legitimate.)
Allocate space. realloc() it to 0 bytes. Repeat. Watch your
Presumably, realloc(ptr, 0) should just call free(ptr) and return
a NULL, just as realloc(NULL, size) just acts like a malloc.