[Smart pointers] Adding memory and overhead
1 unresolved thread
1 unresolved thread
Compare changes
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#unique_ptr" data-toc-modified-id="unique_ptr-2"><code>unique_ptr</code></a></span><ul class="toc-item"><li><span><a href="#Usage-to-store-data-in-a-class" data-toc-modified-id="Usage-to-store-data-in-a-class-2.1">Usage to store data in a class</a></span></li><li><span><a href="#Releasing-a-unique_ptr" data-toc-modified-id="Releasing-a-unique_ptr-2.2">Releasing a <code>unique_ptr</code></a></span></li></ul></li><li><span><a href="#shared_ptr" data-toc-modified-id="shared_ptr-3"><code>shared_ptr</code></a></span></li><li><span><a href="#Efficient-storage-with-vectors-of-smart-pointers" data-toc-modified-id="Efficient-storage-with-vectors-of-smart-pointers-4">Efficient storage with vectors of smart pointers</a></span><ul class="toc-item"><li><ul class="toc-item"><li><span><a href="#Using-a-trait-as-syntactic-sugar" data-toc-modified-id="Using-a-trait-as-syntactic-sugar-4.0.1">Using a trait as syntactic sugar</a></span></li></ul></li></ul></li></ul></div>
This does not mean they supersede entirely ordinary (often called **raw** or **dumb**) pointers: raw pointers might be a good choice to pass an object as a function parameter (see the discussion for the third question in this [Herb Sutter's post blog](https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/)). The raw pointer behind a smart pointer may be accessed through the `get()` method.
```
```
```
```
```
```
```
```
```
```
```
```
```
```
```
There is also a risk of not releasing properly the memory is there is a circular dependancy between two `shared_ptr`. A variation of this pointer named `weak_ptr` enables to circumvent this issue, but is a bit tedious to put into motion. I have written in [appendix](../7-Appendix/WeakPtr.ipynb) to describe how to do so.
* An idea could be to use pointers: copy is cheap, and there is no need to copy the underlying objects when the capacity is exceeded. Another good point is that a same object might be stored in two different containers, and the modifications given in one of this is immediately "seen" by the other (as the underlying object is the same).
```
```
```
```
```
```
```
```
We will perform a simple allocation test, where we will allocate and deallocate an integer 1000000 times the memory. With this we can compare the time consumed with raw pointers and the shared pointers. We will also compare the time cosumed with the constructors (using new is faster than the shared pointer constructor). [Coliru](https://coliru.stacked-crooked.com/a/5558ef99aa241568), [cpp.sh](http://cpp.sh/6qjhp) and [Compiler Explorer](https://godbolt.org/z/3KoYTfqf9).
```