Bird
Raised Fist0
FreeRTOSprogramming~20 mins

Static vs dynamic allocation (configSUPPORT_STATIC_ALLOCATION) in FreeRTOS - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Static vs Dynamic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS task creation code?

Consider this FreeRTOS code snippet where configSUPPORT_STATIC_ALLOCATION is set to 1. What will be the output when the task is created?

FreeRTOS
StaticTask_t xTaskBuffer;
StackType_t xStack[100];

void vTaskCode(void * pvParameters) {
    for(;;) {}
}

int main() {
    TaskHandle_t xHandle = xTaskCreateStatic(
        vTaskCode,          // Task function
        "Task1",           // Name
        100,                // Stack size
        NULL,               // Parameters
        1,                  // Priority
        xStack,             // Stack buffer
        &xTaskBuffer        // Task buffer
    );
    if (xHandle != NULL) {
        printf("Task created successfully\n");
    } else {
        printf("Task creation failed\n");
    }
    return 0;
}
ATask created successfully
BTask creation failed
CCompilation error due to missing heap
DRuntime error due to NULL stack pointer
Attempts:
2 left
💡 Hint

Static allocation requires providing buffers explicitly. Check if buffers are correctly passed.

❓ Predict Output
intermediate
2:00remaining
What happens if configSUPPORT_STATIC_ALLOCATION is 0 and xTaskCreateStatic is called?

Assuming configSUPPORT_STATIC_ALLOCATION is set to 0 in FreeRTOSConfig.h, what will happen if the following code is run?

FreeRTOS
StaticTask_t xTaskBuffer;
StackType_t xStack[100];

TaskHandle_t xHandle = xTaskCreateStatic(
    vTaskCode,
    "Task1",
    100,
    NULL,
    1,
    xStack,
    &xTaskBuffer
);
ACompilation error because xTaskCreateStatic is undefined
BTask is created successfully using static buffers
CRuntime error due to NULL heap
DTask is created dynamically ignoring static buffers
Attempts:
2 left
💡 Hint

Check if the static allocation API is available when the config option is disabled.

🔧 Debug
advanced
3:00remaining
Why does this statically allocated task crash at runtime?

Given this FreeRTOS task creation code with static allocation enabled, the system crashes at runtime. Identify the cause.

FreeRTOS
StaticTask_t *pxTaskBuffer;
StackType_t *pxStackBuffer;

void createTask() {
    TaskHandle_t xHandle = xTaskCreateStatic(
        vTaskCode,
        "TaskCrash",
        100,
        NULL,
        1,
        pxStackBuffer,
        pxTaskBuffer
    );
    if (xHandle == NULL) {
        printf("Task creation failed\n");
    }
}

int main() {
    createTask();
    vTaskStartScheduler();
    return 0;
}
ATask priority is invalid causing scheduler failure
BStack size is too small causing overflow
CpxTaskBuffer and pxStackBuffer are uninitialized pointers causing invalid memory access
DvTaskStartScheduler is called before task creation
Attempts:
2 left
💡 Hint

Check if the buffers passed to xTaskCreateStatic are valid memory locations.

🧠 Conceptual
advanced
2:00remaining
Which statement about static vs dynamic allocation in FreeRTOS is TRUE?

Choose the correct statement regarding static and dynamic allocation in FreeRTOS when configSUPPORT_STATIC_ALLOCATION is enabled.

ADynamic allocation is only possible if static allocation is disabled
BStatic allocation requires the user to provide memory buffers for task control blocks and stacks
CStatic allocation automatically allocates memory from the heap at runtime
DDynamic allocation requires the user to provide buffers explicitly
Attempts:
2 left
💡 Hint

Think about who manages memory in static vs dynamic allocation.

❓ Predict Output
expert
3:00remaining
What is the number of items in the task list after these mixed allocations?

Assuming configSUPPORT_STATIC_ALLOCATION is 1 and configSUPPORT_DYNAMIC_ALLOCATION is 1, consider this code:

TaskHandle_t h1, h2;

StaticTask_t xTaskBuffer;
StackType_t xStack[50];

h1 = xTaskCreateStatic(vTaskCode, "StaticTask", 50, NULL, 1, xStack, &xTaskBuffer);
h2 = NULL;
xTaskCreate(vTaskCode, "DynamicTask", 50, NULL, 1, &h2);

// After both tasks are created, how many tasks exist in the scheduler?
A3
B1
C0
D2
Attempts:
2 left
💡 Hint

Both static and dynamic task creation APIs add tasks to the scheduler.

Practice

(1/5)
1. What does enabling configSUPPORT_STATIC_ALLOCATION in FreeRTOS do?
easy
A. Disables task creation completely.
B. Forces all tasks to use dynamic memory allocation.
C. Automatically increases heap size at runtime.
D. Allows tasks to be created using user-provided fixed memory buffers.

Solution

  1. Step 1: Understand configSUPPORT_STATIC_ALLOCATION purpose

    This option enables static allocation, meaning the user provides memory buffers for tasks instead of using heap.
  2. Step 2: Compare with other options

    The other options describe disabling tasks, heap size changes, or forcing dynamic allocation, which are incorrect.
  3. Final Answer:

    Allows tasks to be created using user-provided fixed memory buffers. -> Option D
  4. Quick Check:

    Static allocation = user buffers [OK]
Hint: Static allocation means you provide memory buffers [OK]
Common Mistakes:
  • Confusing static with dynamic allocation
  • Thinking it disables task creation
  • Assuming it changes heap size automatically
2. Which of the following is the correct way to declare a task using static allocation in FreeRTOS?
easy
A. xTaskCreateStatic(TaskFunction, "Task", 100, NULL, 1, stackBuffer, &taskBuffer);
B. xTaskCreate(TaskFunction, "Task", 100, NULL, 1, NULL);
C. xTaskCreateDynamic(TaskFunction, "Task", 100, NULL, 1, &taskHandle);
D. xTaskCreateStatic(TaskFunction, "Task", 100, NULL, 1, NULL, NULL);

Solution

  1. Step 1: Identify static allocation function

    FreeRTOS provides xTaskCreateStatic for static allocation, requiring stack and task buffers.
  2. Step 2: Check parameters

    xTaskCreateStatic(TaskFunction, "Task", 100, NULL, 1, stackBuffer, &taskBuffer); correctly passes stackBuffer and taskBuffer pointers. xTaskCreate uses dynamic allocation. There is no xTaskCreateDynamic function. Passing NULL buffers is incorrect.
  3. Final Answer:

    xTaskCreateStatic(TaskFunction, "Task", 100, NULL, 1, stackBuffer, &taskBuffer); -> Option A
  4. Quick Check:

    Static task creation needs buffers [OK]
Hint: Use xTaskCreateStatic with buffers for static tasks [OK]
Common Mistakes:
  • Using xTaskCreate instead of xTaskCreateStatic
  • Passing NULL for buffers in static allocation
  • Confusing dynamic and static creation functions
3. Given the following code snippet with configSUPPORT_STATIC_ALLOCATION enabled, what will be the output?
StaticTask_t taskBuffer;
StackType_t stackBuffer[128];

void TaskFunction(void *params) {
  printf("Task running\n");
  vTaskDelete(NULL);
}

int main() {
  TaskHandle_t taskHandle = xTaskCreateStatic(TaskFunction, "MyTask", 128, NULL, 1, stackBuffer, &taskBuffer);
  vTaskStartScheduler();
  return 0;
}
medium
A. Compilation error due to missing heap
B. Task running
C. No output, scheduler never starts
D. Runtime error due to NULL stack

Solution

  1. Step 1: Analyze static task creation

    The task is created statically with valid stack and task buffers, so it will run correctly.
  2. Step 2: Check scheduler and task behavior

    The scheduler starts, runs the task which prints "Task running", then deletes itself. No errors expected.
  3. Final Answer:

    Task running -> Option B
  4. Quick Check:

    Static task runs and prints output [OK]
Hint: Static tasks run if buffers are valid and scheduler starts [OK]
Common Mistakes:
  • Assuming heap is required for static tasks
  • Thinking scheduler won't start without dynamic allocation
  • Confusing NULL stack with valid buffer
4. Identify the error in this static task creation code snippet:
StaticTask_t taskBuffer;
StackType_t stackBuffer[100];

xTaskCreateStatic(TaskFunction, "Task", 100, NULL, 1, NULL, &taskBuffer);
medium
A. Function name is incorrect; should be xTaskCreate.
B. Task buffer pointer is NULL, causing invalid task control block.
C. Stack buffer pointer is NULL, causing invalid stack memory.
D. Task priority is invalid (should be zero).

Solution

  1. Step 1: Check parameters passed to xTaskCreateStatic

    The stack buffer pointer is NULL, which means no memory is provided for the task's stack.
  2. Step 2: Understand consequences of NULL stack buffer

    Without a valid stack buffer, the task cannot run properly and will cause errors.
  3. Final Answer:

    Stack buffer pointer is NULL, causing invalid stack memory. -> Option C
  4. Quick Check:

    Stack buffer must not be NULL [OK]
Hint: Stack buffer pointer must be valid, not NULL [OK]
Common Mistakes:
  • Passing NULL for stack buffer
  • Confusing task buffer with stack buffer
  • Incorrect function name usage
5. You want to create a FreeRTOS task that must never fail due to memory fragmentation or heap exhaustion. Which approach is best and why?
hard
A. Use static allocation with user-provided buffers for predictability.
B. Mix static and dynamic allocation to balance memory usage.
C. Use dynamic allocation with a large heap to avoid failures.
D. Disable task deletion to prevent memory leaks.

Solution

  1. Step 1: Understand memory failure causes in FreeRTOS

    Dynamic allocation can fail due to heap fragmentation or exhaustion, causing task creation failure.
  2. Step 2: Evaluate static allocation benefits

    Static allocation uses fixed buffers provided by the user, ensuring memory is reserved and predictable, preventing allocation failures.
  3. Step 3: Compare other options

    Using dynamic allocation with a large heap still risks fragmentation failures; mixing allocation methods adds unnecessary complexity; disabling task deletion is unrelated to allocation issues.
  4. Final Answer:

    Use static allocation with user-provided buffers for predictability. -> Option A
  5. Quick Check:

    Static allocation prevents heap failures [OK]
Hint: Static allocation avoids heap fragmentation failures [OK]
Common Mistakes:
  • Assuming large heap solves all allocation issues
  • Mixing allocation methods without clear reason
  • Confusing task deletion with allocation safety
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