📖 Scenario: In embedded systems, memory is limited and managing it well is very important. Two main areas where memory is used are the stack and the heap. The stack is used for temporary data like function variables, and the heap is used for dynamic memory allocation.Imagine you are programming a small embedded device that needs to store some data temporarily and also allocate memory dynamically for a buffer.
🎯 Goal: You will write a simple embedded C program that shows how to use the stack and the heap. You will create a local variable on the stack and allocate memory on the heap using malloc. Then you will print their addresses to see the difference.
📋 What You'll Learn
Create a local variable on the stack
Allocate memory dynamically on the heap using malloc
Print the addresses of the stack variable and the heap memory
Understand the difference in memory locations
💡 Why This Matters
🌍 Real World
Embedded devices like sensors and controllers have limited memory. Understanding stack and heap helps write efficient and safe programs.
💼 Career
Embedded software engineers must manage memory carefully to avoid crashes and bugs in devices like medical equipment, automotive systems, and IoT gadgets.
Progress0 / 4 steps
1
Create a local variable on the stack
Write a line of code to declare an integer variable called stack_var and set it to 10 inside the main function.
Embedded C
Hint
Use int stack_var = 10; inside main.
2
Allocate memory dynamically on the heap
Add a line of code to allocate memory for an integer pointer called heap_var using malloc. Allocate space for one integer.
Embedded C
Hint
Use int *heap_var = (int *)malloc(sizeof(int)); to allocate memory on the heap.
3
Store a value in the heap memory
Add a line of code to store the value 20 in the memory pointed to by heap_var.
Embedded C
Hint
Use *heap_var = 20; to store the value in heap memory.
4
Print the addresses of stack and heap variables
Add two printf lines to print the addresses of stack_var and heap_var using %p. Then free the allocated heap memory.
Embedded C
Hint
Use printf("Address of stack_var: %p\n", (void *)&stack_var); and printf("Address of heap_var: %p\n", (void *)heap_var);. Don't forget to free(heap_var);.
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.