0
0
Embedded Cprogramming~20 mins

Stack overflow detection in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Overflow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Detecting stack overflow with a guard variable

What is the output of this embedded C code snippet that uses a guard variable to detect stack overflow?

Embedded C
#include <stdio.h>

#define STACK_SIZE 5

int stack[STACK_SIZE];
int top = -1;
int guard = 0xDEADBEEF;

void push(int val) {
    if (top >= STACK_SIZE - 1) {
        printf("Stack overflow detected!\n");
        return;
    }
    stack[++top] = val;
}

int main() {
    for (int i = 0; i < STACK_SIZE; i++) {
        push(i);
    }
    // Overwrite guard to simulate overflow
    guard = 0xBADCAFE;
    if (guard != 0xDEADBEEF) {
        printf("Stack overflow detected by guard!\n");
    } else {
        printf("No overflow detected.\n");
    }
    return 0;
}
A
Stack overflow detected!
No overflow detected.
B
Stack overflow detected!
Stack overflow detected by guard!
CNo output
DStack overflow detected by guard!
Attempts:
2 left
💡 Hint

Look at how the guard variable is changed and checked after pushing elements.

🧠 Conceptual
intermediate
1:30remaining
Understanding stack overflow causes

Which of the following is the most common cause of stack overflow in embedded C programs?

AAllocating large arrays as local variables inside functions
BUsing too many global variables
CCalling functions with too many parameters passed by value
DUsing dynamic memory allocation with malloc()
Attempts:
2 left
💡 Hint

Think about what uses stack space directly.

🔧 Debug
advanced
2:30remaining
Identify the stack overflow bug

What is the cause of stack overflow in this recursive function?

Embedded C
void recurse(int count) {
    int buffer[1000];
    if (count == 0) return;
    recurse(count - 1);
}

int main() {
    recurse(1000);
    return 0;
}
AThe recursion depth is too shallow to cause overflow
BThe base case condition count == 0 is never reached
CThe large local array buffer[1000] in each recursive call causes stack overflow
DThe function recurse() is missing a return statement
Attempts:
2 left
💡 Hint

Consider how much stack space each call uses and how many calls happen.

📝 Syntax
advanced
1:30remaining
Stack overflow detection code syntax

Which option correctly declares a stack guard variable and checks it to detect overflow?

Embedded C
int stack_guard = 0xCAFEBABE;

void check_stack() {
    if (stack_guard != 0xCAFEBABE) {
        // Overflow detected
    }
}
A
int stack_guard = 0xCAFEBABE;
void check_stack() {
    if (stack_guard = 0xCAFEBABE) {
        // Overflow detected
    }
}
B
int stack_guard = 0xCAFEBABE;
void check_stack() {
    if (stack_guard != 0xCAFEBABE) {
        // Overflow detected
    }
}
C
int stack_guard = 0xCAFEBABE;
void check_stack() {
    if stack_guard != 0xCAFEBABE {
        // Overflow detected
    }
}
D
int stack_guard = 0xCAFEBABE
void check_stack() {
    if (stack_guard != 0xCAFEBABE) {
        // Overflow detected
    }
}
Attempts:
2 left
💡 Hint

Check for missing semicolons, correct if syntax, and comparison operators.

🚀 Application
expert
2:00remaining
Calculate maximum safe recursion depth

Given a stack size of 32 KB and each recursive call uses 512 bytes of stack, what is the maximum safe recursion depth to avoid stack overflow?

A64
B32
C128
D16
Attempts:
2 left
💡 Hint

Divide total stack size by stack used per call.