Bird
Raised Fist0
Embedded Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main characteristic of static memory allocation in embedded C?
easy
A. Memory size is fixed at compile time and cannot change during execution.
B. Memory is allocated dynamically during program execution.
C. Memory is allocated only when a function is called.
D. Memory size can grow or shrink based on input data.

Solution

  1. Step 1: Understand static memory allocation

    Static memory allocation means reserving memory before the program runs, with a fixed size.
  2. Step 2: Compare with dynamic allocation

    Dynamic allocation changes size during execution, but static does not.
  3. Final Answer:

    Memory size is fixed at compile time and cannot change during execution. -> Option A
  4. Quick Check:

    Static memory = fixed size [OK]
Hint: Static means fixed size known before running [OK]
Common Mistakes:
  • Confusing static with dynamic allocation
  • Thinking static memory can resize
  • Assuming static memory is allocated at runtime
2. Which of the following declarations uses static memory allocation correctly in embedded C?
easy
A. int *arr; arr = calloc(10, sizeof(int));
B. int *arr = malloc(10 * sizeof(int));
C. int arr[];
D. int arr[10];

Solution

  1. Step 1: Identify static allocation syntax

    Static arrays are declared with fixed size like int arr[10];
  2. Step 2: Recognize dynamic allocation

    malloc and calloc allocate memory dynamically at runtime, not static.
  3. Final Answer:

    int arr[10]; -> Option D
  4. Quick Check:

    Static array declaration = int arr[10]; [OK]
Hint: Static arrays use fixed size brackets [] [OK]
Common Mistakes:
  • Using malloc or calloc for static allocation
  • Not specifying array size for static arrays
  • Confusing pointer declarations with arrays
3. What will be the output of this code snippet in embedded C?
static int buffer[5] = {1, 2, 3, 4, 5};

void main() {
    for(int i = 0; i < 5; i++) {
        buffer[i] += 1;
        printf("%d ", buffer[i]);
    }
}
medium
A. Compilation error due to static keyword
B. 2 3 4 5 6
C. 0 1 2 3 4
D. 1 2 3 4 5

Solution

  1. Step 1: Understand static array initialization

    The static array buffer starts with values {1, 2, 3, 4, 5} fixed at compile time.
  2. Step 2: Trace the loop increment and print

    Each element is increased by 1, so values become {2, 3, 4, 5, 6} and printed.
  3. Final Answer:

    2 3 4 5 6 -> Option B
  4. Quick Check:

    Increment each element by 1 = 2 3 4 5 6 [OK]
Hint: Static arrays behave like normal arrays with fixed size [OK]
Common Mistakes:
  • Thinking static causes compile error
  • Forgetting to increment values before printing
  • Confusing static with uninitialized variables
4. Identify the error in this static memory allocation code snippet:
void main() {
    static int data[];
    data[0] = 10;
    printf("%d", data[0]);
}
medium
A. Cannot assign value to static array elements.
B. Static keyword cannot be used inside functions.
C. Array size is not specified for static array.
D. Missing return type for main function.

Solution

  1. Step 1: Check static array declaration

    Static arrays must have size or initializer; data[] without size or init is invalid.
  2. Step 2: Verify other code parts

    Static inside functions is allowed; assigning values and printing is valid; main return type is void (allowed in embedded C).
  3. Final Answer:

    Array size is not specified for static array. -> Option C
  4. Quick Check:

    Static array needs size or initializer [OK]
Hint: Static arrays need size or initializer [OK]
Common Mistakes:
  • Assuming static arrays can omit size without initializer
  • Thinking static is disallowed inside functions
  • Confusing main return type errors with static errors
5. You need to store sensor readings in a fixed-size buffer that must retain values between function calls in embedded C. Which static memory allocation pattern is best suited?
hard
A. Declare a static array inside the function to hold readings.
B. Use dynamic memory allocation with malloc inside the function.
C. Declare a local array inside the function without static keyword.
D. Use global variables without static keyword.

Solution

  1. Step 1: Understand requirement for retaining values

    Values must persist between calls, so memory must not be reinitialized each time.
  2. Step 2: Choose static array inside function

    Static array inside function keeps values between calls and has fixed size, perfect for sensor buffer.
  3. Step 3: Exclude other options

    Dynamic allocation is flexible but not static; local array resets each call; global without static may cause namespace issues.
  4. Final Answer:

    Declare a static array inside the function to hold readings. -> Option A
  5. Quick Check:

    Static inside function retains data between calls [OK]
Hint: Static inside function keeps data persistent [OK]
Common Mistakes:
  • Using local arrays that reset each call
  • Choosing dynamic allocation when fixed size needed
  • Ignoring static keyword for persistence
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard