Stack size allocation in FreeRTOS is the process of assigning a specific amount of memory for each task's stack, which stores local variables, function calls, and return addresses during task execution.
Click to reveal answer
beginner
Why is it important to allocate the correct stack size for a FreeRTOS task?
Allocating the correct stack size prevents stack overflow, which can cause unpredictable behavior or crashes, and avoids wasting memory by not allocating more stack than needed.
Click to reveal answer
intermediate
How do you specify the stack size when creating a task in FreeRTOS?
You specify the stack size as the third argument in the xTaskCreate() function, usually in words (not bytes), for example: xTaskCreate(taskFunction, "TaskName", 128, NULL, priority, &handle);
Click to reveal answer
intermediate
What unit is used for stack size in FreeRTOS task creation?
Stack size is specified in words, not bytes. The size of a word depends on the processor architecture (e.g., 4 bytes on a 32-bit system).
Click to reveal answer
advanced
What tool or method can help detect stack overflow in FreeRTOS?
FreeRTOS provides stack overflow checking options that can be enabled in the configuration file (configCHECK_FOR_STACK_OVERFLOW). Also, runtime checks and monitoring functions can help detect stack overflow.
Click to reveal answer
In FreeRTOS, what does the stack size parameter in xTaskCreate() represent?
ANumber of words allocated for the task's stack
BNumber of bytes allocated for the task's stack
CNumber of tasks that can run simultaneously
DPriority level of the task
✗ Incorrect
The stack size parameter in xTaskCreate() is the number of words allocated for the task's stack, not bytes.
What can happen if a FreeRTOS task's stack size is too small?
AThe task will automatically increase its stack size
BThe task will run faster
CThe task will use less CPU time
DStack overflow may occur, causing crashes or unpredictable behavior
✗ Incorrect
If the stack size is too small, the task can overflow its stack, leading to crashes or unpredictable behavior.
How can you detect stack overflow in FreeRTOS?
AUse more heap memory
BEnable configCHECK_FOR_STACK_OVERFLOW in FreeRTOSConfig.h
CIncrease task priority
DUse a debugger to watch CPU registers only
✗ Incorrect
Enabling configCHECK_FOR_STACK_OVERFLOW activates stack overflow checking in FreeRTOS.
When specifying stack size in xTaskCreate(), what should you consider about the processor?
AStack size is in words, and word size depends on processor architecture
BStack size is always in bytes regardless of processor
CProcessor speed determines stack size units
DStack size is irrelevant to processor architecture
✗ Incorrect
Stack size is specified in words, and the size of a word depends on the processor architecture.
What is a good practice when deciding the stack size for a FreeRTOS task?
AAlways use the smallest possible stack size
BUse the maximum stack size allowed for all tasks
CEstimate based on task needs and add some margin for safety
DIgnore stack size because FreeRTOS manages it automatically
✗ Incorrect
Estimating stack size based on task needs and adding margin helps prevent overflow without wasting memory.
Explain how stack size allocation works in FreeRTOS and why it matters.
Think about what happens if the stack is too small or too large.
You got /4 concepts.
Describe how to specify and check stack size for a task in FreeRTOS.
Consider both creation and debugging steps.
You got /4 concepts.
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
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.
Step 2: Identify the unit of stack size
In FreeRTOS, stack size is given in words, not bytes, when creating a task.
Final Answer:
The amount of memory allocated for the task's temporary data in words -> Option C
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
Step 1: Recall stack size unit in FreeRTOS
Stack size is specified in words, so just giving 128 means 128 words.
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.
Final Answer:
xTaskCreate(taskFunction, "Task", 128, NULL, 1, NULL); -> Option D
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
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.
Step 2: Check stack size given
The task is created with 50 words stack size, which is enough for simple local variables and loop.
Final Answer:
50 words -> Option A
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
Step 1: Identify cause of crash
Stack overflow means the task's stack is too small for its needs.
Step 2: Choose appropriate fix
Increasing stack size to a larger value like 100 words prevents overflow safely.
Final Answer:
Increase the stack size to a larger value like 100 words -> Option B
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
Step 1: Understand total heap limit
Total heap is 100 words, so sum of stack sizes must not exceed 100.
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.
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.
Final Answer:
Allocate 60 words to Task A and 40 words to Task B -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.