Re: when can realloc fail?
- From: banansol@xxxxxxxxxxxx
- Date: 1 Apr 2007 14:06:34 -0700
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!
.
- Follow-Ups:
- Re: when can realloc fail?
- From: CBFalconer
- Re: when can realloc fail?
- References:
- when can realloc fail?
- From: banansol
- Re: when can realloc fail?
- From: santosh
- Re: when can realloc fail?
- From: banansol
- Re: when can realloc fail?
- From: santosh
- when can realloc fail?
- Prev by Date: Re: The weird while and If
- Next by Date: Re: Recursive functions
- Previous by thread: Re: when can realloc fail?
- Next by thread: Re: when can realloc fail?
- Index(es):
Relevant Pages
|