Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?



On Wed, 16 Aug 2006 01:01:06 UTC, "Bill Reid"
<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.


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.

Another tip:

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.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!

.



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I blame society. ... p is the pointer holding the address of the memory block. ... result of realloc until you knows that realloc returns memory ... thought reallocfreed the original memory block and returned NULL ...
    (comp.lang.c)
  • Re: realloc question
    ... By returning NULL, realloc lets you know ... >> that your memory block still has its original size. ... > Not that having a memory block reduction fail is something difficult to ...
    (comp.lang.c)
  • Re: when can realloc fail?
    ... A call to reallocwill return NULL on error and the original memory ... But a call to realloc() with size set to zero is equivalent to free, ... Does that mean that a call to realloccan fail when shinking memory ...
    (comp.lang.c)
  • Unit testing malloc and free
    ... Does anyone have a completely portable way to cause malloc and free to ... fail. ... I've tried to simply realloc until I run out of memory in N byte ...
    (comp.software.testing)
  • Re: How to get the memory block size which allocated by new operator?
    ... memory that not released.When allocate memory by new operator,I will ... set of memory management routines that are far superiour to malloc, realloc, ... Programmers know when they're combining multiple outputs into a single ... NULL is stuffed into pbBuf when realloc returns, destroying the only pointer ...
    (microsoft.public.vc.language)