How is C++ good for embedded programming? How is C++ not good for embedded programming?
closed as too broad by gnat, Bart van Ingen Schenau, amon, MichaelT, GlenH7 May 9 at 17:17There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question. |
|||||||||||||
|
This article sums up the points you have to worry about when using c++ in embedded context. Main points :
|
|||
|
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 |
|||
|
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., |
|||
|