Using C++ in
embedded environment

  1. 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.

  2. Why C++ ?
    Why not?    :-)
    To answer this question let's see the advantages and disadvantages of C++ first:

  3. 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.

  4. Some examples
    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++.