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:
Templates and other inline code may increase the resulting footprint of the binary.
After throwing an exception the complier must cleanup and destroy all automatic objects in the previous scopes. This bookkeeping consumes space as well.
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
|Feature||Number of Bytes||Percentage|
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.