C++ for Embedded · Interview question

What is RAII and why is it especially valuable in embedded?

A strong answer

RAII, Resource Acquisition Is Initialization, ties a resource's lifetime to an object's lifetime: you acquire the resource in the constructor and release it in the destructor, and because the destructor runs automatically when the object leaves scope, cleanup happens on every exit path, normal return, early return, or (where enabled) exception. That eliminates the C class of bugs where an early return skips the matching cleanup and leaks a lock, clock, or buffer. It's especially valuable on embedded for two reasons: resources are scarce and leaks are fatal over long uptimes, and the cleanup is deterministic, it happens at a precise, known point (the closing brace), unlike garbage collection which runs at unpredictable times. Deterministic, zero-overhead, leak-proof cleanup is exactly what real-time firmware needs, and it's the headline reason to use C++ on an MCU.

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

Constructors, Destructors & RAII

Tie setup to object creation and cleanup to scope exit, so you can't forget to release a lock, disable a clock, or re-enable interrupts. The defining C++ idiom for embedded.

More Constructors, Destructors & RAII questions

Browse all 472 interview questions
What is RAII and why is it especially valuable in embedded? | EmbeddedPrep.io