Reputation: 1
class A {};
class B {};
// Both the classes are properly defined.
struct MyStruct{
typedef std::vector< std::pair< const A*, const B* > > TVec;
std::map< std::string, TVec > d_data;
};
Now i have a 2D array of 10X10 where each element of my 2D array is of type struct MyStruct
.
Now i have 100 elements to add in this 2D array.
If each element is inserted at unique position in array, in that case, my memory footprint is, let say 'X' MB.
each 2D array element contains exactly one MyStruct
element stored.
But if I add all the 100 elements of type MyStruct
at last position i.e. 2DArr[9][9]
, my memory footprint is, let say 'Y' MB.
Assume that map size is 1 i.e. there is only 1 key and corresponding to that key, multiple values are stored in a vector.
What i see is, 'Y' is close to 4 times greater then 'X'.
Why it is as at the end, I am inserting same number of elements i.e. 100.
MyStruct
elements in the 2D array but at different locations in both the cases.
Upvotes: 0
Views: 63
Reputation: 10880
Vector normally doesn't reallocate at each insert: that'd be slow. Usually there's an exponential allocation strategy: vector memory allocation strategy
Upvotes: -2