0
What is stored on stack for vector
Refer code below: https://www.sololearn.com/compiler-playground/cNjF4miFfycg I understand that vector object is stored on stack but its elements are allocated on heap. This is replicated by above example and we can see that 220 bytes allocated on heap (55 elements * 4 size for each). My query is related to sizeof. Why I am getting 24 and what all is stored into these 24 bytes?
3 Respostas
+ 4
In the case of gcc at least, those 24 bytes are 3 pointers.
1 pointing to the start of the container, 1 to the last element and 1 that points to the end of the container.
+ 2
It's the same in the case of clang, i.e. it stores 3 pointers for the start of container, last element and end of container.
The standard doesn't specify anything about what the vector stores, it only specifies the functionality it provides.
A vector generally needs only 3 things - a pointer to the memory buffer in the heap, the size and the capacity (the latter two can also be calculated using pointers to the last element and the end of the container respectively)
+ 1
I cannot open the URL