Mateusz said:
Hm, seems you've catched me.
I know how to _use_ it but I don't know exactly how it's implemented.
Setting it to NULL seems to not to be enough. Memory should albo be
deallocated.
I'd be glad to hear a deep explanation of this problem from soe Gurus.
Is there anyone who could explain those internals?
Cheers
That's exactly the point-- you're fussing about implementation details
which aren't specified by (nor relevant to) the operation of the class.
(I'll concede that my original reply was not precise enough for legal
work, but I think the message was clear to everyone, particularly the
original poster.)
In my limited experience, maps are usually implemented in terms of red
black trees and the individual data stored in the map are wrapped up in
some sort of node structure which includes the key, the value, and some
additional information (for example, pointers to other nodes in the tree).
You'll get a sense for this if you write your own STL allocator because
you can then observe the individual memory allocations and
deallocations. If you have a map<int,int> for example, and sizeof(int)
= 4, it would be fairly typical to find that each insertion results in
an allocation of size, say, 24, which you can see is 2 ints plus "some
other stuff". This confirms the sometimes surprising fact that a map
passed an allocator<T> when constructed may very likely never allocate
space for a T. Instead the allocator rebind mechanism is used to
allocate space for some other object-- in this case a tree node.
In fact, to really throw a wrench into things (and return to your point
above), I've seen implementations (e.g. RogueWave) which use block
allocations so that as soon as one item is added to the map, space is
allocated for say 32 items. Conversely, it's possible to erase items
from such a map and have no memory immediately deallocated. To the
user the map must appear not to contain the erased items, but there may
be no memory deallocation until a whole block's worth of items have been
erased. Or for that matter, until the implementation feels like
deallocating the memory.
Mark