{"id":12082,"date":"2021-09-03T09:53:57","date_gmt":"2021-09-03T09:53:57","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=12082"},"modified":"2024-02-16T11:15:56","modified_gmt":"2024-02-16T11:15:56","slug":"custom-allocator-to-track-stdvector","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=12082","title":{"rendered":"Custom allocator to track std::vector"},"content":{"rendered":"<p><strong>Source code<\/strong><\/p>\n<pre class=\"brush: cpp; title: main.cpp; notranslate\" title=\"main.cpp\">\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n\r\ntemplate&lt;typename T&gt;\r\nstruct TrackingAllocator : public std::allocator&lt;T&gt;\r\n{\r\n  template&lt;typename U&gt; struct rebind\r\n  {\r\n    typedef TrackingAllocator other;\r\n  };\r\n\r\n  T* allocate(size_t n, const void* hint = 0)\r\n  {\r\n    T* p = std::allocator&lt;T&gt;::allocate(n, hint);\r\n    std::cout &lt;&lt; &quot;Allocated &quot; &lt;&lt; n * sizeof(T)\r\n              &lt;&lt; &quot; bytes at &quot; &lt;&lt; p &lt;&lt; std::endl;\r\n    return p;\r\n  }\r\n\r\n  void deallocate(T* p, size_t n) \r\n  {\r\n    std::allocator&lt;T&gt;::deallocate(p, n);\r\n    std::cout &lt;&lt; &quot;Deallocated &quot; &lt;&lt; n * sizeof(T)\r\n              &lt;&lt; &quot; bytes at &quot; &lt;&lt; p &lt;&lt; std::endl;\r\n  }\r\n};\r\n\r\nint main()\r\n{\r\n  std::vector&lt;int, TrackingAllocator&lt;int&gt;&gt; vec = {1, 2, 3};\r\n  vec.push_back(4);\r\n  vec.push_back(5);\r\n  vec.push_back(6);\r\n  vec.push_back(7);\r\n  return 0;\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n$ g++ --version\r\ng++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609\r\n\r\n$ g++ main.cpp -std=c++11 -g\r\n<\/pre>\n<hr>\n<p><strong>Debugging<\/strong><\/p>\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n$ gdb a.out\r\n(gdb) b 32\r\n(gdb) r\r\nAllocated 12 bytes at 0x615c20\r\n\r\nBreakpoint 1, main () at main.cpp:32\r\n32\t  vec.push_back(4);\r\n(gdb) x\/3w 0x615c20\r\n0x615c20:\t1\t2\t3\r\n(gdb) n\r\nAllocated 24 bytes at 0x616050\r\nDeallocated 12 bytes at 0x615c20\r\n33\t  vec.push_back(5);\r\n(gdb) x\/6w 0x616050\r\n0x616050:\t1\t2\t3\t4\r\n0x616060:\t0\t0\r\n(gdb) n\r\n34\t  vec.push_back(6);\r\n(gdb) x\/6w 0x616050\r\n0x616050:\t1\t2\t3\t4\r\n0x616060:\t5\t0\r\n(gdb) n\r\n35\t  vec.push_back(7);\r\n(gdb) x\/6w 0x616050\r\n0x616050:\t1\t2\t3\t4\r\n0x616060:\t5\t6\r\n(gdb) n\r\nAllocated 48 bytes at 0x616070\r\nDeallocated 24 bytes at 0x616050\r\n36\t  return 0;\r\n(gdb) x\/12w 0x616070\r\n0x616070:\t1\t2\t3\t4\r\n0x616080:\t5\t6\t7\t0\r\n0x616090:\t0\t0\t0\t0\r\n<\/pre>\n<hr>\n<p><strong>Result<\/strong><\/p>\n<p>As soon as the std::vector runs out of memory its default allocator (std::allocator&lt;T&gt;) allocates a new chunk of memory which provides twice as much memory as the last one and deallocates the old one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source code Debugging Result As soon as the std::vector runs out of memory its default allocator (std::allocator&lt;T&gt;) allocates a new<\/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":[28],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12082"}],"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=12082"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12082\/revisions"}],"predecessor-version":[{"id":16727,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/12082\/revisions\/16727"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12082"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}