0
0
Embedded Cprogramming~10 mins

Static memory allocation patterns in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Static memory allocation patterns
Start Program
Declare static/global variables
Memory allocated at compile time
Program runs using fixed memory
No dynamic allocation during runtime
Program ends, memory freed by OS/hardware
Static memory allocation reserves fixed memory at compile time for variables, used throughout program execution without change.
Execution Sample
Embedded C
int main() {
  static int count = 0;
  count = 5;
  return count;
}
This code declares a static variable 'count' with fixed memory allocated before runtime, assigns 5, and returns it.
Execution Table
StepActionVariableValueMemory Allocation
1Program startcount0 (default)Memory allocated at compile time
2Enter main()count0Memory already allocated
3Assign count = 5count5No new allocation, uses static memory
4Return countcount5Memory unchanged
5Program endcount5Memory freed by system
💡 Program ends, static memory is freed by system automatically
Variable Tracker
VariableStartAfter Step 3Final
count0 (default)55
Key Moments - 3 Insights
Why does 'count' keep its value between function calls?
Because 'count' is static, its memory is fixed and retains value throughout program execution as shown in execution_table steps 1 to 5.
Is memory allocated for 'count' during runtime?
No, memory for 'count' is allocated at compile time before the program runs, as shown in execution_table step 1.
Can static memory size change during program execution?
No, static memory size is fixed at compile time and does not change during runtime, unlike dynamic allocation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after step 3?
A5
B0
CUndefined
D10
💡 Hint
Check the 'Value' column for 'count' at step 3 in the execution_table.
At which step is the memory for 'count' allocated?
AStep 4
BStep 3
CStep 1
DStep 5
💡 Hint
Look at the 'Memory Allocation' column in execution_table to find when memory is allocated.
If 'count' was not static, what would change in the execution?
AValue of 'count' would persist between program runs
BMemory would be allocated at runtime inside main()
CMemory would be allocated at compile time
DProgram would not compile
💡 Hint
Static means compile-time allocation; without static, memory is allocated at runtime.
Concept Snapshot
Static memory allocation reserves fixed memory at compile time.
Variables keep their values throughout program execution.
Memory size and location do not change during runtime.
Used for static and global variables in embedded C.
No overhead of dynamic allocation or deallocation.
Full Transcript
Static memory allocation in embedded C means reserving memory for variables before the program runs. Variables declared as static or global get fixed memory at compile time. This memory stays allocated and retains values throughout the program's life. For example, a static int variable 'count' starts with default zero, then when assigned 5 in main(), it keeps that value until the program ends. No new memory is allocated during runtime for static variables. This makes static allocation predictable and efficient for embedded systems where memory is limited. The program ends and the system frees the memory automatically.