Design Article
Guidelines for using C++ as an alternative to C in embedded designs: Part 1
Colin Walls
4/11/2008 1:40 AM EDT
Embedded software applications are most commonly written in C. For many years, C++ has been seen as the natural successor and has found greater acceptance, but the increase in its usage has been much slower than anticipated.
There are a number of reasons for this. Firstly, embedded developers are quite conservative and prefer to use solutions that are proven rather than novel " "if it ain't broke, don't fix it".
There is also the lesson of experience. Many developers have attempted to use C++ for embedded applications and failed. Such failures may sometimes be attributed to shortcomings in development tools, but most often it is the inappropriate use of the language " treating an embedded system like a desktop computer " that is to blame.
In this two part tutorial, an attempt is made to address some of these issues, to give guidelines to the effective use of C++ for embedded applications and to show that the language has some true benefits to the embedded developer.
One of the key reasons why C++ has gained popularity in the computing world in general is its history. Although it is a modern language, with object oriented capabilities, it has backward compatibility that makes its adoption " learning and application " straightforward.
Figure 1 below gives some insight into the evolution of the language. Later in this series we will look at C/C++ compatibility in more detail.
![]() |
| Figure 1. The Genealogy of C++ |
Limitations of C
Although C is widely used, it has limitations, as it was not designed
for embedded applications or for projects of a scale that is now
commonplace. Key limitations include:
1) C is extremely powerful and flexible and can therefore be dangerous.(It has low level capabilities - which are useful for embedded " but also represent many pitfalls for the unwary.)
2) Programmers need to be very methodical and disciplined
3) Programmers need to understand how the program behaves at low and high levels (large projects are thus hard to maintain)
4) Programmers need an expert knowledge of the application
However, C++ has powerful object oriented capabilities which can help significantly with addressing the limitations of C:
1) it encapsulates and hides areas of high expertise from non-experts into "objects;" (A test case will demonstrate the encapsulation of expertise later in Part 2 in this series).
2) Objects can be used intuitively by non-experts to implement conceptual designs at a high-level
Language Overview
Like ANSI C, C++ features many enhancements to the original C language
in addition to its object oriented capabilities. C++ is not simply a
super-set of C, as the two languages evolved in parallel.
However, the language may be learned and applied incrementally, as will be illustrated later in this paper. For the moment, here is a summary of some of the useful language features:
1) Dynamic memory allocation operators. The operators new and delete are alternatives to the library functions malloc() and free() and lead to more readable and less error-prone code.
2) Function prototypes. Iintroduced in C++ and adopted into ANSI C, their use is mandatory.
3) Function parameter default values. A function may have default values for trailing parameters to enhance code readability.
4) Reference parameters. Function parameters may be passed by reference instead of by value (copy). This gives the efficiency of using pointers without the great potential for errors that results from their use. Here is a simple example function:
void swap(int& a,
int& b)
{
int temp = a;
b = temp;
}
4) Inline functions. The code for a (small) function may be declared as being inline " i.e. a copy of the actual code is included at each call site. This improves the speed of execution, but may increase code size. This may be achieved using the inline keyword or by including the code in the body of a class definition. This is only advice to a compiler, which will probably take account of current optimization settings.
5) Function overloading. Multiple functions may be defined with the same name. The compiler would distinguish between them by their unique number/type of parameters. This results in more readable code, with less contrived function naming.
6) Typesafe linkage. In C++, all function names are "mangled" " their names are modified to reflect parameter and returned data types. This enables the linker to perform additional cross-module checking without being required to "know" about C++. This is also the mechanism whereby function overloading is implemented.





Marco Wang
4/15/2008 11:27 PM EDT
We already get enough fights on C and C++. I believe none is better than another, it's all about how to use them. We always see tons of lines code which are hardly to understand and need refactoring entirely even C++ is used there, and also there are perfect codes written in C in a object oriented way.
It doesn't make sense to discuss which language is better - it really depends on how the language is used.
Sign in to Reply
Vitaly Luban
4/17/2008 1:01 PM EDT
This reminds me of communism. With the _proper_ use (at least as it's proponents claim) it should be wonderful, but somehow every single time every possibility of _misuse_ gets exploited, leading to an ultimate failure. Is not that a human nature then, to try every possibility of misuse? Is not it a better way to limit those possibilities somehow?
Sign in to Reply
Skyhigh
5/6/2008 3:06 AM EDT
It depends who is developing the software and the complexity of the application.
If I want to design a simple embedded application using a simple 8-bit MCU, no RTOS needed, the software can be written largely by C, with dedicated blocks of assembly instructions for quick subroutines, thus keeping small program size (can be further optimised by setting your compiler, eg. IAR embedded workbench).
If I want to develop an applicaiton software for a mobile phone that will run on Windows CE, you actually have to do it in C++.
I have to disagree with previous post.
You can definitely use dynamic memory, provided you know exactly when and where you free it. Careless software engineers forget to free the allocated memory or free it at the wrong line of the source code, thus causing memory leakage or worst - corrupting the data, especially when programmers forget to set pointer to NULL, and carelessly reusing that pointer.
It is really to do with software experience and good understanding of what and how you want to do it.
Sign in to Reply
Theckyam
5/21/2008 9:51 PM EDT
I should agree with Marco Wang and others on "it is more important how a language is used" or rather "how a language is NOT ABUSED". Ultimately discipline in programming matters than which language is used at best (limited by the development tools capability of course). I have seen beautiful code in asm, and worst possible C++ code. I just want to note that there are differences between "coding" and "engineering" that matters a lot.
---
Saravanan T S
9+ Years of software engineering + management
---
Sign in to Reply
gheorghe
5/26/2008 2:08 PM EDT
"A class is a new data type." OK! But in the future we will solve problems without "new data types"!!!!!!!!!!!!!! I'm working to a new programming paradigm in software and ... without classes!!!!
Sign in to Reply