Using C++ in
What is it anyway?
This article is about programming microcontrollers, especially the simpliest ones, without operating system.
Many languages can be used for programming such devices: assembly, basic, C, etc... Each of them has advantages and disadvantages, of course.
I would like to introduce an unusual way of programming: using C++ with very small amount of memory on a slow CPU.
I use gcc for compiling, it is an open-source program for nearly every existing CPUs, with very good optimizations.
My favourite hardware is the Atmel AVR 8-bit RISC: a small but smart MCU family,
I have some projects based on it. Some of them has only 128 byte RAM but C++ can be used on those devices too as you can see in the followings.
Why C++ ?
Why not? :-)
To answer this question let's see the advantages and disadvantages of C++ first:
The C++ is reportedly a resource-waster language, which is unusable for small systems. Yes, it can be true if it is used in featherbrained way. It has many possibilities, which may need a lot of memory and CPU in a short way. Modern computers have fast CPU and large memory (I mean desktop computers here), so writing an application for such a system can be easy using such a high-level approach. But what about the embedded systems? The features of C++ used here must be selected carefully.
C++ has many features which need small amount of resources or do not need any. These features can be very useful anyway. I would like to introduce them, see the details below.
Passing the 'this' pointer is commonly mentioned as a disadvantage of the C++, because it needs an additional parameter for all functions. It is a big mistake: passing such a pointer can be avoided only by using global variables, independently from the language used. Using global variables is practical only in some special cases (it is a completely unstructured solution). Functions which do not need to reach the class variables can be declared as 'static', and therefore do not have the 'this' pointer. So, passing the 'this' pointer is not a disadvantage, but an advantage of the C++ language, because it simplifies the source code and makes it more readable.
Using C++ in details
I would like to describe the difference between C and C++ only, so I do not mention the advantages of using structures and other C-specific features.
Constructors and destructors are functions which are called automatically by the compiler. This feature does not
need any additional resource: other languages do it the same way, but they must be called by the programmer.
Automatic calling makes the project more reliable: the programmer cannot forget to call it.
See the class Disable at the bottom of this page for details.
Using it in Modular Design:
Other interesting possibility is to build objects with global or static instances, and use their
constructors to subscribe themselves to a system service. It makes the system design very simple: adding
such an object at link time adds (or modifies) some behaviors. It is not necessary to modify the
main code at all, just the list of objects linked to the resulting firmware. See the modules in my
The Inheritance also does not need any additional resource, but can make the design easier and more
Using virtual functions we can make our code more flexible. It needs a virtual table with one pointer for each
virtual function per class (and not per instance!) and one pointer for each instance for the
virtual table itself. This is probably acceptable for the smallest hardwares too, if it is used with care. At
least it can worth for some cases.
Using new and delete:
Very small systems probably do not have any memory management, so using new and delete is not
possible at all. Using placement new is possible,
however: it also does not need any additional resources but can be useful only in some special cases.
Exception handling is a very interesting feature of C++. It can help e.g. in the error handling, which is a very
important part of a software (firmware) - if the reliability is important. Using it the program flow can be
simplified, which can lead to faster code than using C because it is not necessary to check
the return status in most cases this way. Unfortunately, it needs a lot of resources and cannot be used for
some very small hardwares. Using gcc there is a possibility to use a
longjmp-based solution which needs only some kilobytes
of additional text. It can be acceptable in some cases, however, it can be too much for the smallest
I hope that my project called Gavrio is a good example.
It contains almost all the methods mentioned above, and its result (the generated machine code) is nearly optimal.
I check it regularly: in most of the cases it cannot be smaller nor faster, even if it was written in
assembly. Some bytes of memory, and some cycle of execution could be saved, but it does not worth: the
simplicity of the source and the efficiency of the development process makes the developer(s) be able to manage
more complicated tasks easier by using higher level languages, such as C++.
It is often necessary to disable the interrupts for a specific part of the program.
See its very simple example Disable for details.