Re: when can realloc fail?



banansol@xxxxxxxxxxxx wrote:
On Apr 1, 9:06 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxx> wrote:
banan...@xxxxxxxxxxxx wrote:

<snip>

A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

<snip>

But C99 has no such text, and makes no special case for size
zero. All we're told is that realloc(...,0) either fails or it
returns a pointer to an object of size zero. Nothing I can find
in C99 forbids realloc(...,0) to fail, so presumably it can.

Thank you for your reply!
But I wonder, if realloc(...,0) fails in C99, how can I know that?
How can I know if a returned NULL means that realloc() failed,
or if it is the pointer to the new memory, empty, which is NULL.
(That is what I got on my compiler anyway)

In C99 it only returns NULL when it fails. If it returns non-null
and the size is 0 then it will return an address to a block of
memory of zero size. The address *will not be NULL* and you will
have to free it using free().

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
.



Relevant Pages

  • Re: New version of John Hayes tester
    ... and the absolute test passes. ... But zero is special -- there's no way to say that a nonzero x is ... The above F= test is superfluous unless ABS-NEAR is set to zero. ... = y and relative comparison fails in which I wouldn't want to ...
    (comp.lang.forth)
  • Re: adapting getline
    ... This only works if realloc never fails. ... In practise realloc never fails in this situation as the amount of memory ... For some reason he ...
    (comp.lang.c)
  • Re: when can realloc fail?
    ... A call to reallocwill return NULL on error and the original memory ... But a call to realloc() with size set to zero is equivalent to free, ... Does that mean that a call to realloccan fail when shinking memory ...
    (comp.lang.c)
  • Re: when can realloc fail?
    ... A call to reallocwill return NULL on error and the original memory ... But a call to realloc() with size set to zero is equivalent to free, ...
    (comp.lang.c)
  • Re: [C] please critique my code
    ... If realloc() fails, it returns a NULL pointer, and leaves the old buffer ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)