{"id":521,"date":"2012-10-18T08:07:10","date_gmt":"2012-10-18T08:07:10","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=521"},"modified":"2020-03-26T12:15:25","modified_gmt":"2020-03-26T12:15:25","slug":"c-strings-general","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/c-strings-general","title":{"rendered":".C++ Strings General"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Header Files<\/h4>\n\n\n\n<p><em>In C++, strings are not a built-in data type, but rather a Standard Library facility.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string>\n\/\/#include &lt;string.h>   \/\/(.h is used for C not C++)\n#include &lt;cstring>\nusing namespace std;    \/\/Or use std::string;\n#include &lt;sstream>    \/\/Lets you create strings using &lt;&lt; for instance<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Memory Allocation For Strings<\/h4>\n\n\n\n<p>Its handled automatically &#8211; you don&#8217;t have to specify a size when creating a string. &nbsp;The string class functions simply&nbsp;compute&nbsp;the size every time they&nbsp;need&nbsp;to allocate memory, at a runtime cost (by, for example, using strlen when needed). &nbsp;This is roughly what it does:<\/p>\n\n\n\n<p>Compute the size of the input<br>Compute the new size the string should have after reading<br>Allocate memory (depending on new size), if required<br>Copy data in new memory<br>Return old memory to the heap&nbsp;and now use the new memory instead<\/p>\n\n\n\n<p>So&nbsp;very handy, BUT, at the expense of speed of course. &nbsp;If you wanted to work on a string as fast as possible creating a char array with as big a size as you will ever need&nbsp;and the&nbsp;working on the individual&nbsp;char&#8217;s would be fastest.<\/p>\n\n\n\n<p>Here&#8217;s a few useful examples of how things work:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Constant strings are always there, no need to worry about them being destroyed or&nbsp;destroying&nbsp;them:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const char* func1() {\n   const char* s = \"Hello World\";\n   return s;\n}\nconst char*  s1 = func1();\ndelete s1; \/\/?<\/code><\/pre>\n\n\n\n<p>You can&#8217;t delete s1, as the string it points to does not live on the heap. &nbsp;<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">You can pass a ram memory&nbsp;string back from a function call:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>string func2() {\n   string s = \"Hello world\";\n   return s;\n}\nstring s2 = func2();<\/code><\/pre>\n\n\n\n<p>This is fine. func2&#8217;s s goes out of scope and cleans up. s2 will duplicate the string from s, and also clean itself up at the end of function it is used in.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">You can&#8217;t return a pointer to a string that will get destroyed:<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>const char* func3() {\n   string s = \"this is a literal string\";\n   return s.c_str();\n}\nconst char*  s3 = func3();\ndelete s3;<\/code><\/pre>\n\n\n\n<p>func3 returns a pointer to a string that will be&nbsp;freed&nbsp;when the called&nbsp;function exits.&nbsp;You will get an&nbsp;exception upon execution of delete s3.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Deleting Strings<\/h4>\n\n\n\n<p>You don&#8217;t have to. When the string goes out of scope, it&#8217;s destructor will be called automatically and the memory will be freed.<\/p>\n\n\n\n<p>If you want to clear the string right now (without waiting until it goes out of scope) just use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mystringname.clear().<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">string&nbsp;or&nbsp;wstring?<\/h4>\n\n\n\n<p>std::string is a basic_string templated on a char, and std::wstring on a wchar_t.<\/p>\n\n\n\n<p>char holds&nbsp;a character, usually a 1-byte character.<\/p>\n\n\n\n<p>wchar_t holds a wide character, but its different across systems:<\/p>\n\n\n\n<p>On Linux, a wchar_t is 4-bytes. &nbsp;Holds&nbsp;all the ASCII character set including special characters.<\/p>\n\n\n\n<p>On Windows&nbsp;it&#8217;s 2-bytes. &nbsp;Only special characters are available for the current locale of the Windows user.<\/p>\n\n\n\n<p>Unicode<\/p>\n\n\n\n<p>Sort of &#8211; the&nbsp;problem is that wchar_t is not directly tied to unicode. There&#8217;s a good explanation in an answer <a href=\"http:\/\/stackoverflow.com\/questions\/402283\/stdwstring-vs-stdstring\">here<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">&nbsp;<\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Header Files In C++, strings are not a built-in data type, but rather a Standard Library facility. Memory Allocation For Strings Its handled automatically &#8211; you don&#8217;t have to specify a size when creating a string. &nbsp;The string class functions simply&nbsp;compute&nbsp;the size every time they&nbsp;need&nbsp;to allocate memory, at a runtime cost (by, for example, using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97],"tags":[],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-string-class"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=521"}],"version-history":[{"count":36,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":3161,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/521\/revisions\/3161"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}