You create a dangling pointer. If you send an item by value, the queue copies it before your function returns, so sending a stack local is fine. But if you send a pointer to a stack-local variable, the queue copies only the pointer, and when the sending function returns, that local goes out of scope and its stack space is reused by subsequent calls, so by the time the consumer task dequeues the pointer and dereferences it, it's reading memory that no longer holds the intended data (or has been overwritten by another task's frame), giving corrupted or garbage values, often intermittently and hard to reproduce. It's the same lifetime bug as returning a pointer to a local from a function, just deferred across tasks. The fixes: send small data by value so the queue's copy carries it safely; or if you must pass a pointer, point to memory with a lifetime that spans the transfer, a heap or pool allocation handed off with clear ownership, a static buffer, or a buffer the consumer owns, never an automatic (stack) variable. This is a classic RTOS bug because it can pass casual testing (the stack bytes happen to survive briefly) and fail under real load when another task reuses that stack region promptly.
RTOS & Real-Time Concepts · Interview question
What goes wrong if you queue a pointer to a local variable?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Queues & Inter-Task Communication
Passing data between tasks safely: a kernel queue is a thread-safe blocking FIFO (copy-by-value), the RTOS producer/consumer pattern that replaces shared-global-plus-mutex with message passing.
More Queues & Inter-Task Communication questions
Why use a queue instead of a shared global protected by a mutex?What are the copy semantics of a FreeRTOS queue, and when do you send a pointer instead?How do you pass data from an ISR to a task?Besides queues, what inter-task communication primitives does an RTOS offer, and when would you use them?
Browse all 472 interview questions