{"id":6961,"date":"2014-06-30T16:14:39","date_gmt":"2014-06-30T15:14:39","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=6961"},"modified":"2026-03-26T16:43:41","modified_gmt":"2026-03-26T20:43:41","slug":"c-yet-more-questions-and-answers","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2014\/06\/30\/c-yet-more-questions-and-answers\/","title":{"rendered":"C++ &#8211; yet more Questions and Answers"},"content":{"rendered":"<p>I am continuing to mine the rich vein of questions from my online C++ lectures, which I started <a href=\"https:\/\/blogs.mentor.com\/colinwalls\/blog\/2014\/06\/16\/c-more-questions-and-answers\/\" target=\"_blank\" rel=\"noopener noreferrer\">two weeks ago<\/a>. I am always pleased to receive questions about any aspect of embedded software by comment, <a href=\"mailto:colin_walls@mentor.com\" target=\"_blank\" rel=\"noopener noreferrer\">email<\/a> or through social media.<\/p>\n<p>Another three questions to consider today &#8230;<!--more--><\/p>\n<h3>I just wondering how the compiler and linker optimize C++ code. How do we compare the this optimization in different tool chains?<\/h3>\n<p>For the most part, a C++ compiler will just do the same kind of optimizations that all compilers do. Of course, if it is designed for embedded use, there will be fine control over those optimizations. A compiler does have the possibility to add some &#8220;hints&#8221; to help the linker. A linker, which is designed to optimize C++ utilization can do a number of things, for example:<\/p>\n<ul>\n<li>elimination of redundant code where template instantiation has been done on a per translation unit basis<\/li>\n<li>removal of unused out of line copies of inline functions<\/li>\n<li>elimination of redundant copies of a base class subobject, when a class is based on multiple classes with a common base<\/li>\n<li>on ARM EHABI, the exception handler table entries can be combined for adjacent functions whose unwind opcodes are the same; this can save quite a bit of space because most functions have very simple unwind opcode sequences, and a lot of them end up being the same<\/li>\n<\/ul>\n<h3>What would be the difference between C++ and C# ?<\/h3>\n<p>The C syntax has been the basis for many other programming languages. In the late 1980s and 90s, there were at least three object oriented languages that were based on C. C++ was just one of those. Another one which has survived is Objective-C. More recently Java and Python are examples of the syntax being &#8220;overloaded&#8221;. C# is yet another, but differs because it is vendor [Microsoft] specific. C# has almost no relevance to embedded developers, unless, I suppose, they are using Windows Embedded. Much the same comments may be applied to Apple&#8217;s newly announced Swift language. C# is a &#8220;managed language&#8221; [a term Microsoft made up], and C++ is not. Managed languages are compiled down to bytecodes [much like Java], and then JITed at runtime to hardware specific instructions.<\/p>\n<div>\n<div><\/div>\n<div><a style=\"color: #a7a7a7;text-decoration: none;font-weight: normal !important;border: none\" href=\"http:\/\/www.gettyimages.com\/detail\/83266599\" target=\"_blank\" rel=\"noopener noreferrer\">#83266599<\/a> \/ <a style=\"color: #a7a7a7;text-decoration: none;font-weight: normal !important;border: none\" href=\"http:\/\/www.gettyimages.com\" target=\"_blank\" rel=\"noopener noreferrer\">gettyimages.com<\/a><\/div>\n<\/div>\n<h3>Do you use the C++ template features and library a lot in your code? Sometimes programmers worry about code bloat.<\/h3>\n<p>This question raises a number of issues. Personally, I have no objection to using templates, as they are a legitimate &#8220;shorthand&#8221; way to code sometimes. There is a perception that they can lead to code bloat. This is for two reasons:<\/p>\n<ol>\n<li>A function template, for example, may be quite a small piece of source code, but may yield a great many different instantiations, resulting in a lot of binary code. This is not &#8220;bloat&#8221; because there is no redundancy. I would probably prefer to do explicit function overloading so that the scale of the coding is visible.<\/li>\n<li>As mentioned in the first question above, templates are normally instantiated in each translation unit, which could result in redundant code, unless the linker is capable of deduplicating [optimizing] it.<\/li>\n<\/ol>\n<p>I am wary of template libraries, as many are not optimized for or compatible with the needs of embedded software.<br \/>\nOnce again, I acknowledge the input from my colleague <a href=\"mailto:jonathan@codesourcery.com\">Jon Roelofs<\/a> on this blog posting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am continuing to mine the rich vein of questions from my online C++ lectures, which I started two weeks&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-6961","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=6961"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6961\/revisions"}],"predecessor-version":[{"id":10282,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6961\/revisions\/10282"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=6961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=6961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=6961"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=6961"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=6961"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=6961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}