What if your robot forgets its own name every time you turn it off? Understanding memory layout can stop that from happening!
Why Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C? - Purpose & Use Cases
Imagine you are building a small robot with a microcontroller. You try to store all your program code, temporary data, and saved settings in one big box without organizing it. You mix everything together, and soon you run out of space or lose important data when the power goes off.
Without understanding the memory layout, you might overwrite your program code or lose saved settings because the microcontroller treats Flash, SRAM, and EEPROM differently. This causes bugs that are hard to find and fix. Manually managing memory like this is slow and error-prone.
Knowing the memory layout helps you put your program code in Flash (where it stays safe), use SRAM for temporary data while the microcontroller runs, and store important settings in EEPROM that keep data even when power is off. This clear separation makes your program reliable and efficient.
char settings[100]; // no clear memory use int data[50]; // mixed storage // risk of overwriting code or losing data
const char settings[100] __attribute__((section(".eeprom"))) = {0}; int data[50]; // stored in SRAM // program code in Flash automatically
Understanding memory layout lets you write programs that run smoothly, save important data safely, and use your microcontroller's resources wisely.
When you program a thermostat, the temperature settings must be saved in EEPROM so they stay after power off, the program code runs from Flash, and the current temperature reading is stored temporarily in SRAM.
Microcontrollers have different memory types for different purposes.
Flash stores program code permanently.
SRAM holds temporary data while running.
EEPROM saves data that must survive power loss.
Practice
Solution
Step 1: Understand memory types
Flash memory is non-volatile and stores the program code permanently.Step 2: Compare with other memories
SRAM is temporary for variables, EEPROM stores small data but not the main program.Final Answer:
Flash memory -> Option BQuick Check:
Program code = Flash memory [OK]
- Confusing SRAM with Flash memory
- Thinking EEPROM stores program code
- Assuming cache memory is used in microcontrollers
Solution
Step 1: Identify EEPROM section syntax
Variables placed in EEPROM use the section attribute ".eeprom" in embedded C.Step 2: Check other options
".sram" is for SRAM, ".flash" for Flash, ".stack" is not a valid section for variables.Final Answer:
int var __attribute__((section(".eeprom"))); -> Option AQuick Check:
EEPROM section attribute = .eeprom [OK]
- Using .sram or .flash instead of .eeprom
- Forgetting __attribute__ syntax
- Confusing stack with memory sections
const int x = 10;
int y = 20;
int main() {
static int z = 30;
return 0;
}
Where are the variables x, y, and z stored respectively?Solution
Step 1: Analyze variable storage rules
Constants likexare stored in Flash memory. Global variables likeygo to SRAM. Static variables likezalso reside in SRAM during runtime.Step 2: Match variables to memory types
x = Flash, y = SRAM, z = SRAM.Final Answer:
x in Flash, y in SRAM, z in SRAM -> Option DQuick Check:
const = Flash, global/static = SRAM [OK]
- Assuming static variables go to EEPROM
- Thinking const variables are in SRAM
- Mixing EEPROM with Flash for constants
int var __attribute__((section(".eeprom"))) = 5;
void main() {
var = 10;
}
What is the main problem with this code?Solution
Step 1: Understand EEPROM write limitations
EEPROM variables cannot be assigned new values by simple assignment at runtime; special functions are needed to write EEPROM.Step 2: Check other code aspects
The section attribute syntax is correct, variables need not be const, and main can be void in embedded C.Final Answer:
You cannot assign a new value to EEPROM variable at runtime like this -> Option AQuick Check:
EEPROM write needs special functions [OK]
- Assuming normal assignment writes EEPROM
- Thinking section attribute is wrong
- Confusing main return type with memory issues
Solution
Step 1: Identify memory needs
The value must persist after power off and be modifiable during runtime.Step 2: Match memory type to needs
EEPROM is non-volatile and supports runtime updates via special functions. Flash is non-volatile but rewriting is slower and more complex. SRAM is volatile and loses data on power off. Cache is not used for persistent storage.Final Answer:
EEPROM, because it retains data without power and allows runtime updates with special functions -> Option CQuick Check:
Persistent modifiable data = EEPROM [OK]
- Choosing Flash for frequent runtime updates
- Using SRAM for persistent storage
- Confusing cache with persistent memory
Ready to prove it?
Pass the test and this pattern moves unknown
