Common embedded bugs and fixes in Embedded C - Time & Space Complexity
When we look at common bugs in embedded C code, we want to understand how the time it takes to run the code changes as inputs or conditions change.
We ask: How do these bugs affect the speed or efficiency of the program?
Analyze the time complexity of the following code snippet.
void delay(int count) {
while(count-- > 0) {
// empty loop for delay
}
}
void process_data(int *data, int size) {
for(int i = 0; i < size; i++) { // bug fixed: changed <= to < to avoid off-by-one error
data[i] = data[i] * 2;
}
}
This code has a delay loop and a function that processes an array. The original off-by-one bug has been fixed to prevent unexpected behavior.
Look at the loops that repeat operations.
- Primary operation: The for-loop in
process_datathat multiplies each array element. - How many times: It runs
sizetimes, correctly processing each element once.
The loop runs exactly as many times as the size of the input array, ensuring no extra work or errors.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The loop runs exactly the intended number of times, preventing out-of-bounds access.
Time Complexity: O(n)
This means the time to run grows directly with the size of the input array, with no extra cost or errors.
[X] Wrong: "The loop runs exactly the number of elements in the array without error."
[OK] Correct: Because the loop condition originally used <= instead of <, it ran one extra time, causing out-of-bounds access and possible crashes.
Understanding how small bugs affect time and behavior shows your attention to detail and helps you write safer, more efficient embedded code.
"What if we changed the loop condition from <= size to < size? How would the time complexity and safety change?"
Practice
Solution
Step 1: Understand variable initialization
Uninitialized variables contain garbage values, causing unpredictable behavior.Step 2: Recognize the impact in embedded systems
Embedded systems rely on precise values; uninitialized variables can cause errors or crashes.Final Answer:
Using uninitialized variables before setting a value -> Option AQuick Check:
Uninitialized variables = unpredictable values [OK]
- Assuming variables start at zero automatically
- Ignoring compiler warnings about uninitialized variables
- Thinking only pointers need initialization
Solution
Step 1: Identify interrupt flag clearing method
Interrupt flags are cleared by setting them to 0 to indicate the interrupt is handled.Step 2: Understand why other options fail
Setting to 1 or incrementing does not clear the flag; negative values are invalid.Final Answer:
interrupt_flag = 0; -> Option CQuick Check:
Clear interrupt flag = 0 [OK]
- Setting flag to 1 instead of 0
- Incrementing flag instead of clearing
- Ignoring the need to clear flags causing repeated interrupts
volatile int flag = 0;
void ISR() {
flag = 1;
}
int main() {
while(flag == 0) {
// wait
}
return 0;
}Solution
Step 1: Understand volatile keyword usage
Volatile tells compiler the variable can change anytime, so it won't optimize the loop away.Step 2: Analyze program flow
Main waits in loop until ISR sets flag to 1, then loop ends and program exits.Final Answer:
Program waits until ISR sets flag to 1, then exits -> Option AQuick Check:
Volatile flag updated by ISR = loop ends [OK]
- Ignoring volatile causing infinite loop
- Assuming ISR runs automatically without setup
- Thinking volatile causes compile errors
int counter;
void setup() {
// No initialization of counter
}
void loop() {
counter++;
if(counter > 10) {
counter = 0;
}
}Solution
Step 1: Identify uninitialized variable usage
Counter is used without initialization, causing unpredictable start value.Step 2: Fix by initializing counter
Set counter = 0 in setup() to ensure predictable counting behavior.Final Answer:
Initialize counter to 0 in setup() -> Option BQuick Check:
Initialize variables before use = correct count [OK]
- Assuming global variables start at zero
- Using volatile without need
- Ignoring initialization causing wrong counts
volatile int sensor_value;
int safe_value;
void ISR() {
sensor_value = read_sensor();
}
int main() {
// What should be done here?
}Solution
Step 1: Understand volatile and interrupt safety
Volatile ensures compiler reads fresh value, but reading multi-byte data can be interrupted causing corruption.Step 2: Use interrupt disable-copy-enable pattern
Disable interrupts, copy sensor_value to safe_value, then enable interrupts to get consistent snapshot.Final Answer:
Disable interrupts, copy sensor_value to safe_value, enable interrupts -> Option DQuick Check:
Disable interrupts to safely copy volatile data [OK]
- Reading volatile multi-byte data without protection
- Making volatile variable non-volatile
- Using delay instead of disabling interrupts
Ready to prove it?
Pass the test and this pattern moves unknown
