A
A
askogorev2013-11-29 10:01:10
C++ / C#
askogorev, 2013-11-29 10:01:10

Sizeof(std::vector) =?

In std the value of this function ( for x32 ) sizeof(vector<int>)= 20
Question - why?
My guesses:
4 bytes - a pointer to the beginning of the array
4 bytes - a pointer to the position where the next element will be written (well, that is, fullness)
4 bytes - a pointer to the end of the allocated memory
Total 12 bytes. Where are the 8 more? I suspect there is still a pointer to the allocator, but it's still 4 bytes
. Where am I wrong and what is missing?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vbor, 2013-11-29
@vbor

+_Vector_val::_Container_base::_Container_proxy _Myproxy;
+_Vector_val::_Alty _Alval;

A
AxisPod, 2013-11-29
@AxisPod

Well, firstly, the vector allocates memory with a margin. So most likely he still needs to store the number of reserved elements (total) and the number of added elements. In general, open the debugger and take a look.

B
bak, 2013-11-30
@bak

It depends on the stl implementation. In the stl bundled with gcc-4.6 on a 32-bit platform, sizeof(vector) is 12, three 4-byte pointers:

typename _Tp_alloc_type::pointer _M_start;
typename _Tp_alloc_type::pointer _M_finish;
typename _Tp_alloc_type::pointer _M_end_of_storage;

_M_start - the beginning of the allocated memory
_M_finish - the last inserted element
_M_end_of_storage - the end of the allocated memory
In stl from visual studio, an allocator is also stored on the stack.
allocator_type _M_data_allocator;
_Tp* _M_start;
_Tp* _M_finish;
_Tp* _M_end_of_storage;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question