EmbDev.net

Forum: µC & Digital Electronics C vs. C++ with ARM C/C++ Compiler


von Marco .. (borealis_1427)


Rate this post
useful
not useful
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

von Klaus W. (mfgkw)


Rate this post
useful
not useful
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.

von Klaus W. (mfgkw)


Rate this post
useful
not useful
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).

von Marco .. (borealis_1427)


Rate this post
useful
not useful
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

von Jürgen (Guest)


Rate this post
useful
not useful
> 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.

von Klaus W. (mfgkw)


Rate this post
useful
not useful
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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.