tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Return Value of realloc(3)



Abhinav Upadhyay <er.abhinav.upadhyay%gmail.com@localhost> wrote:

> What should be the expected return value of realloc(3) in case you ask
> it to allocate 0 bytes ?
> 
> As per the standard
> <http://pubs.opengroup.org/onlinepubs/009696899/functions/realloc.html>
> it should either return NULL or a pointer which can be passed to
> free(). By running some tests, it appears that on NetBSD it is the
> latter case. Is this correct?

It satisfies one of the or'ed requirements, so it is correct by definition.


> If this is the case, then I think it should be clearly documented in
> the man page. 

Why?  Programs should not depend on that.  This is an implementation
detail that may change in the future, or change between runs because
of different options passed to allocator, or because of using another
allocator via LD_PRELOAD, etc, etc.  (Arguably, different calls to
realloc(..., 0) in the same program may return NULL and non-null
pointers and that still conforms to the standard).

Note that free(NULL) is correct, so newp = realloc(oldp, 0);
free(newp); is always correct, regardless of which of the two outcomes
of realloc actually happened.

-uwe



Home | Main Index | Thread Index | Old Index