xQueueCreate() function in FreeRTOS - Time & Space Complexity
We want to understand how the time to create a queue changes as the queue size grows.
Specifically, how does the xQueueCreate() function behave when asked to create bigger queues?
Analyze the time complexity of the following code snippet.
QueueHandle_t xQueue;
xQueue = xQueueCreate(10, sizeof(int));
if (xQueue == NULL) {
// Queue creation failed
}
This code creates a queue that can hold 10 integers and checks if creation succeeded.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Allocating memory for the queue storage buffer.
- How many times: This allocation happens once during queue creation.
As the queue length (number of items it can hold) increases, the memory allocation size grows proportionally.
| Input Size (queue length) | Approx. Operations |
|---|---|
| 10 | Allocate space for 10 items |
| 100 | Allocate space for 100 items |
| 1000 | Allocate space for 1000 items |
Pattern observation: The work grows directly with the queue length because memory for each item must be reserved.
Time Complexity: O(1)
This means creating a queue takes constant time regardless of the number of items it can hold.
[X] Wrong: "Creating a queue is always constant time regardless of size."
[OK] Correct: Because the function must allocate memory for all items, the time grows with queue length.
Understanding how resource allocation scales helps you reason about system responsiveness and memory use in embedded systems.
"What if the queue was created with zero length? How would the time complexity change?"
Practice
xQueueCreate() function do in FreeRTOS?Solution
Step 1: Understand the purpose of xQueueCreate()
The function is designed to create a new queue in FreeRTOS for inter-task communication.Step 2: Differentiate from other queue functions
Functions like sending or receiving data operate on existing queues, not create them.Final Answer:
Creates a new queue to hold data items between tasks -> Option BQuick Check:
xQueueCreate() = create queue [OK]
- Confusing creation with sending or receiving data
- Thinking it deletes a queue
- Assuming it initializes queue data
Solution
Step 1: Recall xQueueCreate() parameters
The first parameter is the number of items, the second is the size of each item.Step 2: Match parameters to options
xQueueCreate(10, sizeof(int)); correctly uses 10 items and sizeof(int) as size per item.Final Answer:
xQueueCreate(10, sizeof(int)); -> Option AQuick Check:
Parameters = (number, size) [OK]
- Swapping the order of parameters
- Providing only one parameter
- Using raw numbers instead of sizeof()
QueueHandle_t q = xQueueCreate(5, sizeof(char));
if (q == NULL) {
printf("Queue creation failed\n");
} else {
printf("Queue created successfully\n");
}
What will be printed if the queue is created successfully?Solution
Step 1: Check the return value of xQueueCreate()
If queue creation succeeds, it returns a non-NULL handle.Step 2: Analyze the if condition
The code prints "Queue created successfully" when q is not NULL.Final Answer:
Queue created successfully -> Option CQuick Check:
q != NULL means success [OK]
- Assuming NULL means success
- Ignoring the if condition
- Confusing print messages
QueueHandle_t q = xQueueCreate(0, sizeof(int));
if (q == NULL) {
// handle error
}Solution
Step 1: Understand queue length parameter
The first parameter must be greater than zero to hold items.Step 2: Consequence of zero length
Passing zero causes xQueueCreate() to fail and return NULL.Final Answer:
Queue size cannot be zero, so creation fails -> Option AQuick Check:
Queue length > 0 required [OK]
- Using zero length queue
- Confusing size of item with queue length
- Ignoring return NULL check
SensorData. Which is the correct way to create this queue?Solution
Step 1: Identify queue length and item size
Queue length is 3, item size is size of one SensorData structure.Step 2: Match parameters to xQueueCreate()
First parameter is number of items (3), second is size of each item (sizeof(SensorData)).Final Answer:
xQueueCreate(3, sizeof(SensorData)); -> Option DQuick Check:
Length=3, size=sizeof(struct) [OK]
- Swapping length and size parameters
- Using pointer size instead of struct size
- Creating queue for one large combined item
Ready to prove it?
Pass the test and this pattern moves unknown
