Questions tagged [make-shared]

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

138 questions
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,892
  • 4
  • 43
  • 51
9
votes
1 answer

Why must shared_ptr<> allocate for the control block and managed object separately?

This linked question asked if the make_shared<> function and the shared_ptr<> constructor differ. What happens when using make_shared Part of the answer was that make_shared<> will usually allocate memory for both the pointed to object and smart…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
8
votes
3 answers

Why 'std::make_shared' is always using the global memory allocation even with class overloaded new/delete operators?

When using std::make_shared the class overloaded new/delete operators are not called. When using std::shared_ptr, std::unique_ptr & std::make_unique the class overloaded new/delete operators are used. When looking at the documentation…
8
votes
3 answers

Was the raw-pointer constructor of shared_ptr a mistake?

In hindsight, given make_shared, would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11? Are there strong arguments or use cases in favor of this constructor? It would have avoided the well documented pitfall…
Arvid
  • 10,915
  • 1
  • 32
  • 40
7
votes
4 answers

new and make_shared for shared pointers

I came across this post and one of the answers by @kerek SB states std::shared_ptr p1 = std::make_shared("foo"); std::shared_ptr p2(new Object("foo")); In your code, the second variable is just a naked pointer, not a …
MistyD
  • 16,373
  • 40
  • 138
  • 240
7
votes
3 answers

Can I use boost::make_shared with a private constructor?

Consider the following: class DirectoryIterator; namespace detail { class FileDataProxy; class DirectoryIteratorImpl { friend class DirectoryIterator; friend class FileDataProxy; WIN32_FIND_DATAW currentData; …
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
6
votes
3 answers

boost::make_shared is not calling (placement) operator new?

I am using boost::make_shared for the first time to create objects pointed to by shared pointers. Mainly because our code was too slow and the single allocation really helped to improve performance. After fixing some memory leaks "the hard manual…
Joost Sannen
  • 215
  • 1
  • 9
6
votes
1 answer

Errors in std::make_shared() when trying to make shared_ptr?

(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is…
Bret Kuhns
  • 4,034
  • 5
  • 31
  • 43
6
votes
1 answer

too many arguments to function std::make_shared

I am missing something with std::make_shared. Can't it resolve the type of a std::initializer_list, or am I doing something else wrong? #include #include class A {}; int main() { A a; std::vector veca{A(), A{}, a}; //…
6
votes
4 answers

C++ compiler error: “invalid declarator before”

Here's my code. When compiling I get the error invalid declarator before ‘geometry’ at line 16 and line 48, I am not sure what I am doing wrong. Please advise. #include #include #include using namespace std; class…
6
votes
1 answer

differences between make_unique and make_shared when handling arrays

as of C++17 you can use make_unique in order to create smart pointers to arrays, such as: unique_ptr ptr = make_unique(10); which will create a smart pointer to an array of 10 elements (the fact that proper deleter[] will be called is…
MutomboDikey
  • 155
  • 2
  • 12
5
votes
1 answer

C++ make_shared not available

While I have std::tr1::shared_ptr available in my compiler, I don't have make_shared. Can someone point me to a proper implementation of make_shared? I see that I need to use varargs to provide arguments to constructor of T. But I don't have…
user231536
  • 2,661
  • 4
  • 33
  • 45
5
votes
1 answer

How to fix "Invalid read of size 8 - 40 bytes inside a block of size 64 free'd"

The shared_ptr in SPacket in m_PhyToBtMap seems to cause "Invalid read of size 8 - 40 bytes inside a block of size 64 free'd." Note: this ran for almost 22 hours with millions of messages before valgrind (log below) issued this error message but I'm…
jacknad
  • 13,483
  • 40
  • 124
  • 194
5
votes
2 answers

Using make_shared with a protected constructor + abstract interface

Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory - to implement a DI pattern), how can I make use of make_shared in…
Robinson
  • 9,666
  • 16
  • 71
  • 115
5
votes
1 answer

How do I call make_shared or make_unique with a templated Constructor

How can I call make_shared or make_unique on a class that has a templated constructor? Here's an example: class A { /// constructor shared ptr A(shared_ptr x) ... /// constructor that creates new shared ptr template A()…
DanielGr
  • 191
  • 1
  • 6
1
2
3
9 10