make_shared is a function for creating shared pointers introduced in C++11.
Questions tagged [make-shared]
138 questions
57
votes
4 answers
Is make_shared really more efficient than new?
I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared. As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within…

user1212354
- 589
- 1
- 4
- 6
52
votes
5 answers
How to pass deleter to make_shared?
Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use…

cavitsinadogru
- 940
- 2
- 10
- 17
43
votes
4 answers
Can you allocate an array with something equivalent to make_shared?
buffer = new char[64];
buffer = std::make_shared(char[64]); ???
Can you allocate memory to an array using make_shared<>()?
I could do: buffer = std::make_shared( new char[64] );
But that still involves calling new, it's to my…

Josh Elias
- 3,250
- 7
- 42
- 73
28
votes
2 answers
std::make_shared() change in C++17
In cppref, the following holds until C++17:
code such as f(std::shared_ptr(new int(42)), g()) can cause a
memory leak if g gets called after new int(42) and throws an
exception, while f(std::make_shared(42), g()) is safe, since
two…

Lingxi
- 14,579
- 2
- 37
- 93
27
votes
3 answers
What happens when using make_shared
I'm interested if these two lines of code are the same:
shared_ptr sp(new int(1)); // double allocation?
shared_ptr sp(make_shared(1)); // just one allocation?
If this is true could someone please explain why is it only one…

Tracer
- 2,544
- 2
- 23
- 58
24
votes
2 answers
weak_ptr, make_shared and memory deallocation
A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is kept allocated. (The object itself is properly…

Ilya Popov
- 3,765
- 1
- 17
- 30
21
votes
2 answers
Does std::make_shared() use custom allocators?
Consider this code:
#include
#include
class SomeClass {
public:
SomeClass() {
std::cout << "SomeClass()" << std::endl;
}
~SomeClass() {
std::cout << "~SomeClass()" << std::endl;
}
void*…

Mark Garcia
- 17,424
- 4
- 58
- 94
18
votes
2 answers
How to combine std::make_shared and new(std::nothrow)
C++'s new has an option to return a null pointer instead of throwing a bad_alloc exception when an allocation failed.
Foo * pf = new(std::nothrow) Foo(1, 2, 3);
(Yes, I understand this only prevents the new from throwing a bad_alloc; it doesn't…

Adrian McCarthy
- 45,555
- 16
- 123
- 175
16
votes
2 answers
C++11 storing multiple shared pointers as raw pointers
My question concerns shared_ptr and make_shared in C++11. I have two vectors, the first one stores smart pointers and the second one stores raw pointers. The first vector works as I had expepted but vector2 is just confusing...
Code sample
#include…
user2442944
15
votes
3 answers
Why does boost not have a make_scoped()?
Boost's make_shared() function promises to be exception-safe while attempting to create a shared_ptr.
Why is there no make_scoped() equivalent? Is there a common best practice?
Here's a code example from the boost::scoped_ptr documentation that…

Drew Dormann
- 59,987
- 13
- 123
- 180
15
votes
1 answer
C++11 Passing 'this' as paramenter for std::make_shared
I'm trying to pass 'this' to the constructor using std::make_shared
Example:
// headers
class A
{
public:
std::shared_ptr createB();
}
class B
{
private:
std::shared_ptr a;
public:
B(std::shared_ptr);
}
//…

RdR
- 213
- 3
- 7
13
votes
1 answer
Does enable_shared_from_this and make_shared provide the same optimization
As I understand make_shared(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T).
Do enable_shared_from_this provides the same optimization? So:
class T :…

Dmitry Poroh
- 3,705
- 20
- 34
12
votes
2 answers
Initializing shared_ptr member variable, new vs make_shared?
When initializing a shared_ptr member variable:
// .h
class Customer
{
public:
Customer();
private:
std::shared_ptr something_;
}
// .cpp
Customer():
something_(new OtherClass())
{
}
vs.
Customer():
…

User
- 62,498
- 72
- 186
- 247
11
votes
2 answers
std::make_shared number of parameters in the constructor
In the absence of variadic templates (still!) in Visual Studio 2010/2011, a constructor that takes a lot of parameters can be problematic. For example the following won't compile:
MyMaterials.push_back(std::make_shared(MyFacade,
…

Robinson
- 9,666
- 16
- 71
- 115
9
votes
2 answers
What does "single allocation" mean for boost::make_shared
In the boost doc of make_shared, it says:
Besides convenience and style, such a function is also exception safe and considerably
faster because it can use a single allocation for both the object and its corresponding
control block,…

atomd
- 558
- 1
- 5
- 11