[lug] new operator in C++

D. Stimits stimits at idcomm.com
Sat Apr 27 19:32:59 MDT 2002

Tkil wrote:
> >>>>> "DS" == D Stimits <stimits at idcomm.com> writes:
> i think you misunderstood my surprise.
> DS> delete was called after each new; thus the same memory slot was
> DS> likely to be reused each time.
> right.  but it wasn't, and that's what surprised me.  if it *had* been
> re-used, i would have expected to see 1 2 3 4 5 ... and so on.  but
> having just 1, then that constant number -- very strange!
> DS> Had you done all the new operators in a row on separate variables,
> DS> your results would not have been the same in all likelihood. Or if
> DS> you had done other memory operations possibly the slot being used
> DS> would have been different.
> weird, i am getting the same address each time.  something is
> overwriting it with a consistent value each time through the loop.
> actually, even in the first block (that's not a loop).

The memory slot to which was being pointed was overwritten, but the slot
holding the pointer itself was not. The "++(*pi)" has an effect only on
the indirect memory pointed to, whereas the new and delete were being
reapplied to the same memory location each time. If the memory mechanism
had other memory pressures on it, and between a delete and the next new
it had used that pointer slot up, I'd expect to see it change, otherwise
nothing forces it.

D. Stimits, stimits at idcomm.com

> i wonder what's doing it?
> and it does seem to do something more than just "put it on a free
> list", which is what i thought; if i try to do "*pi = 0" after the
> delete, i get a SEGV (even though pi itself has the same value as
> before the delete.)
> oh well.  confused, and too hungry to step through libstdc++ and glibc
> to figure out what's going on.
> t.
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug

More information about the LUG mailing list