Embedded Systems & Firmware
Why does firmware on small microcontrollers often avoid dynamic memory allocation such as malloc?
- AMicrocontrollers cannot use any C functions
- BRAM is small, fragmentation can make allocations fail, and allocation time is hard to predict
- CDynamically allocated memory survives power-off
Show answer and explanation
Answer: B. RAM is small, fragmentation can make allocations fail, and allocation time is hard to predict
On a microcontroller with only a few KB to a few hundred KB of RAM, repeated allocation and release fragments free space, so a large allocation can fail even though the total is sufficient. In devices running for months, it may eventually fail. Allocation time is also not constant, which suits real-time work poorly. So designs often reserve all needed memory statically at the start.
- A. C functions are used normally.
- B. Correct. It is especially problematic for devices that run for a long time.
- C. It is lost when power is cut.