0
0
Embedded Cprogramming~20 mins

Static memory allocation patterns in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of static variable initialization
What is the output of this C code snippet using static variables?
Embedded C
#include <stdio.h>

void func() {
    static int count = 0;
    count++;
    printf("%d ", count);
}

int main() {
    for(int i = 0; i < 3; i++) {
        func();
    }
    return 0;
}
A3 3 3
B1 2 3
C1 1 1
D0 1 2
Attempts:
2 left
💡 Hint
Remember that static variables keep their value between function calls.
Predict Output
intermediate
2:00remaining
Static array initialization and modification
What will be the output of this code that uses a static array inside a function?
Embedded C
#include <stdio.h>

void updateArray() {
    static int arr[3] = {1, 2, 3};
    arr[0] += 1;
    arr[1] += 1;
    arr[2] += 1;
    for(int i = 0; i < 3; i++) {
        printf("%d ", arr[i]);
    }
}

int main() {
    updateArray();
    updateArray();
    return 0;
}
A2 3 4 3 4 5
B2 3 4 2 3 4
C1 2 3 2 3 4
D1 2 3 1 2 3
Attempts:
2 left
💡 Hint
Static arrays keep their values between function calls and are modified each time.
🔧 Debug
advanced
2:00remaining
Identify the error in static pointer usage
What error will this code produce when compiled or run?
Embedded C
#include <stdio.h>

void func() {
    static char *ptr;
    char buffer[10] = "hello";
    ptr = buffer;
    printf("%s\n", ptr);
}

int main() {
    func();
    func();
    return 0;
}
AUndefined behavior due to pointer to local variable used after function ends
BPrints 'hello' twice without error
CCompilation error: cannot assign local array to static pointer
DRuntime error: segmentation fault on second call
Attempts:
2 left
💡 Hint
Think about the lifetime of local variables and static pointers.
📝 Syntax
advanced
1:30remaining
Static variable declaration syntax
Which option correctly declares a static variable inside a function that retains its value between calls?
Astatic count int = 0;
Bint static count = 0;
Cstatic int count = 0;
Dint count static = 0;
Attempts:
2 left
💡 Hint
The 'static' keyword comes before the type in C declarations.
🚀 Application
expert
3:00remaining
Static memory allocation for a fixed-size buffer
You want to create a function that returns a pointer to a fixed-size buffer that retains its content between calls. Which code snippet correctly implements this using static memory allocation?
A
char* getBuffer() {
    char *buffer = malloc(20);
    return buffer;
}
B
char* getBuffer() {
    char buffer[20];
    return buffer;
}
C
char* getBuffer() {
    static char *buffer = malloc(20);
    return buffer;
}
D
char* getBuffer() {
    static char buffer[20];
    return buffer;
}
Attempts:
2 left
💡 Hint
Static arrays inside functions keep their memory allocated between calls without dynamic allocation.