Re: when can realloc fail?



On 1 Apr 2007 11:50:05 -0700, banansol@xxxxxxxxxxxx wrote:

Hi,
I just want to get this right.
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?

It returns NULL if the space cannot be allocated. Whether it is due
to an error or not is something else.

But a call to realloc() with size set to zero is equivalent to free(),
with returns void.

No it is not. Read the last sentence of 7.20.3 again.

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?

No. Success is not guaranteed.


Thanks!


Remove del for email
.



Relevant Pages

  • when can realloc fail?
    ... A call to reallocwill return NULL on error and the original memory ... both when requesting a larger or a smaller size that the original, ... But a call to realloc() with size set to zero is equivalent to free, ...
    (comp.lang.c)
  • Re: realloc and malloc [was: memfrob and strfry ? OT]
    ... The realloc function is called to resize memory pointed to by 'p' to the ... and if realloc fails it would evaluate to false. ... realloc can fail in which case the original memory is left untouched. ... event of realloc failure (unless the value previously in 'p' has been ...
    (comp.lang.c)
  • Re: realloc and malloc [was: memfrob and strfry ? OT]
    ... and if realloc fails it would evaluate to false. ... realloc can fail in which case the original memory is left untouched. ... event of realloc failure (unless the value previously in 'p' has been ... Just plain old ...
    (comp.lang.c)
  • Re: realloc and malloc [was: memfrob and strfry ? OT]
    ... So if realloc succeeds the expression within the parenthesis ... realloc were to be assigned to 'p' before checking for failure then ... we would lose access to the original memory in the event of realloc ... I seem to learn the concepts better with plain old code to ...
    (comp.lang.c)