1.6. Working With Embedded Systems

1.6.1. The Problem

There are hidden costs concerning memory resources when programming in C++. On a desktop system with plenty of virtual memory you don't necessarily recognise it. But working with embedded systems is different as there is only a limited amount of physical memory available.

For that reasons it is important to know the places where he compiler adds additional code:

Standard Template Library (STL)

Templates and other inline code may increase the resulting footprint of the binary.

Exceptions

After throwing an exception the complier must cleanup and destroy all automatic objects in the previous scopes. This bookkeeping consumes space as well.

Runtime Type Information (RTTI)

To provide information about an object at runtime the compiler includes data like the class name or the inheritance tree in the binary.

The following table shows the the size of the mockpp library when compiled with different settings. The size ranged from around 320KiB with the default settings down to 180KiB with as many features disabled as possible.

Table 1.1. Memory Consumption of C++ Code

FeatureNumber of BytesPercentage
regular STL92KiB28%
RTTI12KiB4%
Exceptions37KiB11%
Library Code180KiB57%


When changing the options for the test executables the size was reduced down to 45%, but using the regular STL resulted only in 13% larger code.