{"id":11595,"date":"2021-06-07T16:45:05","date_gmt":"2021-06-07T16:45:05","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=11595"},"modified":"2024-02-18T14:17:28","modified_gmt":"2024-02-18T14:17:28","slug":"null-terminated-strings-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=11595","title":{"rendered":"Null-terminated strings (C++)"},"content":{"rendered":"<p>If you are working with strings you have two basic ways to define its length. Either you write a specific symbol at its end or you save\/provide its length seperatly.<\/p>\n<hr>\n<p><strong>Null-terminated strings<\/strong><br \/>\nAll old school functions who operate with strings will do that on C-strings (char array) and nearly all of them will be null-terminated.<\/p>\n<p><strong>Not null-terminated strings<\/strong><br \/>\nMany modern functions will operate on C++-Strings (std::string) instead, which contains of a not null-terminated string and its length.<\/p>\n<hr>\n<p><strong>Showcase:<\/strong> printf with not null-terminated strings<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;memory.h&gt;\r\n\r\nint main()\r\n{\r\n    char buf1[3];\r\n    memcpy(buf1, &quot;aaa&quot;, sizeof(buf1));\r\n    char buf2[3];\r\n    memcpy(buf2, &quot;bbb&quot;, sizeof(buf2));\r\n    char buf3[3];\r\n    memcpy(buf3, &quot;ccc&quot;, sizeof(buf3));\r\n\r\n    \/\/ it will write to stdout till it reaches a &#039;&#92;&#48;&#039; (0x00)\r\n    printf(&quot;%s\\n&quot;, buf1);\r\n\r\n    \/\/ but it can also work with not null-terminated strings\r\n    printf(&quot;%.*s\\n&quot;, static_cast&lt;int&gt;(sizeof(buf1)), buf1);\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<pre>\r\n$ g++ main.cpp -g\r\n$ gdb a.out\r\n(gdb) b 18\r\n(gdb) r\r\naaabbbccc\r\naaa\r\n(gdb) x\/10x &buf1\r\n0x7fffffffdcbf:\t0x61\t0x61\t0x61\t0x62\t0x62\t0x62\t0x63\t0x63\r\n0x7fffffffdcc7:\t0x63\t0x00\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are working with strings you have two basic ways to define its length. Either you write a specific<\/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],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11595"}],"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=11595"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11595\/revisions"}],"predecessor-version":[{"id":16740,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/11595\/revisions\/16740"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11595"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}