Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ usage in embedded systems

Tags:

c++

embedded

What features of C++ should be avoided in embedded systems?

Please classify the answer by reason such as:

  • memory usage
  • code size
  • speed
  • portability

EDIT: Lets' use an ARM7TDMI with 64k ram as a target to control the scope of the answers.

like image 820
JeffV Avatar asked Sep 23 '08 13:09

JeffV


People also ask

Why is C popular for embedded systems?

Although not originally designed for embedded software development, the C language allows a range of programming styles from high-level application code down to direct low-level manipulation of hardware registers. As a result, C has become the most popular programming language for embedded systems today.

Is C good for embedded programming?

For many embedded systems, C or C++ will be the best choices. In part, that's because they are “compiled” languages and extremely efficient. In compiled languages, the machine (or embedded device) directly translates the code, which means the language is fast and stable.

Why is C used rather than C++ in embedded systems?

C is a much smaller language to write a compiler for, so a lot of small CPUs have a C compiler available. C++ is massively more difficult, so doesn't often happen. As a result, you can rely on C code for any given chip, but less so having C++ available. This trains embedded coders to use C as their main language.

Is C better than C++ for embedded systems?

C gives you much more control than C++. Show activity on this post. I've written some code for ARM7 embedded paltform on IAR Workbench.


1 Answers

RTTI and Exception Handling:

  • Increases code-size
  • Decreases performance
  • Can often be replaced by cheaper mechanisms or a better software-design.

Templates:

  • be careful with them if code-size is an issue. If your target CPU has no or only a very tiny ínstruction cache it may reduce the performance as well. (templates tend to bloat code if used without care). Otoh clever meta-programming can decrease the code-size as well. There is no clear cut answer on his.

Virtual functions and inheritance:

  • These are fine for me. I write almost all of my embedded code in C. That does not stop me from using function-pointer tables to mimic virtual functions. They never became a peformance problem.
like image 70
Nils Pipenbrinck Avatar answered Oct 14 '22 15:10

Nils Pipenbrinck