0
0
Embedded Cprogramming~10 mins

Stack overflow detection in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a stack pointer variable.

Embedded C
int [1] = -1;  // Stack pointer initialized
Drag options to blanks, or click blank then click option'
Astack_ptr
Btop
Csp
Dpointer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not descriptive of the stack pointer.
Initializing the pointer with a wrong value.
2fill in blank
medium

Complete the code to check if the stack is full.

Embedded C
if (top == [1] - 1) {
    // Stack is full
}
Drag options to blanks, or click blank then click option'
ASIZE
BMAX_SIZE
CSTACK_SIZE
DMAX_STACK
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong constant name for stack size.
Checking for equality with the wrong value.
3fill in blank
hard

Fix the error in the stack overflow detection condition.

Embedded C
if (top [1] STACK_SIZE) {
    // Overflow detected
}
Drag options to blanks, or click blank then click option'
A>=
B==
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which misses cases where top is greater than STACK_SIZE.
Using '<=' which is incorrect for overflow detection.
4fill in blank
hard

Fill both blanks to correctly update the stack pointer and detect overflow.

Embedded C
top [1] top + 1;
if (top [2] STACK_SIZE) {
    // Handle overflow
}
Drag options to blanks, or click blank then click option'
A=
B>=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '=' for assignment.
Using '>' instead of '>=' for overflow check.
5fill in blank
hard

Fill all three blanks to implement a safe push function with overflow detection.

Embedded C
void push(int stack[], int value) {
    if (top [1] STACK_SIZE - 1) {
        // Stack overflow error
        return;
    }
    top [2] top + 1;
    stack[[3]] = value;
}
Drag options to blanks, or click blank then click option'
A>=
B=
Ctop
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '>=' in overflow check.
Using '+' instead of '=' for increment.
Using wrong index for stack assignment.