Explain the concept of Resource Acquisition Is Initialization (RAII). Why is it considered a cornerstone of C++ memory safety?
Last updated
RAII (Resource Acquisition Is Initialization) ties a resource to an object's lifetime: acquire it in the constructor and release it in the destructor.
Destructors run deterministically when scope exits, even during exception unwinding, so there are no leaks and no manual
deleteto forget.Standard RAII types include
std::unique_ptr,std::vectorandstd::lock_guard. Never own a raw resource directly.