C++ – yet more Questions and Answers
I am continuing to mine the rich vein of questions from my online C++ lectures, which I started two weeks ago. I am always pleased to receive questions about any aspect of embedded software by comment, email or through social media.
Another three questions to consider today …
I just wondering how the compiler and linker optimize C++ code. How do we compare the this optimization in different tool chains?
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 “hints” to help the linker. A linker, which is designed to optimize C++ utilization can do a number of things, for example:
- elimination of redundant code where template instantiation has been done on a per translation unit basis
- removal of unused out of line copies of inline functions
- elimination of redundant copies of a base class subobject, when a class is based on multiple classes with a common base
- 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
What would be the difference between C++ and C# ?
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 “overloaded”. 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’s newly announced Swift language. C# is a “managed language” [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.
Do you use the C++ template features and library a lot in your code? Sometimes programmers worry about code bloat.
This question raises a number of issues. Personally, I have no objection to using templates, as they are a legitimate “shorthand” way to code sometimes. There is a perception that they can lead to code bloat. This is for two reasons:
- 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 “bloat” because there is no redundancy. I would probably prefer to do explicit function overloading so that the scale of the coding is visible.
- 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.
I am wary of template libraries, as many are not optimized for or compatible with the needs of embedded software.
Once again, I acknowledge the input from my colleague Jon Roelofs on this blog posting.