Resource Acquisition Is Initialization

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:

    Helping children at a level of genuine intellectual inquiry takes imagination on the part of the adult. Even more, it takes the courage to become a resource in unfamiliar areas of knowledge and in ones for which one has no taste. But parents, no less than teachers, must respect a child’s mind and not exploit it for their own vanity or ambition, or to soothe their own anxiety.
    Dorothy H. Cohen (20th century)

    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 (469–399 B.C.)