The stack is a special memory area that stores local variables, function parameters, and return addresses. It works like a stack of plates: last in, first out.
Click to reveal answer
beginner
What is the heap in embedded systems?
The heap is a memory area used for dynamic memory allocation. It allows programs to request and release memory during runtime.
Click to reveal answer
intermediate
How does stack memory allocation differ from heap allocation?
Stack allocation is automatic and fast, with fixed size per function call. Heap allocation is manual, slower, and can fragment memory over time.
Click to reveal answer
intermediate
Why is stack size usually limited in embedded systems?
Embedded systems have limited RAM, so stack size is small to save memory. Large stacks can cause overflow and crash the system.
Click to reveal answer
advanced
What risks come with using heap memory in embedded systems?
Heap use can cause fragmentation, unpredictable delays, and memory leaks, which are risky in real-time embedded systems.
Click to reveal answer
Which memory area stores local variables in embedded C?
AROM
BHeap
CFlash
DStack
✗ Incorrect
Local variables are stored on the stack, which manages function calls and local data.
What is a key characteristic of heap memory?
ADynamic size and manual allocation
BFixed size and automatic allocation
CRead-only memory
DStores CPU registers
✗ Incorrect
Heap memory allows dynamic allocation and deallocation during program execution.
Why should heap usage be minimized in embedded systems?
AHeap is faster than stack
BHeap is read-only
CHeap causes fragmentation and unpredictable delays
DHeap stores only constants
✗ Incorrect
Heap fragmentation and delays can cause problems in real-time embedded systems.
What happens if the stack overflows in an embedded system?
ASystem may crash or behave unpredictably
BProgram runs faster
CHeap memory increases
DCPU temperature decreases
✗ Incorrect
Stack overflow can overwrite important data and cause system crashes.
Which memory area grows upwards in most embedded systems?
AStack
BHeap
CFlash
DROM
✗ Incorrect
Heap usually grows upwards, while stack grows downwards in memory.
Explain the differences between stack and heap memory in embedded systems.
Think about how memory is used during function calls versus dynamic requests.
You got /6 concepts.
Describe the risks of using heap memory in real-time embedded applications.
Consider what happens if memory is not managed carefully during runtime.
You got /4 concepts.
Practice
(1/5)
1. In embedded C, where are local variables inside functions typically stored?
easy
A. On the stack
B. On the heap
C. In flash memory
D. In CPU registers only
Solution
Step 1: Understand local variable storage
Local variables inside functions are temporary and exist only during function execution.
Step 2: Identify stack usage in embedded C
The stack is used to store temporary data like local variables and return addresses.
Final Answer:
On the stack -> Option A
Quick Check:
Local variables = stack [OK]
Hint: Local variables go on stack, dynamic memory on heap [OK]
Common Mistakes:
Confusing heap with stack for local variables
Thinking local variables are stored in flash
Assuming all variables are in CPU registers
2. Which of the following is the correct way to allocate memory dynamically in embedded C?
easy
A. int p = malloc(sizeof(int));
B. int *p = sizeof(int);
C. int *p = malloc(sizeof(int));
D. malloc(int p);
Solution
Step 1: Recall malloc syntax
malloc returns a pointer to allocated memory, so pointer variable is needed.
Step 2: Check each option
int *p = malloc(sizeof(int)); correctly assigns malloc's return to an int pointer. Others misuse types or syntax.
Final Answer:
int *p = malloc(sizeof(int)); -> Option C
Quick Check:
malloc returns pointer, assign to pointer [OK]
Hint: malloc returns pointer, assign to pointer variable [OK]
Common Mistakes:
Assigning malloc result to non-pointer variable
Using sizeof without malloc
Incorrect function call syntax
3. What will happen if you declare a large array inside a function in embedded C on a system with limited stack size?
medium
A. Stack overflow may occur causing a crash
B. Heap memory will be used automatically
C. The array will be stored in flash memory
D. The compiler will convert it to a global variable
Solution
Step 1: Understand stack size limits
Stack size is limited in embedded systems; large local arrays consume stack space.
Step 2: Consequence of large stack allocation
Allocating large arrays locally can exceed stack size, causing overflow and crash.
Final Answer:
Stack overflow may occur causing a crash -> Option A
Quick Check:
Large local arrays risk stack overflow [OK]
Hint: Large local arrays risk stack overflow in embedded [OK]
Common Mistakes:
Assuming heap is used automatically
Thinking large arrays go to flash
Believing compiler auto-converts locals to globals
4. Consider this code snippet in embedded C:
void func() {
int *p = malloc(10 * sizeof(int));
// ... use p ...
// missing free(p);
}
What is the main problem here?
medium
A. Pointer p is not initialized
B. Stack overflow due to large local array
C. Syntax error in malloc usage
D. Memory leak because allocated heap memory is not freed
Solution
Step 1: Analyze malloc usage
Memory is allocated on the heap with malloc but never freed.