Source code
#include <vector> int main() { std::vector<int> vec = { 1, 2, 3, 4, 5 }; return 0; }
Analysis
$ g++ --version g++ (Ubuntu 13.2.0-4ubuntu3) 13.2.0 $ g++ main.cpp -g $ gdb a.out (gdb) b 6 (gdb) r (gdb) disable pretty-printer 163 printers disabled 0 of 163 printers enabled (gdb) p vec $1 = {... {_M_impl = {... _M_start = 0x55555556b2b0, _M_finish = 0x55555556b2c4, _M_end_of_storage = 0x55555556b2c4}}, ...} (gdb) p vec->_M_impl._M_finish - vec->_M_impl._M_start $2 = 5 (gdb) p sizeof(vec) $3 = 24 (gdb) x/3xg &vec 0x7fffffffde10: 0x000055555556b2b0 0x000055555556b2c4 0x7fffffffde20: 0x000055555556b2c4 (gdb) x/5xw vec->_M_impl._M_start 0x55555556b2b0: 0x00000001 0x00000002 0x00000003 0x00000004 0x55555556b2c0: 0x00000005
Layout
|--------------------------------| | vector vec | | 1 | 2 | 3 | 4 | 5 | |--------------------------------| ↑ ↑ | Ptr in front of the first item |-----------| | | | | | Ptr behind the last item |-------------------------------| | | | | Ptr behind the reserved memory |-------------------------------| |--------------------------------|
Stack (Frame 0) ------------------ 0x7fffffffde20: 0x000055555556b2c4 (Ptr behind the reserved memory) 0x7fffffffde18: 0x000055555556b2c4 (Ptr behind the last item) vec 0x7fffffffde10: 0x000055555556b2b0 (Ptr in front of the first item) ------------------ ↓ ↑ Heap ---------- 0x000055555556b2c0: 0x00000005 (item[4]) 0x000055555556b2bc: 0x00000004 (item[3]) 0x000055555556b2b8: 0x00000003 (item[2]) 0x000055555556b2b4: 0x00000002 (item[1]) 0x000055555556b2b0: 0x00000001 (item[0]) ----------