Consider the following code snippet using xQueueCreate() in FreeRTOS:
QueueHandle_t queue = xQueueCreate(3, sizeof(int));
if (queue != NULL) {
printf("Queue created with length: %u\n", uxQueueSpacesAvailable(queue));
} else {
printf("Queue creation failed\n");
}What will be printed when this code runs successfully?
QueueHandle_t queue = xQueueCreate(3, sizeof(int)); if (queue != NULL) { printf("Queue created with length: %u\n", uxQueueSpacesAvailable(queue)); } else { printf("Queue creation failed\n"); }
Remember that uxQueueSpacesAvailable() returns the number of free spaces in the queue right after creation.
When a queue is created with length 3, it is empty and has 3 free spaces available. So uxQueueSpacesAvailable() returns 3.
In the function call xQueueCreate(5, sizeof(float)), what does the second parameter sizeof(float) represent?
Think about what the queue needs to know to store each element.
The second parameter specifies the size in bytes of each item the queue will hold. This allows the queue to allocate the right amount of memory for each element.
What happens when you call xQueueCreate(0, sizeof(int)) in FreeRTOS?
QueueHandle_t queue = xQueueCreate(0, sizeof(int)); if (queue == NULL) { printf("Queue creation failed\n"); } else { printf("Queue created\n"); }
Queues must have a positive length to hold items.
Creating a queue with zero length is invalid and xQueueCreate() returns NULL, indicating failure.
Identify the error in this code snippet:
QueueHandle_t queue = xQueueCreate(5, int);
if (queue != NULL) {
// use queue
}QueueHandle_t queue = xQueueCreate(5, int); if (queue != NULL) { // use queue }
Check the type and value of the second parameter.
The second parameter to xQueueCreate() must be the size in bytes of each item, so it should be sizeof(int), not just int.
Given the code below, how many items can the queue hold?
QueueHandle_t queue = xQueueCreate(10, sizeof(Data));
Assuming Data is defined as:
typedef struct {
int id;
char name[20];
} Data;typedef struct {
int id;
char name[20];
} Data;
QueueHandle_t queue = xQueueCreate(10, sizeof(Data));The first parameter to xQueueCreate() is the number of items the queue can hold.
The queue length is set by the first parameter (10), so it can hold 10 items of type Data.
