0
0
Embedded Cprogramming~7 mins

Stack overflow detection in Embedded C

Choose your learning style9 modes available
Introduction

Stack overflow detection helps catch when a program uses more memory than the stack can hold. This prevents crashes and unexpected behavior.

When writing embedded programs with limited memory.
When debugging unexpected program crashes.
When you want to ensure your program does not overwrite important data.
When working with recursive functions that might use too much stack.
When developing safety-critical systems that must avoid memory errors.
Syntax
Embedded C
/* Example of stack overflow detection in embedded C */
#define STACK_SIZE 256

unsigned char stack[STACK_SIZE];
unsigned char *stack_pointer = stack;

// Initialize stack pointer to start of stack
void init_stack() {
    stack_pointer = stack;
}

// Push data onto stack
int push(unsigned char value) {
    if (stack_pointer - stack >= STACK_SIZE) {
        // Stack overflow detected
        return -1;
    }
    *stack_pointer = value;
    stack_pointer++;
    return 0;
}

// Pop data from stack
int pop(unsigned char *value) {
    if (stack_pointer == stack) {
        // Stack underflow
        return -1;
    }
    stack_pointer--;
    *value = *stack_pointer;
    return 0;
}

The stack is represented as an array with a fixed size.

The stack pointer points to the next free position in the stack.

Examples
When the stack is empty, the stack pointer points to the start of the stack array.
Embedded C
/* Example: Stack is empty */
init_stack();
// stack_pointer == stack, no overflow
After pushing one value, the stack pointer moves forward by one.
Embedded C
/* Example: Stack has one element */
init_stack();
push(42);
// stack_pointer points one byte past start
When trying to push more than the stack size, overflow is detected and push returns -1.
Embedded C
/* Example: Stack overflow detection */
init_stack();
for (int i = 0; i < STACK_SIZE + 1; i++) {
    if (push(i) == -1) {
        // Overflow detected when i == STACK_SIZE
        break;
    }
}
Trying to pop from an empty stack returns an error.
Embedded C
/* Example: Stack underflow detection */
init_stack();
unsigned char value;
if (pop(&value) == -1) {
    // Underflow detected because stack is empty
}
Sample Program

This program creates a small stack of size 5. It pushes five values, then tries to push one more to cause overflow. It prints the stack contents before and after. Then it pops all values and tries to pop once more to show underflow detection.

Embedded C
#include <stdio.h>

#define STACK_SIZE 5

unsigned char stack[STACK_SIZE];
unsigned char *stack_pointer = stack;

void init_stack() {
    stack_pointer = stack;
}

int push(unsigned char value) {
    if (stack_pointer - stack >= STACK_SIZE) {
        // Stack overflow detected
        return -1;
    }
    *stack_pointer = value;
    stack_pointer++;
    return 0;
}

int pop(unsigned char *value) {
    if (stack_pointer == stack) {
        // Stack underflow
        return -1;
    }
    stack_pointer--;
    *value = *stack_pointer;
    return 0;
}

void print_stack() {
    printf("Stack contents: ");
    for (unsigned char *ptr = stack; ptr < stack_pointer; ptr++) {
        printf("%d ", *ptr);
    }
    printf("\n");
}

int main() {
    init_stack();
    printf("Initial stack state:\n");
    print_stack();

    printf("Pushing values 10, 20, 30, 40, 50:\n");
    for (unsigned char value = 10; value <= 50; value += 10) {
        if (push(value) == -1) {
            printf("Stack overflow detected when pushing %d\n", value);
            break;
        }
    }
    print_stack();

    printf("Attempting to push 60 to cause overflow:\n");
    if (push(60) == -1) {
        printf("Stack overflow detected when pushing 60\n");
    }
    print_stack();

    printf("Popping all values:\n");
    unsigned char popped_value;
    while (pop(&popped_value) == 0) {
        printf("Popped: %d\n", popped_value);
    }
    printf("Stack is now empty.\n");

    printf("Attempting to pop from empty stack:\n");
    if (pop(&popped_value) == -1) {
        printf("Stack underflow detected.\n");
    }

    return 0;
}
OutputSuccess
Important Notes

Time complexity of push and pop operations is O(1) because they only move the pointer and read/write one value.

Space complexity is O(n) where n is the stack size, as the stack uses a fixed array.

A common mistake is not checking for overflow before pushing, which can overwrite memory and cause bugs.

Use stack overflow detection when you want to avoid memory corruption in embedded systems with limited stack size.

Summary

Stack overflow detection prevents writing beyond the stack's fixed size.

It is done by checking the stack pointer before pushing data.

Detecting overflow helps avoid crashes and data corruption in embedded programs.