Reputation: 119
I was messing around to try and figure out how std::vector
manages the size of its underlying array. I know it does some sort of resizing such as, when a call is made to try and add an element when the underlying array is full, create a new array that is twice the size of the previous one and copy the old values into it (or something like that).
So I just did
#include <iostream>
#include <vector>
int main()
{
std::vector<int> myVec;
for (int i = 1; i < 10000; ++i)
{
myVec.push_back(i);
std::cout << i << " elements in vector; size of vector is " << sizeof(myVec) << " bytes." << std::endl;
}
return 0;
}
which was printing out
1 elements in vector; size of vector is 28 bytes.
2 elements in vector; size of vector is 28 bytes.
3 elements in vector; size of vector is 28 bytes.
4 elements in vector; size of vector is 28 bytes.
5 elements in vector; size of vector is 28 bytes.
6 elements in vector; size of vector is 28 bytes.
7 elements in vector; size of vector is 28 bytes.
8 elements in vector; size of vector is 28 bytes.
9 elements in vector; size of vector is 28 bytes.
10 elements in vector; size of vector is 28 bytes.
11 elements in vector; size of vector is 28 bytes.
12 elements in vector; size of vector is 28 bytes.
13 elements in vector; size of vector is 28 bytes.
14 elements in vector; size of vector is 28 bytes.
15 elements in vector; size of vector is 28 bytes.
16 elements in vector; size of vector is 28 bytes.
17 elements in vector; size of vector is 28 bytes.
18 elements in vector; size of vector is 28 bytes.
19 elements in vector; size of vector is 28 bytes.
20 elements in vector; size of vector is 28 bytes.
21 elements in vector; size of vector is 28 bytes.
22 elements in vector; size of vector is 28 bytes.
23 elements in vector; size of vector is 28 bytes.
24 elements in vector; size of vector is 28 bytes.
25 elements in vector; size of vector is 28 bytes.
26 elements in vector; size of vector is 28 bytes.
27 elements in vector; size of vector is 28 bytes.
28 elements in vector; size of vector is 28 bytes.
29 elements in vector; size of vector is 28 bytes.
30 elements in vector; size of vector is 28 bytes.
31 elements in vector; size of vector is 28 bytes.
32 elements in vector; size of vector is 28 bytes.
etcetera
I realize that's because I'm looking at the size of the vector, which contains a pointer to the underlying array, thus the size of the vector does not include the size of the underlying array. Given that I don't see anywhere that I can directly access the underlying array (http://www.cplusplus.com/reference/vector/vector/), how can do I what I was trying to do? All I really want to do is see how the underlying array periodically "jumps" in size as elements are added 1-by-1.
Upvotes: 0
Views: 97
Reputation: 57129
When you push a value to the vector, its size is increased by one. When the size reaches the vector capacity, more memory is allocated and the capacity is doubled.
You can check the capacity by calling myVec.capacity()
. So in order to find out how much memory was allocated, you can do something like:
myVec.capacity() * sizeof(int)
Upvotes: 2