As some opening info, I'm working with Visual Studio 2013 Community. I've noticed a mismatch between what appears to be inside a std::vector
, and what is returned from sizeof, and I was wondering if anyone could shed any light on why this is how it is.
If I declare an empty vector:
std::vector<int> v;
then sizeof(v)
is 16 bytes.
4 for the ptr to the first element. _MyFirst
4 for the ptr to just after the last element. _MyLast
4 for the capacity int.
4 for the size int.
Which makes sense.. except there is another member ptr in the vector class, to the end of the vector's capacity called _MyEnd.
So why does sizeof(v)
not return 20 bytes?!
EDIT: Turns out some of my assertions were wrong :) The answers below pointed me in the right direction... just in case anyone comes across this in future and is puzzled.
The size of 16 was in debug mode, in release sizeof
returns 12.
The 12 is the 3 pointers, _MyFirst
, _MyLast
and _MyEnd
.
size
and one past the end ptr are redundant. – juanchopanza