0
0
Power-electronicsHow-ToBeginner · 4 min read

Common Embedded C Bugs and How to Fix Them

Common embedded C bugs include uninitialized variables, buffer overflows, and incorrect pointer use. Fix these by initializing variables, checking array bounds, and carefully managing pointers with clear logic and testing.
📐

Syntax

Embedded C code often uses pointers, arrays, and hardware registers. Proper syntax includes declaring variables, initializing them, and using control structures carefully.

Example parts:

  • int var = 0; declares and initializes an integer.
  • char buffer[10]; declares an array of 10 characters.
  • volatile keyword tells the compiler a variable can change unexpectedly (important for hardware registers).
c
volatile int *reg = (int *)0x40021000; // hardware register address
int buffer[10];
int i = 0;

for(i = 0; i < 10; i++) {
    buffer[i] = i;
}
💻

Example

This example shows safe initialization and array use to avoid common bugs like uninitialized variables and buffer overflow.

c
#include <stdio.h>

int main() {
    int buffer[5];
    int i;

    // Initialize buffer to zero
    for(i = 0; i < 5; i++) {
        buffer[i] = 0;
    }

    // Fill buffer safely
    for(i = 0; i < 5; i++) {
        buffer[i] = i * 2;
    }

    // Print buffer contents
    for(i = 0; i < 5; i++) {
        printf("buffer[%d] = %d\n", i, buffer[i]);
    }

    return 0;
}
Output
buffer[0] = 0 buffer[1] = 2 buffer[2] = 4 buffer[3] = 6 buffer[4] = 8
⚠️

Common Pitfalls

Common bugs in embedded C include:

  • Uninitialized variables: Using variables before setting a value causes unpredictable behavior.
  • Buffer overflow: Writing outside array bounds can corrupt memory and crash the system.
  • Incorrect pointer use: Dangling or null pointers cause crashes or data corruption.
  • Ignoring volatile keyword: Not marking hardware registers as volatile can cause compiler optimizations that break hardware access.

Fixes include always initializing variables, checking array limits, validating pointers before use, and using volatile for hardware registers.

c
#include <stdio.h>

// Wrong: buffer overflow example
void wrong_buffer() {
    int buffer[3];
    for(int i = 0; i < 4; i++) { // i < 4 causes overflow
        buffer[i] = i;
    }
}

// Right: safe buffer usage
void right_buffer() {
    int buffer[3];
    for(int i = 0; i < 3; i++) {
        buffer[i] = i;
    }
}

int main() {
    right_buffer();
    return 0;
}
📊

Quick Reference

  • Always initialize variables before use.
  • Check array indexes to prevent buffer overflow.
  • Use volatile for hardware registers.
  • Validate pointers before dereferencing.
  • Use static analysis tools to catch bugs early.

Key Takeaways

Always initialize variables to avoid unpredictable behavior.
Prevent buffer overflows by carefully checking array boundaries.
Use the volatile keyword for hardware-related variables.
Validate pointers before use to avoid crashes.
Test and review embedded code thoroughly to catch bugs early.