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?
const int flash_var = 10; int sram_var = 20; void main() { sram_var += flash_var; }
Remember that const variables are stored in Flash memory and can be read but not changed.
The flash_var is stored in Flash memory as a constant. It can be read and used in calculations. The sram_var is stored in SRAM and initially 20. Adding 10 from flash_var results in 30.
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?
int eeprom_var __attribute__((section(".eeprom"))) = 5; void main() { eeprom_var += 3; }
EEPROM memory keeps its data even when power is lost or the system resets.
EEPROM variables keep their values after reset. Since eeprom_var was incremented to 8 before reset, it remains 8 after reset.
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?
volatile int flag __attribute__((section(".eeprom"))) = 0; void main() { flag = 1; }
Think about the physical characteristics of EEPROM memory.
EEPROM has limited write cycles and slower write speed. Using a volatile variable that changes often in EEPROM can cause wear and slow performance.
Which of the following code snippets correctly places a variable in Flash memory on a microcontroller using GCC?
Constants are usually stored in Flash by default.
Declaring a variable as const usually places it in Flash memory automatically. The other options use incorrect or non-standard syntax.
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?
Calculate size by multiplying number of elements by size of int (2 bytes), then convert to KB (1024 bytes = 1 KB).
Flash (lookup_table): 1000 × 2 = 2000 bytes ≈ 2 KB
SRAM (runtime_buffer): 500 × 2 = 1000 bytes ≈ 1 KB
EEPROM (config_data): 100 × 2 = 200 bytes ≈ 0.2 KB
This matches option A.