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
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.
When to use and when not to
| Choice | Use when | Avoid when |
|---|---|---|
| Static storage | size is fixed at build time | size depends on input |
| Fixed-size pool | many identical objects, O(1) needed | mixed sizes, unbounded growth |
malloc / new | init phase only, with a defined failure path | any ISR or control loop |
| Exceptions / RTTI | flash budget measured, with slack left | tight 64 KiB parts |
volatile | single writer and a naturally atomic width | multiple writers, DMA buffers |
Traps interviewers probe
- Trap: the debug build passed, so the design is fine. Fix: measure the signed release image.
-O2changes stack depth and how UB manifests. - Trap:
volatilemakes it thread-safe. Fix: keep a single writer with an atomic access width, otherwise use C11 atomics or a critical section. - Trap: unsigned subtraction is safe because it is defined. Fix: check
header_len <= payload_lenbefore subtracting, since a wrapped length is a valid-looking small value. - Trap: the link fails on a struct copy I never wrote. Fix: a large aggregate assignment can lower to a
memcpycall. Supplymemcpyin the freestanding build, or keep copies small and bounded. - Trap:
*(float *)&buf[i]works at-O0. Fix: type-pun throughmemcpy. 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.