Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost: Initializing shared pointer reset vs make_shared

Apart of the size of generated code, what's the difference between using reset() for initializing a shared pointer over the function make_shared()?

Case 1 by using reset()

boost::shared_ptr<A> pA;
pA.reset(new A());

Case 2 by using make_shared()

boost::shared_ptr<A> pA;
pA = boost::make_shared<A>();

In general, is it a good practice to use reset over make_shared to reduce the size of executable?

like image 258
lucaboni Avatar asked Apr 07 '16 13:04

lucaboni


People also ask

Why is Make_shared more efficient?

One reason is because make_shared allocates the reference count together with the object to be managed in the same block of memory. OK, I got the point. This is of course more efficient than two separate allocation operations.

What is boost :: Make_shared?

The header file <boost/make_shared. hpp> provides a family of overloaded function templates, make_shared and allocate_shared , to address this need. make_shared uses the global operator new to allocate memory, whereas allocate_shared uses an user-supplied allocator, allowing finer control.

What is Make_shared pointer?

Make shared_ptr. Allocates and constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr<T> that owns and stores a pointer to it (with a use count of 1). This function uses ::new to allocate storage for the object.

What is the use of Make_shared?

make_shared can allocate a single block of memory to hold both of these; constructing a shared pointer from a pointer to an already-allocated object will need to allocate a second block to store the reference count.

How to create a smart pointer of boost shared_ptr?

we can create a smart pointer of type boost::shared_ptr using boost::make_shared () without having to calling the constructor of boost::shared_ptr.

What's the difference between reset() and make_shared() for initializing a shared pointer?

Apart of the size of generated code, what's the difference between using reset () for initializing a shared pointer over the function make_shared ()? In general, is it a good practice to use reset over make_shared to reduce the size of executable? make_shared is more efficient. Use that.

What is the advantage of boost make_shared ()?

The advantage of boost::make_shared () is that the memory for the object that has to be allocated dynamically and the memory for the reference counter used by the smart pointer internallycan be reserved in one chunk.

What happens when a shared_ptr is constructed from an existing pointer?

If a shared_ptr is constructed from an existing pointer that is not shared_ptr the memory for the control structure has to be allocated. This Control block is destroyed and deallocated when the last weak ref goes away. A shared_ptr construction approach takes two steps


2 Answers

reset(new T(...)) allocates a heap block, constructs the object, allocates a new heap block for the reference counter and initializes the reference counter.

make_shared<T>(...) allocates a heap block slightly larger than required for the object and constructs the object and the reference counter in the same heap block.

The chance is high that make_shared() runs faster and requires less memory.

But there is a small drawback if you are using an IDE like Microsoft Visual Studio: Intellisense is not able to show you the names of the parameters used in the constructor. The code is working correctly but editing the make_shared() call is uncomfortable.

like image 173
Andreas H. Avatar answered Oct 27 '22 11:10

Andreas H.


make_shared<T> creates the reference counter in the same chunk of memory which is allocated for T. It's an optimization. reset does not do this.

like image 34
Brandon Kohn Avatar answered Oct 27 '22 10:10

Brandon Kohn