Explain the memory layout of a typical microcontroller program: .text, .data, .bss, stack and heap.
What the interviewer wants to hear
- .text holds code and constants in flash
- .data holds initialised globals, copied from flash to RAM at startup
- .bss holds zero-initialised globals, cleared at startup
- Stack for local variables and calls; heap for dynamic allocation
- Linker script defines regions; map file shows usage
Model answer
The .text section holds the program code and usually read-only constants (.rodata), and lives in flash. The .data section holds global and static variables with non-zero initial values; their initial values are stored in flash, and the startup code copies them into RAM before main runs. The .bss section holds globals that start at zero; it takes no flash space, and the startup code just clears that RAM region. The stack holds local variables, function return addresses and saved registers, and on Cortex-M it grows down from the top of RAM, its start address being the first entry in the vector table. The heap, if used, sits between .bss and the stack and serves malloc. In an RTOS, each task also has its own stack. The linker script defines these regions and their addresses, and the map file shows how much each uses, which I check to avoid running out of RAM or flash.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Embedded Systems Engineer questions
- How would you write a driver for a new I2C temperature sensor?
- Explain the difference between a mutex and a semaphore in an RTOS, with examples.
- A device freezes randomly once every few days in the field. How do you debug it?
- When would you use fixed-point arithmetic instead of floating point on a microcontroller?
- What is DMA, when would you use it, and what pitfalls do you watch for?
- Design firmware that receives text commands over UART, like "SET LED 1", and responds reliably.
- Explain how the CAN bus works and why it is popular in automotive and industrial systems.
- Your firmware is close to the flash and RAM limits of the MCU. How do you reduce memory usage?
- How would you design a watchdog strategy for firmware running several RTOS tasks?