Questions tagged [make-shared]

make_shared is a function for creating shared pointers introduced in C++11.

126 questions
54
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
  • 559
  • 1
  • 4
  • 6
44
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
  • 820
  • 1
  • 7
  • 17
32
votes
3 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
  • 2,960
  • 7
  • 36
  • 70
29
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
  • 13,248
  • 1
  • 32
  • 74
26
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,310
  • 2
  • 17
  • 46
20
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,386
  • 1
  • 13
  • 29
19
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
  • 16,438
  • 3
  • 50
  • 93
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
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
  • 41,073
  • 12
  • 108
  • 157
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
  • 50,103
  • 11
  • 109
  • 162
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
  • 8,836
  • 13
  • 64
  • 108
10
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,195
  • 16
  • 33
10
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
  • 49,320
  • 65
  • 164
  • 234
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
  • 528
  • 1
  • 5
  • 11
9
votes
6 answers

How to make boost::make_shared a friend of my class

I have written a class with protected constructor, so that new instances can only be produced with a static create() function which returns shared_ptr's to my class. To provide efficient allocation I'd like to use boost::make_shared inside the…
kyku
  • 5,511
  • 3
  • 39
  • 48
1
2 3
8 9