Yes, ofcourse I'm not intending for anyone to write such a code.

Just tried a rough explanation for newbies to understand,
which I now realize was not entirely correct and clear.

> Your program works fine because even if you have freed the memory,
> it has not been reused/re-allocated yet, so the pointer pointing to this
> portion
> still holds the same data and you can work on it.

 -This is where the internal allocator probably will reuse the freed
portion.
  This is why sometimes under less memory stress, the program won't
  segfault and some of these bugs fall through the crack.

  - I missed out the case when the allocator will release the page back to
the OS,
  as you pointed out will result in a segmentation fault. This usually is
surfaced by
  testing under memory stress conditions.


-
Santosh Vernekar


On Tue, Nov 11, 2008 at 7:04 PM, Thomas Hruska <[EMAIL PROTECTED]>wrote:

>   Santosh Vernekar wrote:
> > Your program works fine because even if you have freed the memory,
> > it has not been reused/re-allocated yet, so the pointer pointing to this
> > portion
> > still holds the same data and you can work on it.
> >
> > However this might fail when you free the memory and try some new
> > allocations,
> > then now try to access the memory (probably you wont get the same data,
> > as it might have been reused).
> > Now the pointer starts misbehaving. So it is a common practice to
> > NULLify the
> > pointer which you have freed, so that the program doesn't make any more
> > access
> > to the freed portion.
> >
> >
> > Santosh Vernekar
> > Engineer
> >
> > - "They say that if you play a microsoft disc backwards you hear satanic
> > messages,
> > thats nothing: if you play it forwards it installs windows"
>
> No. Wrong. It only works because the internal allocator has not given
> the address to another portion of the program _AND_ has not released the
> page back to the OS. The allocator is hanging onto the page for future
> allocations. Had it released the page and you attempted a memory access
> at the same address, a segmentation fault would have happened.
>
> You state, "Now the pointer starts misbehaving". Also false. The
> moment the memory is freed is the moment the pointer is to no longer be
> used (or in your words, "starts misbehaving"). This discussion is
> completely moot - undefined behavior is just that - undefined.
>
> Don't write code like that and you'll be fine.
>
> --
> Thomas Hruska
> CubicleSoft President
> Ph: 517-803-4197
>
> *NEW* MyTaskFocus 1.1
> Get on task. Stay on task.
>
> http://www.CubicleSoft.com/MyTaskFocus/
>
>  
>


[Non-text portions of this message have been removed]

Reply via email to