Bird
Raised Fist0
Embedded Cprogramming~20 mins

Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Embedded Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of variable storage in different memory sections

Consider this embedded C code snippet for a microcontroller:

const int flash_var = 10;
int sram_var = 20;
void main() {
    sram_var += flash_var;
    // What is the value of sram_var after this?
}

What is the value of sram_var after the addition?

Embedded C
const int flash_var = 10;
int sram_var = 20;
void main() {
    sram_var += flash_var;
}
A10
B30
C20
DCompiler error due to const variable
Attempts:
2 left
💡 Hint

Remember that const variables are stored in Flash memory and can be read but not changed.

❓ Predict Output
intermediate
2:00remaining
EEPROM variable persistence after reset

In embedded C, variables stored in EEPROM retain their values after a reset. Consider this code snippet:

int eeprom_var __attribute__((section(".eeprom"))) = 5;
void main() {
    eeprom_var += 3;
    // After a reset, what is the value of eeprom_var?
}

What will be the value of eeprom_var after the microcontroller resets?

Embedded C
int eeprom_var __attribute__((section(".eeprom"))) = 5;
void main() {
    eeprom_var += 3;
}
AUndefined behavior
B5
C0
D8
Attempts:
2 left
💡 Hint

EEPROM memory keeps its data even when power is lost or the system resets.

🔧 Debug
advanced
2:00remaining
Identify the memory section error

Look at this embedded C code snippet:

volatile int flag __attribute__((section(".eeprom"))) = 0;
void main() {
    flag = 1;
    // Some code
}

What is the main problem with placing a volatile int variable in EEPROM?

Embedded C
volatile int flag __attribute__((section(".eeprom"))) = 0;
void main() {
    flag = 1;
}
AWriting to EEPROM is slow and limited; frequent writes can wear it out
BVolatile variables cannot be stored in EEPROM
CEEPROM variables cannot be initialized
DThe compiler will optimize out the variable
Attempts:
2 left
💡 Hint

Think about the physical characteristics of EEPROM memory.

📝 Syntax
advanced
2:00remaining
Correct syntax to place variable in Flash memory

Which of the following code snippets correctly places a variable in Flash memory on a microcontroller using GCC?

Aint val __attribute__((flash)) = 100;
Bint val __flash = 100;
Cconst int val = 100; // automatically in Flash
Dconst int val __attribute__((section(".flash"))) = 100;
Attempts:
2 left
💡 Hint

Constants are usually stored in Flash by default.

🚀 Application
expert
3:00remaining
Memory usage calculation for embedded system

An embedded system has 32 KB Flash, 8 KB SRAM, and 1 KB EEPROM. You have the following variables:

  • const int lookup_table[1000]; // stored in Flash
  • int runtime_buffer[500]; // stored in SRAM
  • int config_data[100]; // stored in EEPROM

Assuming int is 2 bytes, how much memory (in KB) is used in each memory section?

AFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.2 KB
BFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.8 KB
CFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.4 KB
DFlash: 2 KB, SRAM: 1 KB, EEPROM: 0.1 KB
Attempts:
2 left
💡 Hint

Calculate size by multiplying number of elements by size of int (2 bytes), then convert to KB (1024 bytes = 1 KB).

Practice

(1/5)
1. Which type of memory in a microcontroller is used to store the program code permanently?
easy
A. SRAM
B. Flash memory
C. EEPROM
D. Cache memory

Solution

  1. Step 1: Understand memory types

    Flash memory is non-volatile and stores the program code permanently.
  2. Step 2: Compare with other memories

    SRAM is temporary for variables, EEPROM stores small data but not the main program.
  3. Final Answer:

    Flash memory -> Option B
  4. Quick Check:

    Program code = Flash memory [OK]
Hint: Program code is always in Flash memory [OK]
Common Mistakes:
  • Confusing SRAM with Flash memory
  • Thinking EEPROM stores program code
  • Assuming cache memory is used in microcontrollers
2. Which of the following declarations correctly places a variable in EEPROM memory in embedded C?
easy
A. int var __attribute__((section(".eeprom")));
B. int var __attribute__((section(".sram")));
C. int var __attribute__((section(".flash")));
D. int var __attribute__((section(".stack")));

Solution

  1. Step 1: Identify EEPROM section syntax

    Variables placed in EEPROM use the section attribute ".eeprom" in embedded C.
  2. Step 2: Check other options

    ".sram" is for SRAM, ".flash" for Flash, ".stack" is not a valid section for variables.
  3. Final Answer:

    int var __attribute__((section(".eeprom"))); -> Option A
  4. Quick Check:

    EEPROM section attribute = .eeprom [OK]
Hint: EEPROM variables use section ".eeprom" attribute [OK]
Common Mistakes:
  • Using .sram or .flash instead of .eeprom
  • Forgetting __attribute__ syntax
  • Confusing stack with memory sections
3. Consider this embedded C code snippet:
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?
medium
A. x in SRAM, y in EEPROM, z in Flash
B. x in EEPROM, y in Flash, z in EEPROM
C. x in Flash, y in EEPROM, z in SRAM
D. x in Flash, y in SRAM, z in SRAM

Solution

  1. Step 1: Analyze variable storage rules

    Constants like x are stored in Flash memory. Global variables like y go to SRAM. Static variables like z also reside in SRAM during runtime.
  2. Step 2: Match variables to memory types

    x = Flash, y = SRAM, z = SRAM.
  3. Final Answer:

    x in Flash, y in SRAM, z in SRAM -> Option D
  4. Quick Check:

    const = Flash, global/static = SRAM [OK]
Hint: const vars in Flash, globals and statics in SRAM [OK]
Common Mistakes:
  • Assuming static variables go to EEPROM
  • Thinking const variables are in SRAM
  • Mixing EEPROM with Flash for constants
4. This embedded C code tries to store a variable in EEPROM:
int var __attribute__((section(".eeprom"))) = 5;
void main() {
  var = 10;
}
What is the main problem with this code?
medium
A. You cannot assign a new value to EEPROM variable at runtime like this
B. The section attribute syntax is incorrect
C. Variables in EEPROM must be declared as const
D. The main function must return int, not void

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    You cannot assign a new value to EEPROM variable at runtime like this -> Option A
  4. Quick Check:

    EEPROM write needs special functions [OK]
Hint: EEPROM writes need special functions, not simple assignment [OK]
Common Mistakes:
  • Assuming normal assignment writes EEPROM
  • Thinking section attribute is wrong
  • Confusing main return type with memory issues
5. You want to store a configuration value that must persist after power off and be modifiable during runtime on a microcontroller. Which memory type should you use and why?
hard
A. Cache memory, because it stores frequently used data
B. Flash, because it is fast and can be rewritten easily during runtime
C. EEPROM, because it retains data without power and allows runtime updates with special functions
D. SRAM, because it is volatile and fast for temporary storage

Solution

  1. Step 1: Identify memory needs

    The value must persist after power off and be modifiable during runtime.
  2. 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.
  3. Final Answer:

    EEPROM, because it retains data without power and allows runtime updates with special functions -> Option C
  4. Quick Check:

    Persistent modifiable data = EEPROM [OK]
Hint: Use EEPROM for persistent, modifiable data [OK]
Common Mistakes:
  • Choosing Flash for frequent runtime updates
  • Using SRAM for persistent storage
  • Confusing cache with persistent memory
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard