C ++ 11 priority use of smart pointer std :: make_unique and std :: make_shared instead of using new

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/tanningzhong/article/details/90439753

When initializing a unique_ptr or shared_ptr, we best use std :: make_unique priority and std :: make_shared. There are reasons the following points:

Exception Safety

Consider the following function declarations:

intcomputePriority();
void processInvestment(std::shared_ptr<Investment> ptr,int priority);

ProcessInvestment call code is as follows:

processInvestment(std::shared_ptr<Investment>(newInvestment()),computePriority());

 

Since the execution order of function parameters is not fixed in C ++, so on the face of processInvestment function call, the execution order of function arguments is likely to be something like this:

new Investment

computePriority()

std::shared_ptr constructor

This order of execution risk is that if an exception occurs during the second step, the execution computePriority, then in the first step out of the new object will become inaccessible, resulting in a memory leak. Make_ptr by the way, the construction and operation of new shared_ptr placed to perform together, there would not be a memory leak problem.

The efficiency (in terms for shared_ptr)

std :: shared_ptr <Investment> ptr (new Investment); // embodiment 1, new ways

In one embodiment, it involves two dynamic memory allocation:

The first time was when the new Investment, Investment target allocation for the space.

The second is the control block (Control Block) allocation space.

auto pIn = std :: make_shared <Investment> (); // embodiment 2, make_shared manner

In the second approach, a sufficient dynamic memory allocation, which is due make_shared allocates a chunk of memory for the objects Investment and control block (Control block) at once. Since only one memory allocation, and thus Second way to improve the performance of programs.

Second, the function of the drawbacks make

  Since the use of make_shared and make_unique there are so many benefits, whether we should always make use of new functions and completely abandon the way of it? Of course not, make function there are also some disadvantages:

1 make function does not support user-defined release. Due to make memory allocation function has its own rules and destructor, so he does not apply to objects that have customized and release of the dispenser.

2 make function does not support braces initialization method. For this code the following:

auto spv = std::make_shared<vector<int>>(10,20);

Means spv point to a vector, this vector has 10 elements, each element's value is 20. If you want to achieve this vector has two elements, respectively, 10, 20, you can only use new ways.

3 Memory release is not flexible enough.

In the new mode of use, there are two separate heap memory, a storage resource object, a storage control block, a reference resource object when the count reaches zero, the resource objects are destroyed, the memory it will be subsequently destroyed.

In the embodiment with make function, make a one-time function and control block for the resource objects are assigned a memory when the resource reference count for the object is 0, the object is destroyed, but the memory resources occupied by the object they will not be destroyed, only when the control block is occupied by the memory is destroyed before the memory occupied by objects together with the release of resources. Then, the control block of memory when it is released? This relates to the control block of another reference count, the reference count is referred to as "Weak Count", which function is used to count the number of points to the resource weak_ptr. When this weak count is 0, the control block will not be released. When a resource is very large objects, make use of the function will result in no small way to the waste of resources.

Guess you like

Origin blog.csdn.net/tanningzhong/article/details/90439753