Single and common sense of ownership

While reading Wikipedia for RAII , they just saw Single and Shared ownership.

Googled for him and did not find a useful answer!

Can someone explain this concept to a student?

+6
source share
5 answers

Essentially unique_ptr vs shared_ptr .

Single ownership, otherwise known as unique ownership, means that the resource belongs to an instance of one class. As soon as this instance ceases to exist, the resource is freed (via the destructor). Most of the RAII classes you find have a unique ownership, for example std::vector .

Co-ownership means that a resource is shared among multiple instances of a class. A resource is freed only after each instance ceases to exist and therefore requires some form of reference counting or garbage collection. An example of where you want to use shared ownership is a handle that is very expensive to copy an immutable resource. I saw that it is also used in graphs.

This can help think in terms of pointers. Only one ownership pointer will have one ownership, the common will have several. Of course, RAII may not include pointers.

  +---------------+ |Shared instance| +--------+ +------------+--+ +---------------+ |Resource| | +----------+Shared instance| +--------+ vv +---------------+ ^ +--------+ | |Resource|<-----------+ | +--------+ +---+-----------+ | ^ |Shared instance| +------+--------+ | +---------------+ |Unique Instance| | +---------------+ | +------+--------+ |Shared instance| +---------------+ 
+15
source

Property is strongly associated with the concept of life variables .

If you can answer the question, when is everything all right, so that this piece of memory is gone? Then you can answer the question about ownership.

If this piece of memory is associated only with the lifetime of one variable, then you have one property.

Note that it is also important to think about heap or dynamic allocation vs stack or automatically . When using automatic variables , when they go out of scope, the memory associated with them will be obtained. With dynamic allocation , the same is not true in the general case. If you use new objects, such as std :: unique_ptr <> for single ownership, you can make a piece of dynamic memory clean when it leaves the sphere. If there are many links to this piece of memory with an undefined order, when the links go away, you may need something like std :: shared_ptr <> for multiple ownership.

+6
source

Ownership comes down to who is responsible for freeing the resource. You can usually say that one piece of code (whether it be a function or a class) is the sole owner of the resource. When this is done with the resource, the owner will have to free it for the resource not leak . This is known as single or unique .

Similarly, there is the concept of shared ownership, where two or more discrete pieces of code, once again they can be classes or functions, both rely on the same resource. In this case, both of them no longer need a resource until it is released.

C ++ provides two useful wrappers for transferring and providing ownership semantics - ownership semantics is what is described above. These wrappers generate the concept of RAII - automatic release of a resource after it has gone out of scope.

  • unique_ptr - An object wrapped in a unique pointer will be immediately released when it goes out of scope, which means the function returns or the class is destroyed.

  • shared_ptr - an object wrapped with a shared pointer will remain available until all the strong links disappear from the volume. This is a concept called reference counting . When the final link goes out of scope, the resource is freed.

Semantics of ownership is incredibly important in a language such as C ++, so I recommend that you familiarize yourself with how modern C ++ conveys and applies it. You can start by exploring the proper use of unique_ptr and shared_ptr .

+6
source

With respect to / raii smart pointers, joint ownership is when more than one object can reference the same resource, and this resource is freed only when all instances of this object that reference the resource are deconstructed.

 // shared ownership { std::shared_ptr<SomeClass> i(new SomeClass()); { std::shared_ptr<SomeClass> j = i; { std::shared_ptr<SomeClass> k = j; // at this point i, j, k all own the same object } // k deconstructed and no longer shares ownership of the resource } // j deconstructed and no longer shares ownership of the resource } // i deconstructed and he resource is also released / free'd 

With a common (unique) owner, either dies with the object, or is transferred to another object

 // single/unique ownership { std::unique_ptr<SomeClass> i(new SomeClass()); { std::unique_ptr<SomeClass> j = std::move(i); // k takes ownership of the object from i } // j is deconstructed and the resource is also released / free'd } // i deconstructed and nothing is released, as the ownership already passed to j 
+2
source

Single ownership means that when the owner is done with the resource, he must delete it.

If he shares the title, he cannot remove it, because the other owner (s) can still use it! Thus, the removal of resources should be coordinated by certain means, usually by reference counting.

+1
source

All Articles