if i have a map of size 7, so something like this:

/ \
/ \
2 3
/ \ / \
/ \ / \
4 5 6 7

and i have the key and the value of the map to be both ints, then would memory be:

7 * 4 = 28 for the keys
7 * 4 = 28 for the values
6 * 4 = 24 for the pointers

= 80 bytes?

im not sure how maps are implemented - is there anything else i have not taken into account? are there pointers each way (ie doubley linked)?

11 Years
Discussion Span
Last Post by unclepauly

Is this a C++ map? Those are usually written as red-black trees and they'll probably have a parent pointer too. You can guestimate three pointers per node plus the size of the keys and values per node. So 7 nodes with 32 bit integer keys and values and 32 bit pointers you could guess about 140 bytes on the upper end.

But it's important to remember that this is a guess and nothing more. You can't figure it out exactly unless you know how a particular compiler did the map, and even then your answer is only accurate for that compiler.


Well, you'd have to have a pointer to the top of the map, so that'd be 7*4 for the pointers, but of course that depends on what you formally define the 'map' to be in terms of calculating memory usage.

And there's also the fact that your memory manager will be using extra memory to maintain each of the nodes, something like 8 bytes, give or take a few multiples of 4.


thats great, thanks for your help once again...:)

[edit] (and yes it was a C++ STL map! [/edit]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.