Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

How is C++ good for embedded programming? How is C++ not good for embedded programming?

share|improve this question
    
See various related questions, if not dupes, over at stackoverflow.com. –  delnan Feb 3 '11 at 15:16
    
Related: stackoverflow.com/questions/812717/… –  Steve Melnikoff May 11 '11 at 11:52
    
I think you should read this talk slides by Peter Bushell –  Abdurahman Aug 1 '13 at 13:54
add comment

3 Answers

This article sums up the points you have to worry about when using c++ in embedded context.

Main points :

Get Some Training!

Be Careful Using Streams

Be Careful Using Templates

Exceptions

Use Polymorphic Interfaces to Make Frameworks

Make an OS Encapsulation Library

ROM Beware

Multiple Interface Levels

Come up with common naming conventions.

Debug and Error System First

Think About Memory

Think About System Integrity

Remember to Use Volatile

share|improve this answer
add comment

Typically, in embedded systems you only see either C or C++. Embedded systems often require direct access to hardware, explicit memory management, a high level of performance and most importantly deterministic behavior. C and C++ both offer all of these capabilities.

Possible disadvantage over C:

C++ can often make applications bloat. This mostly a direct result of using exceptions and poor usage of templates. Therefore, in versions of C++ explicitly designed for embedded platforms you will often see these features taken out. Personally, I think C is easier to read, but that is probably because I don't use C++ frequently. However, if you plan to read the assembly code output then C has a clear advantage over C++. C++ does some name mangling and generates virtual function tables for virtual function that often make it more difficult to understand.

Advantage over C:

Many of the features in C++ were a result of attempting formalize common design practices that were perhaps difficult or clunky to implement in C, data encapsulation being high on the list. You can sort of fake this in C using static variables and functions in modules, but it is difficult to provided any sort of protection within a module itself. Furthermore, there is a not a good way to associate functions with specific data objects. C++ also supports RAII, which is very convenient for managing dynamically allocated resources.

The concept of base classes is also often convenient. For example, suppose a system has multiple input/output devices. It probably safe to assume that all of those devices will have read and write facilities. Having a base class with virtual functions that must be implement by the child class has distinct advantages including providing a common interface and providing a template of all function that should or must be implemented.

I am probably missing some things, but this is what immediately comes to mind

share|improve this answer
add comment

checking out EC++, a dialect of C++ especially for embedded systems, may help

but last i did anything embedded was in 1983 or thereabouts. used PL/M-86 and assembly.

full c++ requires some runtime support, especially for exception handling and initialization of static variables. possibly one might desire to not have that in a small embedded system. hence dialects like EC++.

cheers & hth.,

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.