Embedded Systems & Firmware
On many microcontrollers, what is the advantage of declaring a large table that never changes (such as font data) as const?
- AIt is placed in flash memory and does not consume scarce RAM
- BThe table contents are automatically compressed
- CThe table contents become freely writable at run time
Show answer and explanation
Answer: A. It is placed in flash memory and does not consume scarce RAM
Microcontrollers often have hundreds of KB of flash for programs but only tens of KB of RAM. A non-const initialized array goes in .data and is copied from flash to RAM at start-up, consuming RAM. Declaring it const lets it stay in flash as read-only data and be read from there. Some microcontrollers require a special attribute to place data in flash.
- A. Correct. RAM is often much smaller than flash.
- B. Nothing is compressed.
- C. const means it will not be modified.