Microcontrollers use different memory types to store code and data. Knowing where things go helps your program run correctly and saves space.
Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C
/* Example memory sections in embedded C */ // Flash memory: stores program code const char program_code[] = "Hello"; // SRAM: stores variables during program run int runtime_variable = 10; // EEPROM: stores data that must persist after power off #include <avr/eeprom.h> uint8_t EEMEM saved_value = 5;
Flash is non-volatile memory where your program code lives.
SRAM is fast, temporary memory for variables while the program runs.
/* Flash stores program code */ const char greeting[] = "Hi!";
/* SRAM stores variables during run */ int counter = 0;
/* EEPROM stores persistent data */ #include <avr/eeprom.h> uint8_t EEMEM config_value = 42;
/* Edge case: empty EEPROM variable */ #include <avr/eeprom.h> uint8_t EEMEM empty_value;
This program shows how Flash stores the program string, SRAM holds a copy for use, and EEPROM keeps a counter that survives power off.
#include <avr/io.h> #include <avr/eeprom.h> #include <util/delay.h> #include <stdio.h> // Simulated Flash data const char flash_message[] = "Flash Memory"; // SRAM variable char sram_buffer[20]; // EEPROM variable uint8_t EEMEM eeprom_counter = 0; int main(void) { // Copy flash message to SRAM buffer for (int i = 0; i < sizeof(flash_message); i++) { sram_buffer[i] = flash_message[i]; } // Read EEPROM counter uint8_t counter = eeprom_read_byte(&eeprom_counter); // Increment counter and save back to EEPROM counter++; eeprom_write_byte(&eeprom_counter, counter); // Print values (simulated) printf("Flash message: %s\n", sram_buffer); printf("EEPROM counter: %d\n", counter); while (1) { _delay_ms(1000); } return 0; }
Time complexity: Accessing Flash and SRAM is fast; EEPROM writes are slower and limited in number.
Space complexity: Flash is large for code; SRAM is smaller for variables; EEPROM is very small for persistent data.
Common mistake: Trying to write to Flash or SRAM to save data permanently. Use EEPROM for that.
Use EEPROM only for data that must survive power loss, because writing to it is slower and wears out memory.
Flash memory stores your program code and is non-volatile.
SRAM is temporary memory for variables while the program runs.
EEPROM stores small amounts of data that must stay after power off.
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
