11.06 Generic Programming vs Flash Memory Overhead in Embedded Microcontrollers
💻 1. Annotated Source Code
#include <iostream> #include <string> using namespace std; //double getBigger(double a, double b); //int getBigger(int a, int b); //string getBigger(string a, string b); template <class T> T getBigger(T a, T b) { return (a > b) ? a : b; } template <class T> T getSmaller(T a, T b) { return (a < b) ? a : b; } int main() { double d1 = 3.14; double d2 = 5.55; int i1 = 11; int i2 = 9; string s1 = "Alice"; string s2 = "John"; double biggerDub = getBigger(d1, d2); int biggerInt = getBigger(i1, i2); string biggerStr = getBigger(s1, s2); double smallerDub = getSmaller(d1, d2); int smallerInt = getSmaller(i1, i2); string smallerString = getSmaller(s1, s2); cout << "Bigger items:" << endl; cout << "\t" << biggerDub << "\n\t" << biggerInt << "\n\t" << biggerStr << endl; cout << "Smaller items:" << endl; cout << "\t" << smallerDub << endl; cout << "\t" << smallerInt << endl; cout << "\t" << smallerString << endl; return 0; } //double getBigger(double a, double b) { // return (a > b) ? a : b; //} // //int getBigger(int a, int b) { // return (a > b) ? a : b; //} // //string getBigger(string a, string b) { // return (a > b) ? a : b; //}
📐 2. Architecture & UML Class Model
📚 3. Core C++ Concepts Deep-Dive
1. Compile-Time Monomorphization
Unlike Java or C# generics (which use type erasure at runtime), C++ templates are instantiated at compile-time. The compiler generates an entirely dedicated copy of the machine code for each unique type (print<int>, print<double>, print<string>).
⚡ 4. Embedded Systems & Hardware Reality
1. The Flash ROM Code Bloat Hazard
If a large template class is instantiated with 10 different types on a 64KB Flash microcontroller, the compiler will emit 10 distinct copies of the class binary, easily overflowing available Flash ROM.
💡 Embedded Optimization: Template Hoisting (Common Base Idiom)
Extract all type-independent code into a non-templated base class. The templated derived class only implements thin inline type-casts, sharing a single binary implementation in Flash ROM!
💡 5. Production-Ready Embedded Refactoring
Template hoisting pattern reducing Flash ROM consumption:
// Non-templated base: Single copy in Flash ROM (.text) class CircularBufferBase { protected: void* buffer_; size_t head_ = 0, tail_ = 0, capacity_; void advanceTail() { tail_ = (tail_ + 1) % capacity_; } }; // Thin templated wrapper: Inlined with ZERO extra Flash code template <typename T, size_t N> class CircularBuffer : public CircularBufferBase { public: CircularBuffer() { buffer_ = storage_; capacity_ = N; } void push(T val) { storage_[head_] = val; head_ = (head_ + 1) % N; } private: T storage_[N]; };
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.