RAII

RAII

Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life cycle of a resource that must be acquired before use to the lifetime of an object. Examples:

  • allocated heap memory

  • thread of execution

  • open socket, open file

  • locked mutex, disk space, database connection—anything that exists in limited supply

Resource Acquisition is Initialisation (RAII) Explained — Tom Dallingarrow-up-right

Non-RAII approach

#include <iostream>
using std::cout, std::endl,std::string,;

struct Resource{
    Resource()  { cout << "Resource created\n"; }
    ~Resource() { cout << "Resource destroyed\n"; }
};

int main (){
    Resource *r1 = new Resource;
    //maybe forget to delete or throw error
    delete r1;
    return 0;
}

maybe we'll forget to delete or throw an error

we could use smart-pointers

RAII

There are 3 parts to an RAII class:

  • The resource is relinquished in the destructor (e.g. closing a file)

  • Instances of the class are stack allocated

  • Optional: The resource is aquired in the constructor (e.g. opening a file).

When it comes to opening and closing files, std::fstream already has an RAII type of design because it closes itself in its destructor.

Last updated