RTOS & Real-Time Concepts · Interview question

What goes wrong if you queue a pointer to a local variable?

A strong answer

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.

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

Browse all 472 interview questions
What goes wrong if you queue a pointer to a local variable? | EmbeddedPrep.io