realloc

Unix Consultation Mailbox consult at osiris.UUCP
Thu Mar 30 04:30:20 AEST 1989


In article <10170 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu (Steve Summit) writes:
>It is an apparently well-
>kept secret that realloc is supposed to behave gracefully at
>a slightly special-cased boundary point: when handed a NULL
>pointer and a nonzero size, it acts essentially as a malloc.

I'll say it's well-kept, neither the 4.2BSD nor SysV.2 manpages imply
that calling realloc with first argument of 0 will do anything useful,
let alone that it's *supposed* to.  The SunOS 4.0.0 manpage even says
that "[realloc will fail if] an invalid argument was specified.  The
value of ptr passed to [realloc] must be a pointer to a block previously
allocated by malloc, calloc, realloc, valloc, or memalign."  I'm afraid
0 doesn't qualify as a pointer to a legitimately-allocated block, so
realloc *should* fail in this situation with errno == EINVAL.  That's a
far cry from "is supposed to ... [act] as a malloc."

I suggest that you be more careful in the future about which effects
of library functions are required and which are arbitrary and unreliable.
(I wish all our programmers would do that too, it would keep me from
having to do stupid things with null ptrs in my case-insensitive strcmp
and other such garbage.)


Phil Kos



More information about the Comp.lang.c mailing list