get memory size for STL containers

R

Rares Vernica

Hello,

How can I determine how much memory is used by various STL containers?

For example, I have a vector<int>, I know that an int takes 4B and I
have 10 elements in my vector. So, I know that that my vector will take
at least 40B, but I would like to know more exactly. I would like to
know how much more does the vector needs for its stuff.

I also need to know the memory size for set and map (unordered_map).

Thanks,
Ray
 
A

Alf P. Steinbach

* Rares Vernica:
Hello,

How can I determine how much memory is used by various STL containers?

For example, I have a vector<int>, I know that an int takes 4B and I
have 10 elements in my vector. So, I know that that my vector will take
at least 40B, but I would like to know more exactly. I would like to
know how much more does the vector needs for its stuff.

capacity() gives you a pretty good indication (multiply by element size
and add a few bytes overhead).

I also need to know the memory size for set and map (unordered_map).

More difficult and non-portable.

If it turns out to be a problem you can always hammer away with the
nearest sledge-hammer, uh, debugging tools.


Cheers, & hth.,

- Alf
 
P

Pete Becker

How can I determine how much memory is used by various STL containers?

For example, I have a vector<int>, I know that an int takes 4B and I
have 10 elements in my vector. So, I know that that my vector will take
at least 40B, but I would like to know more exactly. I would like to
know how much more does the vector needs for its stuff.

I also need to know the memory size for set and map (unordered_map).

Write your own allocator to track memory usage.
 
R

Rares Vernica

Pete said:
Write your own allocator to track memory usage.

If this is the way to go in order to find the size of the of an STL
container, then, did anyone created such an allocator? Did anyone
created an allocator that acts as the default one, but also keeps track
of the number of allocated bytes? I assume this can also be done by
starting from the default allocator and adding some counters.

Thanks,
Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top