Re: when can realloc fail?
- From: banansol@xxxxxxxxxxxx
- Date: 1 Apr 2007 12:15:45 -0700
On Apr 1, 9:06 pm, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxx> wrote:
banan...@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?
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?
Yes, realloc() can fail when shrinking an allocation, or
when growing it, or even when leaving its size unchanged.
When reallocating to zero size, the situation is complicated.
C90 says "If size is zero and ptr is not a null pointer, the
object it points to is freed." Since free() cannot fail (given
an argument that doesn't invoke undefined behavior), it follows
that realloc(...,0) cannot fail.
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)
.
- Follow-Ups:
- Re: when can realloc fail?
- From: Nelu
- Re: when can realloc fail?
- From: Hallvard B Furuseth
- Re: when can realloc fail?
- References:
- when can realloc fail?
- From: banansol
- Re: when can realloc fail?
- From: Eric Sosman
- when can realloc fail?
- Prev by Date: Re: when can realloc fail?
- Next by Date: Re: when can realloc fail?
- Previous by thread: Re: when can realloc fail?
- Next by thread: Re: when can realloc fail?
- Index(es):
Relevant Pages
|