Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: "Herbert Rosenau" <os2guy@xxxxxxxxxxxxx>
- Date: Fri, 18 Aug 2006 18:52:11 +0000 (UTC)
On Thu, 17 Aug 2006 23:54:41 UTC, "Bill Reid"
<hormelfree@xxxxxxxxxxxxxxxx> wrote:
youWhen realloc fails you needs to either work with the memory (p)
have already or to cleanup (free(p). Overwriting p with NULL gives youHmmm...if this is the case this should be in the NON-documentation.
a memory leak as you lost the address of the memory you have laready
allocated.
Actually, I guess it kind of is, but after a quick read of this I always
thought realloc() freed the original memory block and returned NULL
if it couldn't reallocate the new block:
Yes, but it does NOT free() the old block then. That block leaves
unchanged.
...
Syntax
#include <stdlib.h>
void *realloc(void *block, size_t size);
...
If the block cannot be reallocated, realloc returns NULL.
If the value of size is 0, the memory block is freed and realloc returns
NULL.
---end of NON-documentation
I guess I got the last two conditions conflated in my mind; it just seemed
logical to me that if realloc() failed it would free the previous block.
It SEEMS like it should.
But it does not so because you may need to continue your work with the
old block.
When you have no need for the old block you have to free() that
yourself, else you should free() it. In any case you needs its
address.
As an orthogonal point, what horrible things happen if you try
to free() a NULL pointer?
Nothing. free(NULL); works like a noop. Nothing occures. That is
guaranteed.
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
.
- Follow-Ups:
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- References:
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Barry Schwarz
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Herbert Rosenau
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Herbert Rosenau
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Bill Reid
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Prev by Date: Re: Newbie needs help with pointers/memory allocation
- Next by Date: Re: Benchmark results (Re: Storage of char in 64 bit machine)
- Previous by thread: Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Next by thread: Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Index(es):
Relevant Pages
|