tech-userlevel archive

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

Re: Return Value of realloc(3)

On Sun, Dec 30, 2012 at 5:46 AM, Valeriy E. Ushakov 
<> wrote:
>> 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).

My point was that we should document this exact behaviour as mentioned
in the standards.
"If size is 0, either NULL or a unique pointer that can be
successfully passed to free(3) is returned."

> 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.

Right. I actually came across this due to a bug in some code, where it
was trying to reallocate more memory by calculating the size
Under some circumstances, the calculated size value to be passed to
realloc was turning out to be zero and the code was trying to do
realloc(p, 0);, and
checking for NULL in the return value. I have fixed the faulty code,
but I thought probably this was an important detail missing in the


Home | Main Index | Thread Index | Old Index