Categories
Blog Posts

Torrox Prefers C++ Over C

If you ask me to write firmware for your embedded hardware, I will most likely offer you a solution written in C++.

I will provide a C++ solution unless you explicitly request it to be written in C. While there are a few legitimate reasons to choose C over C++, many arguments suggesting that C is preferable to C++ for embedded systems are often misleading or untrue.

Technical arguments, such as “bloated program code” or “excessive memory usage” can be easily debunked. C++ is essentially a superset of C, so there is no inherent reason why a C program compiled with a C++ compiler should become bloated or suddenly consume more RAM. If a certain C++ feature incurs costs that are not affordable, that feature can simply be avoided.

More subjective arguments like “code obscures what it does” or “C++ is too complex/complicated” boil down to the need to know the tools in use. For example, using floating-point arithmetic on an 8-bit MCU can lead to the same surprises as the naive use of STL containers.

This leads to the first valid reason to avoid C++: If customers plan to maintain the firmware themselves but do not have personnel proficient in C++, it might be more practical to write the firmware in C. (And the lack of C++ knowledge could simply be due to not being a full-time software developer.)

Secondly, once in a while (but rather seldom), a C++ compiler might not be available for the hardware platform.

C++ Has a Lot to Offer

C++ has so much to offer that its features simply cannot be ignored. Most C compilers nowadays are C++ compilers under the hood that have some special C mode. So the tooling comes basically for free and is already installed on your machines.

Testability

Correctness and robustness are software attributes that are highly valued in the embedded industry due to the consequences that come with bugs in software. This leads to an increased value of tests. This begins with thorough unit tests, where abstractions are required to isolate the different parts of the software so that they become testable. C++ provides a rich set of abstractions which, most of the time, have zero additional costs/overhead (so-called Zero Cost Abstractions).

Correctness

C++’s rich type system allows attaching all kinds of information at compile time to a value. Tracking the physical unit and scale of a value is an example where this can be utilized to find bugs of mismatching units at compile time without any additional runtime costs.

Catching bugs at compile time greatly reduces the time required to debug software at runtime.

Small Binaries

To keep firmware sizes small, it makes sense to move as much logic/calculations from runtime to compile time. When searching for the correct settings of a PLL, looping over a set of settings to find the one that results in the desired frequency is a straightforward algorithm that we find in a lot of vendors’ SDKs. When the desired frequency is fixed and known at compile time, a C++ implementation can guarantee that the algorithm will find the correct settings at compile time!

Coping with Complexity, Increasing Productivity

Software development for embedded systems started with a couple of hundred assembler lines a long time ago. Currently, we see a trend to implement an increasing amount of a system’s requirements in software. Complex protocols like WLAN or Bluetooth are more flexible to be handled in software and implemented by increasingly large SoCs (or even multiple SoCs).

C++ provides better tools to handle increasing complexity.

1 reply on “Torrox Prefers C++ Over C”

Leave a Reply

Your email address will not be published. Required fields are marked *