[lug] new operator in C++

D. Stimits stimits at idcomm.com
Sat Apr 27 23:41:07 MDT 2002


Tkil wrote:
> 
> >>>>> "DS" == D Stimits <stimits at idcomm.com> writes:
> 
> DS> The indirect memory being pointed to is no longer accessible after
> DS> the free. The pointer itself still exists, just not what it points
> DS> at.
> 
> let me try to make it clear here that i do, in fact, understand
> pointers.  :)
> 
> what confuses me is that it appears that the allocator actually gives
> the memory back to the system, since i segv when i try to access it
> after "delete".

It is supposed to do that. The very purpose of delete is to give it
back. It isn't uncommon to use a custom allocator that does not give it
back, but in which a pool is reused without the overhead of continuously
doing system calls.

> 
> in this context of a program which relies on a library to do most of
> this management, that library only grabbing chunks from the operating
> system and then giving them back when done, i'd expect the library to
> maintain a list of free slots/blocks that it can give out on demand.

I think the kernel VM manager is responsible for this. Any other
behavior requires custom allocators.

> in this case, since i'm continuously doing "new" and "delete", i
> expected to get the same chunk of memory each time -- and i do (at
> least in the virtual sense: printing out "pi" itself shows that the
> address, as seen by the program, didn't change).
> 
> so, the things that surprised me:
> 
> 1. the allocator gave the memory all the way back to the OS (which is
>    what i thought had to happen before i'd get a SEGV).

Yes.

> 
> 2. that something else is scribbling consistently into that memory.
>    i suppose it's something in the iostream classes, but i'm surprised
>    that anything in there is doing free store manipulation.

Don't forget, each time the system gives you memory in user space, VM
has remapped it. You are seeing only one mapping, the physical memory
location could be anywhere. Chances are that a repeat of the same
operation without disturbing memory in other ways makes it more than
possible to get the same actual slot.

> 
> not that it really matters.  just the usual "hm, i didn't know it
> worked that way" type over-analysis.  as someone pointed out, just
> initialize the damned thing and be done with it.  :)

That's the advice of an ancient wise-one: initialize :P

D. Stimits, stimits at idcomm.com

> 
> 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