Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What c++ features should be avoided for embedded development

Tags:

c++

embedded

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"
like image 564
cooper Avatar asked Jul 18 '10 06:07

cooper


People also ask

What are the features of embedded C?

Embedded C uses most of the syntax and semantics of standard C, e.g., main() function, variable definition, datatype declaration, conditional statements (if, switch case), loops (while, for), functions, arrays and strings, structures and union, bit operations, macros, etc.

Why is C preferred in embedded?

C provides optimized machine instructions for the given input, which increases the performance of the embedded system. Most of the high-level languages rely on libraries, hence they require more memory which is a major challenge in embedded systems.

Is C or C++ better for embedded systems?

Experts say myths have often existed about using C++ for embedded systems. Myths include that C++ performs too slowly. That's not true with properly written C++ code. Reality: Engineers can avoid bloated machine code with C++, and the language can run as quickly and efficiently as C.

Why C is used in embedded systems not C++?

there are two answers to this: Any programming language that is “Turing Complete” (almost every programming language is) - can be used to write anything that any other “Turing complete” language can do…so, there is nothing that can be written in C++ that cannot be written in C (or Java or Python or assembly code or….


2 Answers

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.

like image 137
Joel Avatar answered Oct 05 '22 05:10

Joel


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.

like image 34
zvrba Avatar answered Oct 05 '22 05:10

zvrba