4

I am using std::unordered_map and was trying to get the memory consumption so I did the following:

#include <iostream>
#include <unordered_map>

using namespace std;

int main(){
    unordered_map<int, int> map;
    map[21] = 12;

    cout << map.size() << endl << map.max_size() << endl;

    return 0;
}

to which the result is:

1
1152921504606846975

the first one is correct, what the heck is the second one?!

mmirzadeh
  • 6,893
  • 8
  • 36
  • 47

4 Answers4

4
map.max_size() 

Returns the maximum potential size the container can reach due to system or library implementation limitations.


map.size() 

Returns the number of elements in the container.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Oh, i see. So this is not some buffer size? So then in this case is the memory consumption equal to `map.size() * 2 * sizeof(int)` or is there some "hidden" memory usage? – mmirzadeh Mar 01 '12 at 08:22
  • 1
    @GradGuy: There *might* be some overhead due to memory needed by the container itself. Have a look [here](http://stackoverflow.com/questions/720507/how-can-i-estimate-memory-usage-of-stlmap) – Alok Save Mar 01 '12 at 08:25
2

Check cppreference: link

map::max_size

Return maximum size

Returns the maximum number of elements that the map container object can hold.

This is the maximum potential size the container can reach due to system or library implementation limitations.

neciu
  • 4,373
  • 2
  • 24
  • 33
1

max_size()

Returns the maximum number of elements that the map container object can hold.

Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
1

It's an upper bound on the maximum number of elements the container could potentially hold.

NPE
  • 486,780
  • 108
  • 951
  • 1,012