Bird
Raised Fist0
FreeRTOSprogramming~5 mins

Stack size allocation in FreeRTOS

Choose your learning style10 modes available
Introduction

Stack size allocation decides how much memory a task can use for its temporary data. It helps the system run tasks safely without running out of memory.

When creating a new task in FreeRTOS and you need to set how much stack memory it uses.
When a task crashes or behaves strangely due to running out of stack space.
When optimizing memory usage by giving tasks only the stack size they need.
When debugging stack overflow issues in embedded systems.
When porting FreeRTOS to a new hardware platform and setting default stack sizes.
Syntax
FreeRTOS
xTaskCreate(
    TaskFunction_t pvTaskCode,
    const char * const pcName,
    configSTACK_DEPTH_TYPE usStackDepth,
    void *pvParameters,
    UBaseType_t uxPriority,
    TaskHandle_t *pxCreatedTask
);

usStackDepth is the stack size in words, not bytes. For example, on a 32-bit system, 100 means 400 bytes.

Stack size must be enough for the task's needs, including function calls and local variables.

Examples
Creates a task with 100 words of stack. This is a normal case.
FreeRTOS
xTaskCreate(
    vTaskFunction,
    "Task1",
    100,  // Stack size of 100 words
    NULL,
    1,
    NULL
);
Stack size zero is invalid and will cause the task creation to fail.
FreeRTOS
xTaskCreate(
    vTaskFunction,
    "EmptyStack",
    0,  // Stack size zero (invalid)
    NULL,
    1,
    NULL
);
Small stack size may cause stack overflow if the task uses more stack than allocated.
FreeRTOS
xTaskCreate(
    vTaskFunction,
    "SmallStack",
    10,  // Very small stack size
    NULL,
    1,
    NULL
);
Large stack size uses more memory but is safer for complex tasks.
FreeRTOS
xTaskCreate(
    vTaskFunction,
    "LargeStack",
    1000,  // Large stack size
    NULL,
    1,
    NULL
);
Sample Program

This program creates two tasks with different stack sizes. It prints the creation result and simulates running the tasks. The task prints its stack size parameter.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void vTaskFunction(void *pvParameters) {
    int local_array[10]; // Uses some stack
    printf("Task running with stack size: %d words\n", (int)(intptr_t)pvParameters);
    vTaskDelete(NULL); // Delete self
}

int main(void) {
    BaseType_t result;
    printf("Creating tasks with different stack sizes...\n");

    // Create task with 50 words stack
    result = xTaskCreate(vTaskFunction, "Task50", 50, (void *)(intptr_t)50, 1, NULL);
    printf("Task with 50 words stack creation result: %d\n", result);

    // Create task with 5 words stack (too small)
    result = xTaskCreate(vTaskFunction, "Task5", 5, (void *)(intptr_t)5, 1, NULL);
    printf("Task with 5 words stack creation result: %d\n", result);

    // Start scheduler (in real FreeRTOS, this starts tasks)
    // Here we just simulate by calling task function directly for demo
    vTaskFunction((void *)(intptr_t)50);
    vTaskFunction((void *)(intptr_t)5);

    return 0;
}
OutputSuccess
Important Notes

Stack size allocation affects how much memory each task uses and prevents crashes from stack overflow.

Time complexity: Setting stack size is a constant time operation during task creation.

Common mistake: Confusing stack size in words vs bytes, leading to too small or too large stacks.

Use larger stack sizes for tasks with deep function calls or large local variables.

Summary

Stack size allocation sets how much memory a task can use for temporary data.

It is important to allocate enough stack to avoid crashes but not waste memory.

Stack size is given in words, not bytes, when creating FreeRTOS tasks.

Practice

(1/5)
1. What does the stack size parameter specify when creating a FreeRTOS task?
easy
A. The total heap size available to the system
B. The number of tasks that can run simultaneously
C. The amount of memory allocated for the task's temporary data in words
D. The priority level of the task

Solution

  1. Step 1: Understand stack size meaning in FreeRTOS

    The stack size defines how much memory a task can use for its temporary data and function calls.
  2. Step 2: Identify the unit of stack size

    In FreeRTOS, stack size is given in words, not bytes, when creating a task.
  3. Final Answer:

    The amount of memory allocated for the task's temporary data in words -> Option C
  4. Quick Check:

    Stack size = task memory in words [OK]
Hint: Stack size means task memory in words, not bytes [OK]
Common Mistakes:
  • Confusing stack size with heap size
  • Thinking stack size is in bytes
  • Mixing stack size with task priority
2. Which of the following is the correct way to specify stack size when creating a FreeRTOS task in C?
easy
A. xTaskCreate(taskFunction, "Task", 128 / sizeof(int), NULL, 1, NULL);
B. xTaskCreate(taskFunction, "Task", 128 * sizeof(char), NULL, 1, NULL);
C. xTaskCreate(taskFunction, "Task", 128 * sizeof(int), NULL, 1, NULL);
D. xTaskCreate(taskFunction, "Task", 128, NULL, 1, NULL);

Solution

  1. Step 1: Recall stack size unit in FreeRTOS

    Stack size is specified in words, so just giving 128 means 128 words.
  2. Step 2: Analyze each option

    xTaskCreate(taskFunction, "Task", 128, NULL, 1, NULL); uses 128 words directly, which is correct. xTaskCreate(taskFunction, "Task", 128 * sizeof(char), NULL, 1, NULL); and C multiply by size, which is incorrect. xTaskCreate(taskFunction, "Task", 128 / sizeof(int), NULL, 1, NULL); divides, which is also wrong.
  3. Final Answer:

    xTaskCreate(taskFunction, "Task", 128, NULL, 1, NULL); -> Option D
  4. Quick Check:

    Stack size = number of words, no sizeof needed [OK]
Hint: Use stack size as word count, no sizeof multiplication [OK]
Common Mistakes:
  • Multiplying stack size by sizeof(int)
  • Dividing stack size by sizeof(int)
  • Confusing bytes and words in stack size
3. Given the code snippet:
void vTaskCode(void * pvParameters) {
    int x = 0;
    for(int i = 0; i < 10; i++) {
        x += i;
    }
    vTaskDelete(NULL);
}

xTaskCreate(vTaskCode, "Task1", 50, NULL, 2, NULL);

What is the minimum stack size allocation (in words) to safely run this task without overflow?
medium
A. 50 words
B. 10 words
C. 100 words
D. 5 words

Solution

  1. Step 1: Analyze task stack usage

    The task uses a few local variables (x, i) and a small loop. This requires some stack but not very large.
  2. Step 2: Check stack size given

    The task is created with 50 words stack size, which is enough for simple local variables and loop.
  3. Final Answer:

    50 words -> Option A
  4. Quick Check:

    Simple task needs moderate stack, 50 words is safe [OK]
Hint: Simple tasks need tens of words, not just a few [OK]
Common Mistakes:
  • Assuming 10 words is enough for loop variables
  • Overestimating stack need as 100 words
  • Underestimating stack causing overflow
4. You have a FreeRTOS task created with stack size 20 words, but it crashes with stack overflow. What is the best fix?
medium
A. Decrease the stack size to save memory
B. Increase the stack size to a larger value like 100 words
C. Change the task priority to higher
D. Remove local variables from the task

Solution

  1. Step 1: Identify cause of crash

    Stack overflow means the task's stack is too small for its needs.
  2. Step 2: Choose appropriate fix

    Increasing stack size to a larger value like 100 words prevents overflow safely.
  3. Final Answer:

    Increase the stack size to a larger value like 100 words -> Option B
  4. Quick Check:

    Stack overflow fix = increase stack size [OK]
Hint: Stack overflow? Increase stack size [OK]
Common Mistakes:
  • Reducing stack size to save memory
  • Changing priority instead of stack size
  • Ignoring local variables impact
5. You want to create two FreeRTOS tasks: Task A needs 80 words stack, Task B needs 40 words stack. The total heap is 100 words. How should you allocate stack sizes to avoid crashes and minimize wasted memory?
hard
A. Allocate 60 words to Task A and 40 words to Task B
B. Allocate 80 words to Task A and 40 words to Task B
C. Allocate 80 words to Task A and 20 words to Task B
D. Allocate 50 words to each task

Solution

  1. Step 1: Understand total heap limit

    Total heap is 100 words, so sum of stack sizes must not exceed 100.
  2. Step 2: Allocate stack sizes within limit

    Task A needs 80 words ideally, Task B needs 40 words, total 120 words which is over heap.
  3. Step 3: Adjust allocation to fit heap

    Reduce Task A to 60 words and keep Task B at 40 words to fit 100 words total.
  4. Final Answer:

    Allocate 60 words to Task A and 40 words to Task B -> Option A
  5. Quick Check:

    Sum stack ≤ heap, adjust sizes accordingly [OK]
Hint: Sum stack sizes ≤ heap size to avoid crashes [OK]
Common Mistakes:
  • Allocating more stack than heap allows
  • Ignoring total heap limit
  • Splitting stack sizes evenly without need
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