Any way to find the size of memory allocated for a card?

Is there any method for determining the amount / size of memory allocated for a card in C ++? There is a function to find the size of the map, i.e. The number of entries on the map, but is there any such method for memory. I have a map (line, line). Sizeof () always gives me size 48. What is the reason why this is so? Thanks:)

+6
source share
4 answers

No no. However, you can achieve something similar for classes that support the .size method, such as strings or a standard container:

 template <class Key, class Value> unsigned long mapSize(const std::map<Key,Value> &map){ unsigned long size = sizeof(map); for(typename std::map<Key,Value>::const_iterator it = map.begin(); it != map.end(); ++it){ size += it->first.size(); size += it->second.size(); } return size; } 

If you want to know the allocated memory, you can use .capacity :

 template <class Key, class Value> unsigned long mapCapacity(const std::map<Key,Value> &map){ unsigned long cap = sizeof(map); for(typename std::map<Key,Value>::const_iterator it = map.begin(); it != map.end(); ++it){ cap += it->first.capacity(); cap += it->second.capacity(); } return cap; } 
+2
source

There is no easy way, but if you really need to know (though ... why do you need it?), Then you can find out.

All standard library containers are allocated by default using the "default allocator", which is not much more than a structure / class with a couple of wrapping functions around new and delete (which are internal themselves, a little more than wrappers around malloc and free with a little alignment and a type different from many compilers).

If for some reason you are unhappy with the default dispenser, you can provide a custom dispenser for the container template and it will just use this one.

If you write a allocator that increases / decreases an integer when allocating / freeing, you know how much memory has been dynamically allocated. Add to this the sizeof value for ultra-precise accuracy.

+4
source

The size of the map class is 48. An instance for the map will be created on the stack and all the records that we insert will be saved on the heap. That way, the map object will simply point to entries on the heap. Confidence may be like why 48 even after inserting records.? Since the records are not saved with the map object, the size is constant - 48. As already mentioned in the answers, the size of the object will not change at run time.

Total size used map =

 ((sizeof(key)+sizeof(value))* map.size())+sizeof(map) 

map.size () will give the number of entries on the map

48 - the size of the card instance.

+1
source

An object cannot resize at runtime. For the card, as for most std containers, memory is allocated dynamically under the hood. To find the total size that the card occupies and controls , you can:

 std::map<X,Y> mymap; int totalSize = sizeof(mymap); int noElements = 0; for ( std::map<X,Y>::iterator i = mymap.begin() ; i != mymap.end() ; i++ ) noElements++; totalSize += noElements * sizeof(X); totalSize += noElements * sizeof(Y); 
0
source

Source: https://habr.com/ru/post/920783/


All Articles