Re: when can realloc fail?
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 01 Apr 2007 15:06:28 -0400
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?
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.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: when can realloc fail?
- From: banansol
- Re: when can realloc fail?
- References:
- when can realloc fail?
- From: banansol
- when can realloc fail?
- Prev by Date: Re: OT - Re: Looking for a *FAST* graphics library More options
- Next by Date: Re: when can realloc fail?
- Previous by thread: when can realloc fail?
- Next by thread: Re: when can realloc fail?
- Index(es):
Relevant Pages
|