Skip to content
Tech Interview Prep
Technical interview guide

Embedded C/C++ & Memory Constraints

Why systems programming on bare metal with kilobytes of RAM surfaces correctness issues that rarely bite on a general-purpose OS.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: GCC current C/C++ implementation and optimization guidance; CMSIS 6 startup/linker/compiler control; Arm ABI and SEI CERT guidance reviewed 2026-09-04.

Overview

Curated: · Written: · Reviewed:

Embedded C/C++ & Memory Constraints

In one line

Bare-metal C/C++ runs freestanding, so the linked image, the target ABI and the startup code decide flash use, RAM use, timing and safety, not the source file. Reach for this framing whenever a question involves flash, RAM, stacks, ISRs or memory-mapped registers on a part with kilobytes of memory.

Key definitions

  • Freestanding: no hosted OS. Startup, termination, library support and memory layout come from the platform.
  • LMA / VMA: load address (in flash) and run address (in RAM) of an initialized section such as .data.
  • .bss: zero-initialized statics. Costs RAM, no flash; zeroed at startup.
  • volatile: each access is observable and kept in order relative to other volatile accesses. It gives no atomicity, no hardware barrier and no mutual exclusion.
  • Undefined behavior (UB): the standard places no constraint on the result, so the optimizer may assume it never happens.
  • Stack watermark: paint the stack with a known pattern at boot, then scan for the deepest overwritten word.

Formulas and complexity

flash=∣.text∣+∣.rodata∣+∣.data∣LMA\text{flash} = |.text| + |.rodata| + |.data|_{LMA} RAM=∣.data∣+∣.bss∣+stack+heap\text{RAM} = |.data| + |.bss| + \text{stack} + \text{heap}

Worked example from the STM32F411 map: 0x1F000 + 0xB400 + 0x400 = 174,080 bytes of flash, starting at 0x08000000. The .data initializer is stored in flash, so it counts there and again in RAM. Free-list allocation: worst-case time grows with the number of free blocks. Fixed-size pool: O(1) allocate and free. Allocation guard: reject count > SIZE_MAX / element_size before calling malloc.

How it works

Startup copies .data from its LMA to its VMA, zeroes .bss, runs the .init_array constructors, then calls main. A bootloader that jumps to Reset_Handler + 4 skips the first two steps, so .bss holds whatever flash erase left, 0xFF.

Reset vector Copy .data from flash LMA to RAM VMA Zero .bss Run .init_array constructors Call main Tasks and ISRs use stack and static storage

When to use and when not to

ChoiceUse whenAvoid when
Static storagesize is fixed at build timesize depends on input
Fixed-size poolmany identical objects, O(1) neededmixed sizes, unbounded growth
malloc / newinit phase only, with a defined failure pathany ISR or control loop
Exceptions / RTTIflash budget measured, with slack lefttight 64 KiB parts
volatilesingle writer and a naturally atomic widthmultiple writers, DMA buffers

Traps interviewers probe

  1. Trap: the debug build passed, so the design is fine. Fix: measure the signed release image. -O2 changes stack depth and how UB manifests.
  2. Trap: volatile makes it thread-safe. Fix: keep a single writer with an atomic access width, otherwise use C11 atomics or a critical section.
  3. Trap: unsigned subtraction is safe because it is defined. Fix: check header_len <= payload_len before subtracting, since a wrapped length is a valid-looking small value.
  4. Trap: the link fails on a struct copy I never wrote. Fix: a large aggregate assignment can lower to a memcpy call. Supply memcpy in the freestanding build, or keep copies small and bounded.
  5. Trap: *(float *)&buf[i] works at -O0. Fix: type-pun through memcpy. Strict aliasing lets the optimizer reorder the accesses, so the release build returns stale values.

Interview tips

Open with the artifact: "I would start with the map file and the release ELF, then the stack watermark."

Follow-ups to expect:

  • What changes at 85 °C? Flash wait states, leakage, crystal error and brownout threshold.
  • What happens on brownout mid-write? The boot slot and CRC must fail closed.
  • What is your allocation-failure policy? A defined behavior, never an ignored return.
  • How do you prove the binary is the one you tested? Build identity embedded in the image, with the toolchain and map archived alongside the ELF.

30-second outline: name the artifact (map, ELF, watermark). State the worst-case number and the budget it is checked against. Name the trap and its fix. Say how the claim is verified on the release image.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.