Stack and heap are two ways your program stores data in memory. Knowing the difference helps you write better embedded programs that use memory safely and efficiently.
Stack vs heap in embedded context
/* Stack example: local variables inside a function */ void exampleFunction() { int localVariable = 10; // Stored on stack } /* Heap example: dynamic memory allocation */ #include <stdlib.h> void exampleHeap() { int *pointer = (int *)malloc(sizeof(int)); // Allocated on heap if (pointer != NULL) { *pointer = 20; free(pointer); // Free heap memory } }
The stack stores local variables and function calls. It grows and shrinks automatically.
The heap is used for dynamic memory you allocate and free manually using malloc and free.
void stackExample() {
int number = 5; // Stored on stack
}#include <stdlib.h> void heapExample() { int *pointer = (int *)malloc(sizeof(int)); if (pointer != NULL) { *pointer = 10; free(pointer); } }
void emptyStackFunction() {
// No local variables, stack usage is minimal
}#include <stdlib.h> void heapNullCheck() { int *pointer = (int *)malloc(0); // May return NULL or valid pointer if (pointer != NULL) { free(pointer); } }
This program shows how a variable is stored on the stack inside a function and how memory is allocated and freed on the heap. It prints values before and after to show the flow.
#include <stdio.h> #include <stdlib.h> void printStackExample() { int localVariable = 42; // Stored on stack printf("Stack variable value: %d\n", localVariable); } void printHeapExample() { int *heapVariable = (int *)malloc(sizeof(int)); // Allocate on heap if (heapVariable == NULL) { printf("Heap allocation failed\n"); return; } *heapVariable = 99; printf("Heap variable value: %d\n", *heapVariable); free(heapVariable); // Free heap memory } int main() { printf("Before stack example\n"); printStackExample(); printf("After stack example\n\n"); printf("Before heap example\n"); printHeapExample(); printf("After heap example\n"); return 0; }
Time complexity: Accessing stack variables is very fast (constant time). Heap allocation and freeing can be slower and may fragment memory.
Space complexity: Stack size is limited and fixed per program. Heap size depends on available memory and can grow or shrink.
Common mistake: Forgetting to free heap memory causes memory leaks, which is bad for embedded devices with limited memory.
Use stack for small, short-lived data. Use heap for large or variable-sized data that needs to live longer.
The stack stores temporary data like local variables and function calls.
The heap stores dynamic data you allocate and free manually.
In embedded systems, managing stack and heap carefully prevents crashes and saves memory.
Practice
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 AQuick Check:
Local variables = stack [OK]
- Confusing heap with stack for local variables
- Thinking local variables are stored in flash
- Assuming all variables are in CPU registers
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 CQuick Check:
malloc returns pointer, assign to pointer [OK]
- Assigning malloc result to non-pointer variable
- Using sizeof without malloc
- Incorrect function call syntax
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 AQuick Check:
Large local arrays risk stack overflow [OK]
- Assuming heap is used automatically
- Thinking large arrays go to flash
- Believing compiler auto-converts locals to globals
void func() {
int *p = malloc(10 * sizeof(int));
// ... use p ...
// missing free(p);
}
What is the main problem here?Solution
Step 1: Analyze malloc usage
Memory is allocated on the heap with malloc but never freed.Step 2: Identify consequences of missing free
Not freeing heap memory causes memory leak, wasting limited embedded memory.Final Answer:
Memory leak because allocated heap memory is not freed -> Option DQuick Check:
Missing free causes memory leak [OK]
- Confusing stack overflow with memory leak
- Thinking malloc syntax is wrong
- Assuming pointer is uninitialized
Solution
Step 1: Evaluate stack usage for large buffer
1KB local array uses half the stack, risking overflow in embedded systems.Step 2: Consider heap allocation safety
Allocating 1KB on heap with malloc is safer; heap is larger and managed manually.Step 3: Compare global and static arrays
Global/static arrays use fixed memory but reduce flexibility and increase RAM usage always.Final Answer:
Use malloc to allocate 1KB on the heap and free after use -> Option BQuick Check:
Heap allocation safer for large temporary buffers [OK]
- Using large local arrays risking stack overflow
- Ignoring freeing heap memory
- Assuming global arrays are always best
Ready to prove it?
Pass the test and this pattern moves unknown
