#include <iostream>
#include <vector>
int main(int argc, char const *argv[])
{
std::vector<int> a(32768, 0);
std::cout << "Size " << sizeof a << "\nCapacity " << a.capacity() << "\nElements " << a.size();
return 0;
}
for this program im getting the output:
Size 24
Capacity 32768
Elements 32768
using valgrind i calculated heap usage which is:
132096 bytes
that is (32768 x 4 bytes) + 24 bytes
im interested in how are these 24 bytes used by vector a
As addressed in the comments by Kamil, a std::vector
keeps track of three pointers internally. One pointer to the begin, one to end and one to the end of allocated memory (see stack post). Now, the size of a pointer should be 8 bytes on any 64-bit C/C++ compiler so, 3 * 8 bytes = 24 bytes (see wiki).