Re: when can realloc fail?



On Apr 1, 10:17 pm, "santosh" <santosh....@xxxxxxxxx> wrote:
banan...@xxxxxxxxxxxx wrote:
On Apr 1, 9:07 pm, "santosh" <santosh....@xxxxxxxxx> wrote:
banan...@xxxxxxxxxxxx wrote:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error

The word failure might be more appropriate in this context than the
word error.

and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?

Yes.

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

Semantically yes.

with returns void.

No, it returns an indeterminate value.

What? My manpage says "void free(void *ptr);",
doesn't free return void? By that I meant, free doesn't return
anything.

You're correct. But realloc with a new size of zero either returns a
null pointer, upon failure, or returns an indeterminate value.

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. realloc is not guaranteed to suceed even when the new size is zero.

And as I wrote in the other post, I wonder how I can know that.

If the reallocation to zero size suceeds, an indeterminate pointer
value is returned, if it fails, a null pointer value is returned and
the original block is unchanged.

In that case there is something strange here, if I compile this in gcc

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *ptr = malloc(42);
if (realloc(ptr, 0) == NULL)
printf("realloc failed\n");
return 0;
}

I always get "realloc failed" printed, in both c89 and c99 mode.
I wouldn't expect it everytime as it does here.
Anyway, I will use free() instead, to avoid confusion.
Thanks for the help!


.



Relevant Pages

  • Re: realloc() implicit free() ?
    ... >>> the implementation in the case of zero block size requests. ... >> realloc, when provided a size of zero, cannot safely be passed ... The pointer returned if the ... Each such allocation shall yield a pointer to ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... >> the implementation in the case of zero block size requests. ... > realloc, when provided a size of zero, cannot safely be passed ... The pointer returned if the ... Each such allocation shall yield a pointer to ...
    (comp.lang.c)
  • Re: realloc() to zero size
    ... What happens if you reallocto a size of zero? ... Implementations are allowed to return NULL on malloc, and realloc() ... says it reutrns NULL on failure. ... either a pointer to a new object, or a null pointer if the new object ...
    (comp.lang.c)
  • Re: realloc() to zero size
    ... What happens if you reallocto a size of zero? ... Implementations are allowed to return NULL on malloc, and realloc() ... either a pointer to a new object, or a null pointer if the new object could ... indicate allocation failure, and on allocation failure, the old buffer is ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... >> realloc, when provided a size of zero, cannot safely be passed ... > pointer is returned. ... > zero block size as a request for one byte, and let the alignment ...
    (comp.lang.c)