Sign in to follow this  
grill8

[.net] Simple question about stl containers.

Recommended Posts

Hello, I have a simple question about C++ stl containers. My question is: How much memory is used by an empty stl container (specifically an std::map<const char*, T> ? There are no objects in the map so there is no memory used by key/value pairs (I assume). But the container itself is stored somewhere and is thus taking up some finite amount of memory. I am just wondering how much memory the empty container is using. Thank you for your help. Jeremy

Share this post


Link to post
Share on other sites
Why is this in .Net? Is this a question about a deriative of C++ like C++/CLI or C++ .Net? In native C++ the actual size depends on the implementation, but in most cases almost no memory is used. In VC++ 8.0 std::map<const char*, int> is 28 bytes in debug mode and 12 bytes in release mode. Of course this might change with other key/value types, but with all the combinations I tried the size was the same, if you want to be completely sure you need to check the source of map, then you can also account for dynamically allocated memory (this won't be a pleasant experience).

You can get the size on your on compiler by calling: sizeof(std::map<T1,T2>) where T1 and T2 is the types of the key and value.

Share this post


Link to post
Share on other sites
The container takes up at least sizeof(type) bytes, and may actually use more than that internally (through dynamic allocation).

These values depend on your STL implementation, and possibly also on whether you're in debug or release mode.

Share this post


Link to post
Share on other sites
Further to CTar's answer


std::map<const char*,int> myMap;
int size = sizeof(myMap);
//size = 16

std::map<const char*,double> myMap;
int size = sizeof(myMap);
//size = 16

std::map<const char*,short> myMap;
int size = sizeof(myMap);
//size = 16

std::map<const char*,char> myMap;
int size = sizeof(myMap);
//size = 16

//for the structure
struct myStruct
{
int a;
int b;
double c;
};

std::map<const char*,myStruct> myMap;
int size = sizeof(myMap);
//size = 16



This was tested in VC6

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this