{"id":56,"date":"2007-08-17T07:57:10","date_gmt":"2007-08-17T11:57:10","guid":{"rendered":"http:\/\/www.jrtwine.com\/blog\/?p=56"},"modified":"2007-09-17T11:21:21","modified_gmt":"2007-09-17T15:21:21","slug":"cstring-misuse-1","status":"publish","type":"post","link":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/","title":{"rendered":"CString misuse #1"},"content":{"rendered":"<p>This is the first of many examples of ways to misuse and\/or abuse MFC&#8217;s <strong>CString<\/strong> class.  While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not).  Here is the offending code:<\/p>\n<p><code>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nCString str( \"First Part Of Message\\n\" );<br \/>\nstr = str +  \"Second Part Of Message\\n\";<br \/>\nstr = str +  \"Third Part Of Message\";<\/p>\n<p>MessageBox( str );<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/code><\/p>\n<p>If you write code like this, stop now and back slowly away from the keyboard &#8211; <strong><em>You&#8217;re Doing It Wrong!<\/em><\/strong><\/p>\n<p>First, the developer is adding (concatenating) strings together, but these are static\/constant strings!  They always add up to the same string, and as such can be made into a single constant string:<\/p>\n<p><code>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n\"First Part Of Message\\nSecond Part Of Message\\nThird Part Of Message\"<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/code><\/p>\n<p>So at a minimum, the start of the code should read:<\/p>\n<p><code>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nCString str( \"First Part Of Message\\nSecond Part Of Message\\nThird Part Of Message\" );<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/code><\/p>\n<p>Why not add up the strings separately like the original code did?  Two reasons &#8211; overhead and exception opportunity.  Each use of <strong>CString::operator+(&#8230;)<\/strong> can result in dynamic memory operations (allocation and deallocation).  So you are looking at six potential heap operations (three potential allocations and deallocations including destruction, although in release builds of <strong>CString<\/strong>, the number of operations is less).  Each operation has the potential to raise an exception and in the absence of per-thread heaps, can effectively bottleneck a multi-threaded application to the performance of a single-threaded one because the heap operations have to be serialized.<\/p>\n<p>So by manually putting the strings together we have reduced heap operations from 6 to 2 &#8211; one allocation and one deallocation.  That is a pretty good improvement, but we can do better!<\/p>\n<p>The <strong>MessageBox(&#8230;)<\/strong> function does not take <strong>CString<\/strong>s, it takes pointers to constant strings (<strong>LPCTSTR<\/strong>).  So why is a CString needed here at all?  <\/p>\n<p><code>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\nMessageBox( \"First Part Of Message\\nSecond Part Of Message\\nThird Part Of Message\" );<br \/>\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/code><\/p>\n<p>This final version of the code is simpler, will execute faster, and is more robust.  Sounds like a winner to me!<\/p>\n<p>Note: Some of you may be thinking about the preprocessor&#8217;s ability to automatically concatenate static strings.  Yes it does, but it cannot automatically coalesce the above strings because they are separate &#8211; they are being passed (separately) as parameters to a function.  If the <strong>+<\/strong> operator was not present in-between the parts of the string, they would be coalesced to a single string, but the unnecessary <strong>CString<\/strong> would still be there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first of many examples of ways to misuse and\/or abuse MFC&#8217;s CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &#8220;First Part Of Message\\n&#8221; ); str = str + &#8220;Second Part [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,19],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-developing","category-youre-doing-it-wrong"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"This is the first of many examples of ways to misuse and\/or abuse MFC&#039;s CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &quot;First Part Of Message\\n&quot; ); str = str + &quot;Second Part\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"James R. Twine\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"James R Twine Blog -\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"CString misuse #1 - James R Twine Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"This is the first of many examples of ways to misuse and\/or abuse MFC&#039;s CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &quot;First Part Of Message\\n&quot; ); str = str + &quot;Second Part\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2007-08-17T11:57:10+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2007-09-17T15:21:21+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"CString misuse #1 - James R Twine Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"This is the first of many examples of ways to misuse and\/or abuse MFC&#039;s CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &quot;First Part Of Message\\n&quot; ); str = str + &quot;Second Part\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#blogposting\",\"name\":\"CString misuse #1 - James R Twine Blog\",\"headline\":\"CString misuse #1\",\"author\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/author\\\/jtwine\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#organization\"},\"datePublished\":\"2007-08-17T07:57:10+00:00\",\"dateModified\":\"2007-09-17T11:21:21+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#webpage\"},\"articleSection\":\"Developing, You're Doing It Wrong\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/category\\\/developing\\\/#listItem\",\"name\":\"Developing\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/category\\\/developing\\\/#listItem\",\"position\":2,\"name\":\"Developing\",\"item\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/category\\\/developing\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#listItem\",\"name\":\"CString misuse #1\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#listItem\",\"position\":3,\"name\":\"CString misuse #1\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/category\\\/developing\\\/#listItem\",\"name\":\"Developing\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#organization\",\"name\":\"James R Twine Blog\",\"url\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/author\\\/jtwine\\\/#author\",\"url\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/author\\\/jtwine\\\/\",\"name\":\"James R. Twine\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/77d36362e353ec1c88adc48b48055faee96497898a208d3a6a05897d4902e552?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"James R. Twine\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#webpage\",\"url\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/\",\"name\":\"CString misuse #1 - James R Twine Blog\",\"description\":\"This is the first of many examples of ways to misuse and\\\/or abuse MFC's CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/\\\/ CString str( \\\"First Part Of Message\\\\n\\\" ); str = str + \\\"Second Part\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/cstring-misuse-1\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/author\\\/jtwine\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/author\\\/jtwine\\\/#author\"},\"datePublished\":\"2007-08-17T07:57:10+00:00\",\"dateModified\":\"2007-09-17T11:21:21+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/\",\"name\":\"James R Twine Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/jrtwine.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"CString misuse #1 - James R Twine Blog","description":"This is the first of many examples of ways to misuse and\/or abuse MFC's CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( \"First Part Of Message\\n\" ); str = str + \"Second Part","canonical_url":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#blogposting","name":"CString misuse #1 - James R Twine Blog","headline":"CString misuse #1","author":{"@id":"https:\/\/jrtwine.com\/blog\/author\/jtwine\/#author"},"publisher":{"@id":"https:\/\/jrtwine.com\/blog\/#organization"},"datePublished":"2007-08-17T07:57:10+00:00","dateModified":"2007-09-17T11:21:21+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#webpage"},"isPartOf":{"@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#webpage"},"articleSection":"Developing, You're Doing It Wrong"},{"@type":"BreadcrumbList","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/jrtwine.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/category\/developing\/#listItem","name":"Developing"}},{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/category\/developing\/#listItem","position":2,"name":"Developing","item":"https:\/\/jrtwine.com\/blog\/category\/developing\/","nextItem":{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#listItem","name":"CString misuse #1"},"previousItem":{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#listItem","position":3,"name":"CString misuse #1","previousItem":{"@type":"ListItem","@id":"https:\/\/jrtwine.com\/blog\/category\/developing\/#listItem","name":"Developing"}}]},{"@type":"Organization","@id":"https:\/\/jrtwine.com\/blog\/#organization","name":"James R Twine Blog","url":"https:\/\/jrtwine.com\/blog\/"},{"@type":"Person","@id":"https:\/\/jrtwine.com\/blog\/author\/jtwine\/#author","url":"https:\/\/jrtwine.com\/blog\/author\/jtwine\/","name":"James R. Twine","image":{"@type":"ImageObject","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/77d36362e353ec1c88adc48b48055faee96497898a208d3a6a05897d4902e552?s=96&d=mm&r=g","width":96,"height":96,"caption":"James R. Twine"}},{"@type":"WebPage","@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#webpage","url":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/","name":"CString misuse #1 - James R Twine Blog","description":"This is the first of many examples of ways to misuse and\/or abuse MFC's CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( \"First Part Of Message\\n\" ); str = str + \"Second Part","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/jrtwine.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/#breadcrumblist"},"author":{"@id":"https:\/\/jrtwine.com\/blog\/author\/jtwine\/#author"},"creator":{"@id":"https:\/\/jrtwine.com\/blog\/author\/jtwine\/#author"},"datePublished":"2007-08-17T07:57:10+00:00","dateModified":"2007-09-17T11:21:21+00:00"},{"@type":"WebSite","@id":"https:\/\/jrtwine.com\/blog\/#website","url":"https:\/\/jrtwine.com\/blog\/","name":"James R Twine Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/jrtwine.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"James R Twine Blog -","og:type":"article","og:title":"CString misuse #1 - James R Twine Blog","og:description":"This is the first of many examples of ways to misuse and\/or abuse MFC's CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &quot;First Part Of Message\\n&quot; ); str = str + &quot;Second Part","og:url":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/","article:published_time":"2007-08-17T11:57:10+00:00","article:modified_time":"2007-09-17T15:21:21+00:00","twitter:card":"summary_large_image","twitter:title":"CString misuse #1 - James R Twine Blog","twitter:description":"This is the first of many examples of ways to misuse and\/or abuse MFC's CString class. While this example (and following ones) are specific to MFC, they likely apply to all string classes (mutable or not). Here is the offending code: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ CString str( &quot;First Part Of Message\\n&quot; ); str = str + &quot;Second Part"},"aioseo_meta_data":{"post_id":"56","title":null,"description":null,"keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-09-17 06:48:54","updated":"2026-09-17 06:48:54"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/jrtwine.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/jrtwine.com\/blog\/category\/developing\/\" title=\"Developing\">Developing<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCString misuse #1\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/jrtwine.com\/blog\/"},{"label":"Developing","link":"https:\/\/jrtwine.com\/blog\/category\/developing\/"},{"label":"CString misuse #1","link":"https:\/\/jrtwine.com\/blog\/cstring-misuse-1\/"}],"_links":{"self":[{"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jrtwine.com\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}