118. Heap-Free Polymorphic Driver

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

  • One integer value is read from standard input.

Driver selection rules:

  • If the input value is 0 → select the SPI driver
  • If the input value is 1 → select the I2C driver

Program flow:

  1. Read one integer value from input
  2. Create both driver objects on the stack
  3. Select the appropriate driver at runtime
  4. Store the selected driver using a base-class reference
  5. Invoke the driver function polymorphically

Output

  • If input is 0, output:

    SPI transfer started
    
  • If input is 1, output:

    I2C transfer started
    

Output Requirements

  • Exactly one line of output
  • Output text and spacing must match exactly
  • Output must be produced via a polymorphic call
  • No extra output or blank lines

Constraints

  • Language standard: C++11
  • Dynamic memory allocation is forbidden
    • Do NOT use new, delete, malloc, or free
  • Do NOT use STL containers
  • Do NOT change existing function names
  • Polymorphism must be preserved
  • Object lifetime must remain valid and deterministic

 

 

 

Loading...

Input

0

Expected Output

SPI transfer started