What is stored on stack for vector | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
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?

23rd Nov 2022, 1:35 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 Antworten
+ 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.
23rd Nov 2022, 2:06 PM
Dennis
Dennis - avatar
+ 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)
24th Nov 2022, 5:07 AM
XXX
XXX - avatar
+ 1
I cannot open the URL
23rd Nov 2022, 1:56 PM
Giannis
Giannis - avatar