Embedded Systems & Firmware

Why does firmware on small microcontrollers often avoid dynamic memory allocation such as malloc?

  1. AMicrocontrollers cannot use any C functions
  2. BRAM is small, fragmentation can make allocations fail, and allocation time is hard to predict
  3. 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.
Practice this topic Back to topic

More Embedded Systems & Firmware questions

  1. On a small microcontroller without an MMU, what should be kept in mind about excessive stack use?
  2. A 12-bit A/D converter with a 3.3 V reference reads a value of 2048. What is the closest input voltage?
  3. When a microcontroller captures many A/D conversion results at regular intervals, what is the advantage of using DMA?
  4. When representing peripheral registers with C bit-field structs, what needs care?
  5. On many microcontrollers, what is the advantage of declaring a large table that never changes (such as font data) as const?