In embedded firmware systems, dynamic memory allocation is often forbidden due to fragmentation risks and nondeterministic behavior.
Despite this restriction, firmware frequently requires runtime polymorphism to select and execute different driver implementations through a common interface.
You are given a small firmware-style driver framework with a base driver interface and two concrete drivers.
Your task is to implement runtime polymorphism without using heap allocation, ensuring safe and deterministic object lifetime.
You must select the correct driver at runtime and invoke its behavior through a base-class interface, while keeping all objects stack-allocated.
Input / Program Flow
Driver selection rules:
0 → select the SPI driver1 → select the I2C driverProgram flow:
Output
If input is 0, output:
SPI transfer started
If input is 1, output:
I2C transfer started
Output Requirements
Constraints
new, delete, malloc, or free
Input
0
Expected Output
SPI transfer started