tags:

views:

165

answers:

4

I'm interested in compiling a list of c++ features that are not advisable for use in embedded systems (and which may cause people to recommend c over c++). Please try to add why if you know, or add your why to others' answers.

Here's one for a start (the only one I know)

  • Dynamic polymorphism, don't know why, but someone said it's "costly"
+3  A: 

You should choose features depending on your device. It could be sensible for some feature or maybe not. It depends on its architecture. For instance, Google has a reduced version of C++ compiler for Android platform. Simple common rule is to avoid constructions that will result in heavy runtime code.

Kirill V. Lyadvinsky
+7  A: 

The Joint Strike Fighter Coding Standards here: http://www2.research.att.com/~bs/JSF-AV-rules.pdf are a pretty good overview of how to use C++ for embedded programming.

The ban on Dynamic Polymorphism is a holdover from the 90s, and has no rational basis. It takes no longer to call a virtual function than it does to do a switch and a call. If you are going to avoid virtual function calls, you might as well be using C.

Joel
+2  A: 

Certain features require run-time support, so if you miss the required support, you should avoid those features. In particular, the following features usually need extra run-time support:

  • exceptions
  • RTTI
  • dynamic memory allocation
  • virtual inheritance (a bit unsure about this one)

People also usually mention templates, but they are only an advanced macro facility -- so you can freely use them in embedded systems. Still, you might want to avoid them since they can lead to code bloat after compilation.

Your embedded system should come with documentation saying what, if any, run-time support for C++ (and otherwise) is available.

zvrba
Virtual inheritance doesn't need run-time support. It comes at a cost, though (an additional indirection when accessing base class members). If you need to avoid that run-time cost, then you need to avoid virtual inheritance.,
sbi
+1  A: 
  1. Excessive template use. Multiple template instantiations with different parameters will result in multiple copies of the same functions in your object code, hence increasing its size, unless your compiler is smart enough to fold identical code (e.g. if template depends on a type T, instantiation with int will in most cases be identical to instantiation with long).
    A way to avoid code size increase with templates, you can write an unsafe core version of your code, and have thin type-safe template wrappers.
  2. dynamic_cast can be pretty costly CPU-wise because it needs to scan the class hierarchy and do string comparison of class names [citation needed].
Alex B