Boost logo

Boost Users :

Subject: Re: [Boost-users] Help in calculating the size of a boost::unordered_map
From: Juan Ramírez (jramirez.uy_at_[hidden])
Date: 2016-11-07 09:19:07


What you are trying to obtain is not easy, I must say.

The static size might be solved by using sizeof, but the size of the
allocated data will be more complicated (and less performant) to compute.

Suppose you have an unordered_map of string to integers.

    boost::unordere_map<string, int> my_map_;

The size int is known as sizeof(int), so it seems logical to multiply this
value by the size of the container in order to get the actual size of the
containers IN TERMS OF THE VALUE.

   size_t size_of_val = sizeof(int) * my_map_.size();

Now, what if the container does not contain integers, what if it contains a
class? And what if that class uses dynamic allocation (std::string for
example). You cannot know the size of each element in the container unless
you visit them all!!

The reason above applies for the key elements, the key must be inside the
container, but keys have dynamic data so they all have potentially
different sizes!

So, my advice would be to:

1- Compute the static size of the unordered_map using size_of
2- Sum up the size of each element (key + value) in the map

Remarks: I don't know the internals of boost::unordered_map, if the class
allocates data in the heap you are out of luck!

Best regards
Juan

On Mon, Nov 7, 2016 at 8:58 AM, Ram <sourceopen_at_[hidden]> wrote:

> Hi,
>
> Can anybody please help with this?
>
> Thanks,
> -R
>
> On Mon, Nov 7, 2016 at 11:00 AM, Ram <sourceopen_at_[hidden]> wrote:
>
>> Hi,
>>
>> I would like to calculate the size of my filled up boost::unordered_map
>> including the house keeping. I was thinking of sizeof(boost:unordered_map
>> mymap). I asked this question on stackoverflow but they say that wont help.
>> Can somebody tell me an easy way to find the size including the
>> housekeeping that it takes?
>>
>> The boost::unordered_map stores a std::string as the key and a pointer to
>> my class object as the value.
>>
>> Thanks,
>> Ram
>>
>>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
Juan
:wq


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net