Static memory allocation patterns in Embedded C - Time & Space Complexity
Let's explore how the time cost behaves when using static memory allocation in embedded C.
We want to know how the program's running time changes as the size of statically allocated data grows.
Analyze the time complexity of the following code snippet.
#define SIZE 100
int data[SIZE];
void initialize() {
for (int i = 0; i < SIZE; i++) {
data[i] = i * 2;
}
}
This code statically allocates an array and fills it with values using a loop.
Look for repeated actions in the code.
- Primary operation: The for-loop that assigns values to each array element.
- How many times: Exactly SIZE times, once per element.
As the array size grows, the loop runs more times, doing more work.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The number of operations grows directly with the size of the array.
Time Complexity: O(n)
This means the time to initialize grows in a straight line as the array size increases.
[X] Wrong: "Static memory allocation means the program runs instantly regardless of size."
[OK] Correct: Even though memory is fixed at compile time, the program still needs to do work for each element during initialization, so time grows with size.
Understanding how loops over statically allocated arrays affect time helps you explain performance clearly and confidently in real projects.
"What if we replaced the for-loop with a function that initializes only half the array? How would the time complexity change?"
Practice
Solution
Step 1: Understand static memory allocation
Static memory allocation means reserving memory before the program runs, with a fixed size.Step 2: Compare with dynamic allocation
Dynamic allocation changes size during execution, but static does not.Final Answer:
Memory size is fixed at compile time and cannot change during execution. -> Option AQuick Check:
Static memory = fixed size [OK]
- Confusing static with dynamic allocation
- Thinking static memory can resize
- Assuming static memory is allocated at runtime
Solution
Step 1: Identify static allocation syntax
Static arrays are declared with fixed size like int arr[10];Step 2: Recognize dynamic allocation
malloc and calloc allocate memory dynamically at runtime, not static.Final Answer:
int arr[10]; -> Option DQuick Check:
Static array declaration = int arr[10]; [OK]
- Using malloc or calloc for static allocation
- Not specifying array size for static arrays
- Confusing pointer declarations with arrays
static int buffer[5] = {1, 2, 3, 4, 5};
void main() {
for(int i = 0; i < 5; i++) {
buffer[i] += 1;
printf("%d ", buffer[i]);
}
}Solution
Step 1: Understand static array initialization
The static array buffer starts with values {1, 2, 3, 4, 5} fixed at compile time.Step 2: Trace the loop increment and print
Each element is increased by 1, so values become {2, 3, 4, 5, 6} and printed.Final Answer:
2 3 4 5 6 -> Option BQuick Check:
Increment each element by 1 = 2 3 4 5 6 [OK]
- Thinking static causes compile error
- Forgetting to increment values before printing
- Confusing static with uninitialized variables
void main() {
static int data[];
data[0] = 10;
printf("%d", data[0]);
}Solution
Step 1: Check static array declaration
Static arrays must have size or initializer; data[] without size or init is invalid.Step 2: Verify other code parts
Static inside functions is allowed; assigning values and printing is valid; main return type is void (allowed in embedded C).Final Answer:
Array size is not specified for static array. -> Option CQuick Check:
Static array needs size or initializer [OK]
- Assuming static arrays can omit size without initializer
- Thinking static is disallowed inside functions
- Confusing main return type errors with static errors
Solution
Step 1: Understand requirement for retaining values
Values must persist between calls, so memory must not be reinitialized each time.Step 2: Choose static array inside function
Static array inside function keeps values between calls and has fixed size, perfect for sensor buffer.Step 3: Exclude other options
Dynamic allocation is flexible but not static; local array resets each call; global without static may cause namespace issues.Final Answer:
Declare a static array inside the function to hold readings. -> Option AQuick Check:
Static inside function retains data between calls [OK]
- Using local arrays that reset each call
- Choosing dynamic allocation when fixed size needed
- Ignoring static keyword for persistence
Ready to prove it?
Pass the test and this pattern moves unknown
