What is meant by Resource Acquisition is Initialization (RAII)?

前端 未结 8 1693
面向向阳花
面向向阳花 2020-11-21 04:23

What is meant by Resource Acquisition is Initialization (RAII)?

8条回答
  •  攒了一身酷
    2020-11-21 05:09

    An object's lifetime is determined by its scope. However, sometimes we need, or it is useful, to create an object that lives independently of the scope where it was created. In C++, the operator new is used to create such an object. And to destroy the object, the operator delete can be used. Objects created by the operator new are dynamically allocated, i.e. allocated in dynamic memory (also called heap or free store). So, an object that was created by new will continue to exist until it's explicitly destroyed using delete.

    Some mistakes that can occur when using new and delete are:

    • Leaked object (or memory): using new to allocate an object and forget to delete the object.
    • Premature delete (or dangling reference): holding another pointer to an object, delete the object, and then use the other pointer.
    • Double delete: trying to delete an object twice.

    Generally, scoped variables are preferred. However, RAII can be used as an alternative to new and delete to make an object live independently of its scope. Such a technique consists of taking the pointer to the object that was allocated on the heap and placing it in a handle/manager object. The latter has a destructor that will take care of destroying the object. This will guarantee that the object is available to any function that wants access to it, and that the object is destroyed when the lifetime of the handle object ends, without the need for explicit cleanup.

    Examples from the C++ standard library that use RAII are std::string and std::vector.

    Consider this piece of code:

    void fn(const std::string& str)
    {
        std::vector vec;
        for (auto c : str)
            vec.push_back(c);
        // do something
    }
    

    when you create a vector and you push elements to it, you don't care about allocating and deallocating such elements. The vector uses new to allocate space for its elements on the heap, and delete to free that space. You as a user of vector you don't care about the implementation details and will trust vector not to leak. In this case, the vector is the handle object of its elements.

    Other examples from the standard library that use RAII are std::shared_ptr, std::unique_ptr, and std::lock_guard.

    Another name for this technique is SBRM, short for Scope-Bound Resource Management.

提交回复
热议问题