Is there a situation in which I would not use std :: make_shared?

From my research, it seems that std::make_shared is the preferred way to build std::shared_ptr . In particular, since:

  • It performs only one memory allocation compared to using new , which performs at least two.
  • If ctor switched to make_shared throws, then it will not flow, as it will be with the new one.

My question is: if I want shared_ptr, should I always use make_shared , or are there cases where new is preferred?

+7
c ++ memory-management c ++ 11 smart-pointers shared-ptr
source share
5 answers

Since the counter and the object have the same distribution, they also use the same release.

The counter should be kept until the last shared_ptr and weak_ptr . If you have a large object (or many small objects) with long weak_ptr s, this can lead to a memory conflict if you make_shared shared_ptr via make_shared .

Secondly, if you have a third-party API that passes you a pointer or a resource descriptor and possibly has its own functionality, make_shared not suitable and cannot be used in each case. Creating your own make_ functions can cause messy details to help you deal with this problem, and also deal with the issue of exception.

Finally, while generic pointers are awesome, they are also too strong. Quite often, I want unique_ptr or even boost::scoped_ptr , or an intrusive reference count pointer, or the like, to represent ownership. shared_ptr should be used only when the situation actually involves joint ownership of the resource: using it willy-nilly, because it is "easy", as a rule, ends with the resource equivalent of spaghetti code.

+6
source share

You may have to deal with legacy code that returns a dynamically allocated object. In this case, you will need to use std::shared_ptr<T> ctor with a pointer parameter. Using std::make_shared not recommended, but this allows you to use all std::shared_ptr<T> Q factors with outdated code.

I know that this is not strictly equivalent to using std::shared_ptr<T> ctor with new directly, but it is a valid example of using std::shared_ptr<T> where make_shared cannot be used.

+2
source share

I have little doubt about the interpretation of your question. I assume it makes sense to use shared_ptr<T> ; I can only second Yakk explain the reasons why you would not want to use shared_ptr in the first place.

There is one situation where you cannot use make_shared or allocate_shared to build shared_ptr , but you need to use the corresponding ctor: If you need to pass a custom div, see (3) and (4)) in ctors shared_ptr .

+1
source share

I am having problems using make_shared in a private constructor class (from a static factory method). I do not think this is a simple solution.

0
source share

Should I always use make_shared, or are there cases when a new one is preferred

make_shared not allowed when we store a bare pointer in shared_ptr allocated by someone else. and it can only call public constructors. However, some compilers have some reports about accessing the protected constructor using make_shared like this .

0
source share

All Articles