{"id":4983,"date":"2020-01-23T12:33:25","date_gmt":"2020-01-23T12:33:25","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=4983"},"modified":"2024-03-20T15:18:11","modified_gmt":"2024-03-20T15:18:11","slug":"memory-management-of-vectors-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=4983","title":{"rendered":"Layout of std::vector (libstdc++)"},"content":{"rendered":"<p><strong>Source code<\/strong><\/p>\n<pre class=\"brush: cpp; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &lt;vector&gt;\r\n\r\nint main()\r\n{\r\n  std::vector&lt;int&gt; vec = { 1, 2, 3, 4, 5 };\r\n  return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Analysis<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n$ g++ --version\r\ng++ (Ubuntu 13.2.0-4ubuntu3) 13.2.0\r\n$ g++ main.cpp -g\r\n$ gdb a.out\r\n(gdb) b 6\r\n(gdb) r\r\n(gdb) disable pretty-printer\r\n163 printers disabled\r\n0 of 163 printers enabled\r\n(gdb) p vec\r\n$1 = {...\r\n    {_M_impl = {...\r\n        _M_start = 0x55555556b2b0,\r\n        _M_finish = 0x55555556b2c4,\r\n        _M_end_of_storage = 0x55555556b2c4}}, ...}\r\n(gdb) p vec-&gt;_M_impl._M_finish - vec-&gt;_M_impl._M_start\r\n$2 = 5\r\n(gdb) p sizeof(vec)\r\n$3 = 24\r\n(gdb) x\/3xg &amp;vec\r\n0x7fffffffde10:\t0x000055555556b2b0 0x000055555556b2c4\r\n0x7fffffffde20:\t0x000055555556b2c4\r\n(gdb) x\/5xw vec-&gt;_M_impl._M_start\r\n0x55555556b2b0:\t0x00000001 0x00000002 0x00000003 0x00000004\r\n0x55555556b2c0:\t0x00000005\r\n<\/pre>\n<hr>\n<p><strong>Layout<\/strong><\/p>\n<pre>\r\n|--------------------------------|\r\n|           vector vec           |           | 1 | 2 | 3 | 4 | 5 |\r\n|--------------------------------|           \u2191                   \u2191\r\n| Ptr in front of the first item |-----------|                   |\r\n|                                |                               |\r\n| Ptr behind the last item       |-------------------------------|\r\n|                                |                               |\r\n| Ptr behind the reserved memory |-------------------------------|\r\n|--------------------------------|\r\n<\/pre>\n<pre>\r\n                       Stack (Frame 0)\r\n                    ------------------\r\n    0x7fffffffde20: 0x000055555556b2c4 (Ptr behind the reserved memory)\r\n    0x7fffffffde18: 0x000055555556b2c4 (Ptr behind the last item)\r\nvec 0x7fffffffde10: 0x000055555556b2b0 (Ptr in front of the first item)\r\n                    ------------------\r\n                             \u2193\r\n\r\n                             \u2191\r\n                           Heap\r\n                        ----------\r\n    0x000055555556b2c0: 0x00000005 (item[4])\r\n    0x000055555556b2bc: 0x00000004 (item[3])\r\n    0x000055555556b2b8: 0x00000003 (item[2])\r\n    0x000055555556b2b4: 0x00000002 (item[1])\r\n    0x000055555556b2b0: 0x00000001 (item[0])\r\n                        ----------\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source code Analysis Layout |&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;| | vector vec | | 1 | 2 | 3 | 4 | 5 |<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[80,28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4983"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4983"}],"version-history":[{"count":7,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4983\/revisions"}],"predecessor-version":[{"id":17204,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4983\/revisions\/17204"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4983"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}