Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: "Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx>
- Date: Thu, 17 Aug 2006 23:54:41 GMT
Herbert Rosenau <os2guy@xxxxxxxxxxxxx> wrote in message
news:wmzsGguTDN6N-pn2-Chy9PK7Cy7sV@xxxxxxxxxxxxxxxxxxxxxxxxx
On Wed, 16 Aug 2006 01:01:06 UTC, "Bill Reid"I blame society.
<hormelfree@xxxxxxxxxxxxxxxx> wrote:
Anybody blames her/himself as s/he can. :-) You will learn that
mostenly when you tries to blame the compiler you blames yourself
instead.
Hmmm...if this is the case this should be in the NON-documentation.
As to your code, I'm not sure why you do the two-step
with "*p" and "*temp"...it seems like all is required is to set
*p=NULL when declared, at least that's what I did, and
it worked just fine. Am I (again!) missing something?
p is the pointer holding the address of the memory block. As realloc
an fail (returning NULL) you needs another pointer to assign the
result of realloc until you knows that realloc returns (new) memory
address.
When realloc fails you needs to either work with the memory (p) you
have already or to cleanup (free(p). Overwriting p with NULL gives you
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:
....
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.
As an orthogonal point, what horrible things happen if you try
to free() a NULL pointer?
Another tip:I'm not quite sure how this is functionally different from SOP using
You should initialise any variable when defining it to be sure to fail
on that because you've not already assigned a known valid value. So
initialise a 0 (or a value you can easyly identify as invalid to data
and NULL to pointer. Then learn how to use a debugger and set a
breakpoint immediately before the fail occures, analyse the date found
there and then, when anything seems ok step a single step forward and
analyse again until the failture occures.
a debugger; initialized or not, I can "mouse over" ALL the variables
after an exeption point or break point, so I don't see garbage at an
exception point I step back and put in a break, run it again, check
the values, step forward, etc.
---
William Ernest Reid
.
- Follow-Ups:
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Herbert Rosenau
- Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- From: Keith Thompson
- 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
- Can I Trust Pointer Arithmetic In Re-Allocated Memory?
- Prev by Date: Re: Screen Editing
- Next by Date: Re: help for writing my own assert()
- 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
|