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.
C++ for Embedded · Interview question
What is RAII and why is it especially valuable in embedded?
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
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
Write an RAII guard for a critical section. Why is it better than paired enable/disable calls?Why prefer the member initializer list over assigning members in the constructor body?In what order are destructors called, and why does it matter?If a class has a destructor that releases a resource, what else must you consider?
Browse all 472 interview questions