Skip to content

Commit 09c296e

Browse files
author
Michael McLeod
committed
make_unique vs new discussion out of date
1 parent 32f8485 commit 09c296e

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

02cpp1/sec05Pointers.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ When Alice, for example, goes out of scope then Alice's data stays live because
218218
- This is an example of using shared pointers in a situation where it does not correctly model the ownership of the data. In this model a `Person` shouldn't have control over the lifetime of another, since different `Person` objects should be allowed to be created or destroyed independently. One `Person` is not a part of, or owned, by another.
219219
- In order to model this concept properly we need to use *non-owning* pointers, that allow objects to point to one another without influencing their lifetime.
220220

221-
## Aside: Using `make_unique` / `make_shared` or `new`
221+
## Aside: Using `make_unique` / `make_shared` or `new` in older C++ versions (before C++17)
222222

223-
There can be some circumstances where the use of `new` when creating a smart pointer can be a problem. Consider the following example:
223+
If you need to work on a project using C++11 or C++14, there can be some circumstances where the use of `new` when creating a smart pointer can be a problem. Consider the following example:
224224

225225
```cpp
226226
int someFunction(const std::shared_ptr<int>& sp_x, const int& i)
@@ -246,7 +246,9 @@ int main()
246246
- If the `riskyFunction` is called in between allocating the new int and creating the shared pointer to it, then if it throws an exception it will prevent the pointer to that memory being created and thus lead to memory allocated that cannot be accessed or deallocated i.e. a memory leak.
247247
- This can be fixed by using `std::make_shared<int>` instead.
248248
249-
**The `make_unique` and `make_shared` methods are generally preferred to using `new` as they are safer.**
249+
**The `make_unique` and `make_shared` methods are generally preferred to using `new` in older versions of C++ as they are safer.**
250+
251+
In C++ 17 this potential leak doesn't happen any more, because the evaluation order of arguments to functions has been fixed, and each argument must be fully evaluated in turn.
250252
251253
## Weak Pointers `std::weak_ptr<>`
252254

0 commit comments

Comments
 (0)