Hi, I have a question regarding C/C++ with ARM C/C++ Compiler, RVCT4.0. Does anybody have figures or study results of the overhead of using C++ instead of C in terms of MIPS and resources (RAM, ROM footprint)? Thanks for any tips and insights! -- Regards, Marco
I have no experience regarding ARM, but the impact of C++ may be nearly 0 in some cases or frightening in other cases. It depends on which featrures of C++ you want to use. C++ is nearly a superset of C, so you can program C++ like C with no additional overhead. Cautious use of templates may be very useful with no overhead. Virtual methods should be checked. std::strings are clearly more expensive than zero terminated strings, and so on. There will be no easy way to calculate the additional resources in general.
BTW: using inlining (e.g. for operators) may produce better code in C++ in some cases than equivalent function calls in C. Once i tried a template class for fixed point arithmetic in C++, tested on AVR and had no disadvantages in C++ compared to C, neither memory nor speed. The thread can be found in Beitrag "Festkommazahlen in Würde" (in german, sorry).
Hi Klaus, thanks for your input. I have attended a talk at the Embedded World 2010 in Nuremberg about this topic, but specific to the IAR compiler. The speaker categorized the overhead (in terms of footprint) for the various features of C++ as free, cheap and expensive. The following list shows the conclusion: • encapsulation/classes - FREE • namespaces - FREE • inlining - FREE • operator overloading - FREE • constructors/destructors - FREE • references - FREE • virtual functions - CHEAP • templates - EXPENSIVE • STL (Standard Template Library) – EXPENSIVE • RTTI – expensive • exceptions - expensive What is missing in the list are dynamic and reinterpret casts. I fear these would be expensive too. As I see, your experience with templates is different from what IAR states. Virtual functions might cause increasing overhead in deep hierarchies I guess (vtable gets more entries). Nevertheless, the above list only tells me how the IAR compiler deals with the features. Since I am using the ARM Real View Compiler, figures on this would be great. -- Regards, Marco
> What is missing in the list are dynamic and reinterpret casts. > I fear these would be expensive too. Dynamic casts are expensive, because they need RTTI. Reinterpret casts should be free, they are just a way to circumvent the type system.
ACK Note: I do not agree with "templates - EXPENSIVE" in general. Templates have no overhead compared with the same functionality written as usual functions or methods. Templates become expensive (in terms of generated code) when they are instantiated more than one time - but again not more expensive than normal functions written in as many variants. The only problem while using templates is the fact that the programmer may be not aware of how many instances of a template he produces simply by using the templates.