Phelodas
Phelodas

Reputation: 4183

Size in bytes of a structure containing unordered_map

Need to find the exact size in bytes, occupied by a tree data structure that I have implemented. Node Structure is as follows

struct Node
{
    int  word;       
    int   count;       
    unordered_map<int, Node*> map;       

}node;   

What I have been doing is size(int)*2(for word and count) + map.bucket_count() * (sizeof(int) + sizeof(Node*)) and repeatedly do this for each node. Is this the correct way of doing this if i am neglecting the element overhead of storage in the unordered_map?

Also, if I am correct map.bucket_count() gives the number of buckets that are currently allocated that is including the pre- allocated ones. Should I be using map.size() instead which will ignore the pre-allocated buckets?

Or instead of all this, is it better to use tools like MemTrack to find the memory used?

Upvotes: 3

Views: 2646

Answers (1)

Fred Foo
Fred Foo

Reputation: 363567

Or instead of all this, is it better to use tools like MemTrack to find the memory used?

Yes. There's no telling from the outside how much memory an unordered_map or other complex, opaque object takes, and a good memory profiler might also show you how much overhead the memory allocator itself takes.

Upvotes: 3

Related Questions