Why RTOS Queues Copy Data Instead of Passing Pointers
FreeRTOS queues copy items by value so senders can reuse variables, stack data stays safe and ownership is clear. See the trade-offs and when to pass pointers.

A newcomer who reads the FreeRTOS queue documentation hits a surprise: xQueueSend does not store a pointer to your variable. It copies the bytes. Passing a pointer would be cheaper, so why does the kernel pay for a copy on every send and every receive? The design is deliberate. Copying solves three problems that pointers create in a multitasking system: dangling references to stack variables, races between sender and receiver on the same memory and unclear ownership of buffers. This post shows each problem in code, explains what the copy buys and then explains when passing a pointer is still the right call and how to do it safely. It uses FreeRTOS Kernel V11.3.3. The cornerstone guide on FreeRTOS queues, stream buffers and message buffers covers the full family, and the post on xQueueSend and xQueueReceive gives the basic calls this post builds on.
What copying means
When a task calls xQueueSend(queue, &item, ticks), the kernel copies uxItemSize bytes from item into the queue's own storage area. When another task calls xQueueReceive(queue, &buffer, ticks), the kernel copies the bytes from the storage out to buffer. The sender's variable and the receiver's variable never alias, and the queue holds a private copy while the item waits.
typedef struct { uint8_t id; uint16_t value; } msg_t;
msg_t m = { .id = 1, .value = 100 };
xQueueSend(xQ, &m, 0); /* a copy of m is now in the queue */
m.value = 999; /* changing m now does not affect the queued copy */
xQueueSend(xQ, &m, 0); /* the second item holds 999 */The receiver gets 100 and then 999, in order, because each send captured the value at that moment. That property sounds obvious, and it is what a pointer cannot give you.
Problem 1: the dangling stack pointer
Tasks keep their locals on their own stacks, and a stack variable exists only while its function runs. A pointer to one is valid only as long as that function has not returned.
/* WRONG: sends a pointer to a local variable */
void vSenderBad(void *p)
{
(void) p;
for (;;)
{
sensor_reading_t reading = read_sensor();
sensor_reading_t *ptr = &reading;
xQueueSend(xPtrQueue, &ptr, portMAX_DELAY); /* queues the pointer */
vTaskDelay(pdMS_TO_TICKS(100));
}
}The queue stores the pointer value. By the time the receiver reads it, the sender has gone round the loop and reused the stack slot for the next reading, or, in a function that returned, the slot belongs to something else. The receiver dereferences a pointer to data that no longer means what it did. The failure is intermittent, because it depends on timing and priorities, and it produces plausible-looking wrong numbers rather than a crash.
With copying, the same program is correct without any care, because the kernel took the bytes before xQueueSend returned:
void vSenderGood(void *p)
{
(void) p;
for (;;)
{
sensor_reading_t reading = read_sensor();
xQueueSend(xValueQueue, &reading, portMAX_DELAY); /* copies the reading */
vTaskDelay(pdMS_TO_TICKS(100));
}
}The sender may reuse or discard reading the moment the call returns.
Problem 2: two tasks on one buffer
If the queue held a pointer to a shared buffer, the sender could write to the buffer again while the receiver was still reading it. The receiver then sees a half-old, half-new record, a torn read. Avoiding it means a lock around every access to the buffer, which brings back everything the queue was supposed to remove.
Copying makes the transfer atomic with respect to the tasks. The receiver works on its own copy, and the sender is free to produce the next item. The only shared state is inside the queue, protected by the kernel's own short critical sections.
Problem 3: who frees it?
A pointer raises the question of ownership. If the sender allocates a buffer and queues the pointer, the receiver must free it, exactly once. If several receivers exist, or the item is dropped when the queue is reset, the buffer leaks, or it is freed twice. Copying avoids the question for small items: the queue owns its storage, allocated once at creation, and the items are plain data.
What copying costs
The copy costs time and memory in proportion to the item size.
| Item size | Cost of a send plus a receive |
|---|---|
| A few bytes to a few dozen | Negligible next to the kernel's own bookkeeping |
| Hundreds of bytes | Two copies of that size plus any wait and wake-up work |
| Kilobytes | Large enough to hurt latency, and the queue needs length x size RAM |
A queue of 16 items of 1,024 bytes takes 16 KB of RAM, whether or not it is ever full. And the copy runs with the scheduler partly locked, which adds latency for other tasks and interrupts for large items. That is why the documentation advises against large items.
When to pass a pointer anyway
For a big buffer, a received network packet or a frame of audio, sending a pointer is the right design. The queue then carries a small fixed-size record that says where the data is:
typedef struct {
uint8_t *data;
size_t length;
} packet_ref_t;
void vRxTask(void *p)
{
(void) p;
for (;;)
{
uint8_t *buf = pool_alloc(); /* take a buffer from a fixed pool */
size_t n = radio_receive(buf, POOL_BUF_SIZE);
packet_ref_t ref = { .data = buf, .length = n };
if (xQueueSend(xPacketQueue, &ref, pdMS_TO_TICKS(10)) != pdPASS)
{
pool_free(buf); /* nobody took it: give it back */
}
}
}
void vProcessTask(void *p)
{
packet_ref_t ref;
(void) p;
for (;;)
{
if (xQueueReceive(xPacketQueue, &ref, portMAX_DELAY) == pdPASS)
{
handle_packet(ref.data, ref.length);
pool_free(ref.data); /* the receiver owns it now */
}
}
}The queue copies the 8-byte packet_ref_t, not the packet. Safety comes from a rule the code follows without exception: the sender relinquishes the buffer when the send succeeds, and the receiver frees it when done. The post on passing large buffers through a queue of pointers builds this out with a fixed pool.
Three conditions must hold, or a pointer design fails.
The buffer outlives the transfer. It cannot be a stack variable or anything reused before the receiver finishes.
Only one owner at a time. After the send, the sender must not touch the buffer.
Someone frees it exactly once, including on the error path where the send fails.
Comparing the two designs
| Question | Copy the item | Send a pointer |
|---|---|---|
| Safe with stack variables | Yes | No |
| Sender may reuse its variable at once | Yes | No, until the receiver is done |
| Cost grows with item size | Yes | No |
| RAM for the queue | length x size | length x pointer size plus the buffers |
| Needs an ownership rule | No | Yes |
| Easy to test | Yes | Harder, leaks and double frees appear |
| Good for | Commands, events, readings, small structs | Packets, frames, file blocks |
A middle path for medium items
Items of 50 to 200 bytes are a judgment call. A copy of 100 bytes takes microseconds on a Cortex-M, which is trivial for a message that arrives a few times a second and noticeable at tens of thousands a second. If the rate is low, copy, and keep the safety. If the rate is high and the item is large, move to pointers with a pool.
Another option for the "latest value" case is a queue of length one used with xQueueOverwrite and xQueuePeek, which copies a small status record that many readers can look at without consuming it.
The boundary
Copying protects the transfer, and it does nothing about what the data means. A struct that contains a pointer, such as a char * into another task's buffer, copies the pointer, and all of the dangling-pointer problems return. Keep queue items free of pointers unless you intend the ownership transfer. Also, the queue itself is safe from many tasks and from interrupts using the FromISR calls, while the buffers a pointer design points to have no such protection.


