{"id":17259,"date":"2024-03-27T13:07:46","date_gmt":"2024-03-27T13:07:46","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=17259"},"modified":"2024-03-28T09:23:42","modified_gmt":"2024-03-28T09:23:42","slug":"layout-of-stdstring-libstdc","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=17259","title":{"rendered":"Layout of std::string (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;string&gt;\r\n\r\nint main()\r\n{\r\n    std::string s = &quot;Hello World&quot;;\r\n    const char* cs = s.c_str();\r\n    return 0;\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Analysis<\/strong><\/p>\n<pre>\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 7\r\n(gdb) r\r\n(gdb) disable pretty-printer\r\n(gdb) p \/x s\r\n$1 = {\r\n    _M_dataplus = {..., _M_p = 0x7fffffffde30},\r\n    _M_string_length = 0xb,\r\n    {_M_local_buf = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f,\r\n                     0x72, 0x6c, 0x64, 0x0, 0x0, 0x0, 0x0, 0x0},\r\n     _M_allocated_capacity = 0x6f57206f6c6c6548}}\r\n(gdb) p sizeof(s)\r\n$2 = 32\r\n(gdb) x\/4xg &s\r\n0x7fffffffde20:\t0x00007fffffffde30\t0x000000000000000b\r\n0x7fffffffde30:\t0x6f57206f6c6c6548\t0x0000000000646c72\r\n(gdb) x\/1s s._M_dataplus._M_p\r\n0x7fffffffde30:\t\"Hello World\"\r\n(gdb) x\/1s cs\r\n0x7fffffffde30:\t\"Hello World\"\r\n<\/pre>\n<p>Here the payload is on the stack (small string optimization), but if &#8216;_M_string_length&#8217; is greater than 15 it will be on the heap. (Source: <a href=\"https:\/\/github.com\/gcc-mirror\/gcc\/blob\/c891d8dc23e1a46ad9f3e757d09e57b500d40044\/libstdc%2B%2B-v3\/include\/bits\/basic_string.h#L202\">gcc\/libstdc++-v3\/include\/bits\/basic_string.h<\/a>)<\/p>\n<p>The std::string payload is null-terminated (&#8216;\\0&#8217;). This seems to be useless, cause std::string stores the length, but it&#8217;s done to provide a null-terminated const char* via its method c_str().<\/p>\n<hr>\n<p><strong>Size comparison<\/strong><\/p>\n<table>\n<tr>\n<th>char*<\/th>\n<th>std::string (len \u2264 15)<\/th>\n<th>std::string (len > 15)<\/th>\n<\/tr>\n<tr valign=\"top\">\n<td>Raw Ptr (to first char)<sup>1<\/sup><br \/>\n        + Heap buffer (payload)<br \/>\n        = 4 Byte + Payload<\/td>\n<td>Raw Ptr (to first char)<sup>1<\/sup><br \/>\n        + Length (of payload)<sup>2<\/sup><br \/>\n        + Stack buffer (payload)<sup>3<\/sup><br \/>\n        = 32 Byte<\/td>\n<td>Raw Ptr (to first char)<sup>1<\/sup><br \/>\n        + Length (of payload)<sup>2<\/sup><br \/>\n        + Stack buffer (capacity)<sup>3<\/sup><br \/>\n        + Heap buffer (payload)<br \/>\n        = 32 Byte + Payload<\/td>\n<\/tr>\n<\/table>\n<p>On 64 Bit: <sup>1<\/sup> 8 Byte, <sup>2<\/sup> 8 Byte, <sup>3<\/sup> 16 Byte<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source code Analysis $ g++ &#8211;version g++ (Ubuntu 13.2.0-4ubuntu3) 13.2.0 $ g++ main.cpp -g $ gdb a.out (gdb) b 7<\/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\/17259"}],"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=17259"}],"version-history":[{"count":42,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17259\/revisions"}],"predecessor-version":[{"id":17304,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/17259\/revisions\/17304"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17259"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}