# Implementation details
___
### Implementation details – `std::unique_ptr<>`
* Just a holding wrapper
* Holds an object pointer
* Constructor copies a pointer
* Call proper delete in destructor
* No copying
* Moving means:
* Copying original pointer to a new object
* Setting source pointer to nullptr
___
### Implementation details – `std::shared_ptr<>`
* Holds an object pointer
* Holds 2 reference counters:
* shared pointers count
* weak pointers count
* Destructor:
* decrements shared-refs
* deletes user data when shared-refs == 0
* deletes reference counters when shared-refs == 0 and weak-refs == 0
* Extra space for a deleter
___
### Implementation details – `std::shared_ptr<>`
* Copying means:
* Copying pointers to the target
* Incrementing shared-refs
* Moving means:
* Copying pointers to the target
* Setting source pointers to nullptr
___
### Implementation details – `std::weak_ptr<>`
* Holds an object pointer
* Holds 2 reference counters:
* shared pointers count
* weak pointers count
* Destructor:
* decrements weak-refs
* deletes reference counters when shared-refs == 0 and weak-refs == 0
___
### Implementation details – `std::weak_ptr<>`
* Copying means:
* Copying pointers to the target
* Incrementing weak-refs
* Moving means:
* Copying pointers to the target
* Setting source pointers to nullptr
___
### `std::weak_ptr<>` + `std::shared_ptr<>`
* Having a shared pointer and a weak pointer
* After removing the shared pointer
___
## Making a `std::shared_ptr<>`
std::shared_ptr<Data> p{new Data};
* Perform two allocations: one for control block and second for data
* Before C++17 can make a problem with ordering of operations
* When all shared_ptr will be deleted but there is some weak_ptr allocated memory for data can be freed.
auto p = std::make_shared<Data>();
* Less memory (most likely)
* Only one allocation
* Cache-friendly
* When all shared_ptr will be deleted but there is some weak_ptr allocated memory for data cannot be freed