C Programming · Interview question

What is a memory leak, and why is it worse on an embedded device than a desktop?

A strong answer

A leak is allocated heap memory that's never freed and to which you've lost all pointers, it can't be reused or reclaimed. On a desktop, leaks are bounded by the process: the OS reclaims everything when the program exits, and virtual memory plus an OOM killer give some slack, so a small leak in a short-lived program is often harmless. On embedded, there's typically no OS to reclaim memory, the heap is tiny (kilobytes), and the device runs for weeks or months without restarting. A slow leak in a loop will eventually exhaust the heap, at which point malloc returns NULL and, if that's unchecked, the device crashes or resets. There's no safety net, so leaks that would be invisible on a PC are device-killing on a microcontroller.

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

Dynamic Memory: malloc & free

Sizing memory at runtime with malloc/calloc/realloc, and the discipline of free that prevents leaks, double-frees, and use-after-free bugs.

More Dynamic Memory: malloc & free questions

Browse all 472 interview questions
What is a memory leak, and why is it worse on an embedded device than a desktop? | EmbeddedPrep.io