Resource Acquisition Is Initialization (RAII) is a programming idiom used in several object-oriented languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup to deal with resource allocation and deallocation in C++. In this language, the only code that can be guaranteed to be executed after an exception is thrown are the destructors of objects residing on the stack. Resource management therefore needs to be tied to the lifespan of suitable objects in order to gain automatic allocation and reclamation. They are acquired during initialization, when there is no chance of them being used before they are available, and released with the destruction of the same objects, which is guaranteed to take place even in case of errors.
RAII is vital in writing exception-safe C++ code: to release resources before permitting exceptions to propagate (in order to avoid resource leaks) one can write appropriate destructors once rather than dispersing and duplicating cleanup logic between exception handling blocks that may or may not be executed.
Read more about Resource Acquisition Is Initialization: Typical Uses, C++ Example
Famous quotes containing the words resource and/or acquisition:
“In a world which furnishes so many employments which are useful, and so many which are amusing, it is our own fault if we ever know what ennui [boredom] is, or if we are ever driven to the miserable resource of gaming, which corrupts our dispositions, and teaches us a habit of hostility against all mankind.”
—Thomas Jefferson (17431826)
“Wars and revolutions and battles are due simply and solely to the body and its desires. All wars are undertaken for the acquisition of wealth; and the reason why we have to acquire wealth is the body, because we are slaves in its service.”
—Socrates (469399 B.C.)