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




Herbert Rosenau <os2guy@xxxxxxxxxxxxx> wrote in message
news:wmzsGguTDN6N-pn2-Chy9PK7Cy7sV@xxxxxxxxxxxxxxxxxxxxxxxxx
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.

I blame society.

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.

Hmmm...if this is the case this should be in the NON-documentation.
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:

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.

I'm not quite sure how this is functionally different from SOP using
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



.



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... mostenly when you tries to blame the compiler you blames yourself ... p is the pointer holding the address of the memory block. ... result of realloc until you knows that realloc returns memory ... You should initialise any variable when defining it to be sure to fail ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... > If reallocfinds it necessary to move the memory block, ... > realloc()'s memory that was freed by realloc, ... A proper realloccall can't invoke undefined behavior. ...
    (comp.lang.c)
  • Re: Does standard C guarantee same memory content after realloc()?
    ... >>The memory location will only change if at the current position there is ... > circumstance is realloc() constrained to return the old address. ... > about the circumstances when the memory block will have its address ... > So, could you, please, stop posting bullshit to this newsgroup? ...
    (comp.lang.c)
  • Re: A Tale of Two Memory Managers (long)
    ... I thought the main purpose of realloc was to check, if after the memory block there's enough heap memory free to prevent copying of the old memory content to the new memory position. ... The purpose of ReallocMem is to tell the memory manager how much memory you need for a pointer, and the memory manager will ensure that the buffer is large enough. ...
    (borland.public.delphi.non-technical)
  • 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)