Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single vs shared ownership meaning

Tags:

c++

ownership

Was reading Wikipedia for RAII when just saw Single and Shared ownership.

Googled for it and couldn't find any useful answer!

Could some one possibly explain this concept for a schoolboy?

like image 733
Amir Rezvani Avatar asked Dec 13 '12 03:12

Amir Rezvani


People also ask

Is it worth doing shared ownership?

Pros of Shared OwnershipDeposits are generally lower than buying on the open market. Shared Ownership makes mortgages more accessible, even if you're on a lower wage. Your monthly repayments can often work out cheaper than if you had an outright mortgage.

What does it mean when it says shared ownership?

Shared Ownership is a type of affordable home ownership when a purchaser takes out a mortgage on a share of a property and pays rent to a landlord on the remaining share. For example, someone might buy a 50% share in a property, and pay rent to the landlord on the remaining 50%.

What is the difference between shared ownership?

Shared ownership involves buying a share of a property and paying rent on the rest. Shared equity involves paying a low property deposit, using an equity loan for a percentage of the property's value, and getting a mortgage for the remaining amount.


2 Answers

It is essentially unique_ptr vs shared_ptr.

Single ownership, otherwise known as unique ownership, means that the resource is owned by a single class instance. Once that instance ceases to exist the resource is released (via the destructor). The majority of RAII classes you find have unique ownership, such as std::vector.

Shared ownership means the resource is shared between multiple class instances. The resource is only released once every instance ceases to exist and thus requires some form of reference counting or garbage collection. An example of where you would want shared ownership is a handle to a very expensive to copy immutable resource. I've seen it used in graphs too.

It might help to think in terms of pointers. Single ownership will only have 1 owning pointer, shared will have multiple. Of course, RAII might not involve pointers.

                    +---------------+
                    |Shared instance|
    +--------+      +------------+--+           +---------------+
    |Resource|                   |   +----------+Shared instance|
    +--------+                   v   v          +---------------+
        ^                    +--------+
        |                    |Resource|<-----------+
        |                    +--------+        +---+-----------+
        |                         ^            |Shared instance|
 +------+--------+                |            +---------------+
 |Unique Instance|                |
 +---------------+                |
                           +------+--------+
                           |Shared instance|
                           +---------------+
like image 117
Pubby Avatar answered Sep 24 '22 17:09

Pubby


Ownership is strongly tied with the concept of variable lifetimes.

If you can answer the question, when is it ok for this chunk of memory to go away?, then you can answer the issue of ownership.

If that chunk of memory is only tied with the lifetime of one variable, then you have single ownership.

Note that it's also important to think about heap or dynamic allocation vs stack or automatic variables. With automatic variables, when they go out of scope, the memory associated with them are reaped. With dynamic allocation, the same is not true, in the general case. If you use new facilities like std::unique_ptr<> for single ownership, then you can make a chunk of dynamic memory clean itself up when it falls out of scope. If there are many references to that chunk of memory with uncertain ordering of when the references will go away, then you may need something like a std::shared_ptr<> for multiple ownership.

like image 30
kfmfe04 Avatar answered Sep 22 '22 17:09

kfmfe04