Reputation: 2839
Right after I declare a vector<int>
and a deque<int>
if I print out sizeof
on both of them, the std::vector
has 12 bytes ( I guess begin, end and size ) while deque
has 40 bytes. Where do those extra bytes come from?
I'm using Code::Blocks IDE 13.12
and I did select the C++11
standard to be used.
Upvotes: 2
Views: 875
Reputation: 254771
The size is implementation dependent; but it's not surprising that the control structure for a more complicated structure like deque
might be larger than a simple one like vector
(which, as you say, can be easily managed by three pointers, or a pointer and two sizes).
Looking at the GNU implementation, it stores precalculated "start" and "end" iterators, presumably because they're difficult to calculate on demand, and easier to update when the container changes. Each of those is quite complicated, containing four pointers: the current position, the start and end of the current block, and a pointer to a map structure needed to move between blocks. The deque
also has a (possibly redundant?) pointer to that map, and the size (again, presumably difficult to calculate on demand), giving a total of ten pointers/sizes, as you observed.
Upvotes: 2
Reputation: 234885
sizeof does not give you the number of elements in a container; rather the compile time evaluated size of the structure.
So the actual value is down to the implementation of the standard library and, as such, can vary from platform to platform.
Upvotes: 0
Reputation: 409482
The sizeof
operator takes the size of the object and not the size of its contents.
So if an object have three data member variables, the sizeof
operator will get the size of those member variables, but if one of them is a pointer to more data then you only get the size of the pointer and not the length of the data it points to.
Upvotes: 1