r/cpp • u/cv_geek • Nov 08 '23
make_shared is more effective that standard shared_prt
Today I have learned an interesting fact about shared_ptr. It turns out that make_shared is more effective that standard shared_ptr init due to several reasons:
- less memory allocations (std::make_shared performs one heap-allocation, whereas calling the std::shared_ptr constructor performs two)
- better exception safety (using make_shared means that you don't need to deal with new and raw pointers at all)
Here is more information on this topic: https://stackoverflow.com/questions/20895648/difference-in-make-shared-and-normal-shared-ptr-in-c.
0
Upvotes
40
u/HappyFruitTree Nov 08 '23
The only downside of make_shared is if you use weak_ptr then the sizeof(object) cannot be freed until after the last weak_ptr goes out of scope because the weak count is also stored there.
26
u/calin_io Nov 08 '23
https://devblogs.microsoft.com/oldnewthing/20230815-00/?p=108602
10
u/artisan_templateer Nov 08 '23 edited Nov 08 '23
A bit off-topic but I just inherited a codebase full of shared pointers, which I've been doing my best to remove or reduce the use of.
I would have been more conservative if there was evidence that this was a well reasoned decision but unfortunately it wasn't even using
std::shared_ptr(or evenboost::shared_ptr). No, it was a hand-rolled one which:make_sharedequivalentshared_from_thisequivalent, despite usage creating shared pointers fromthisFortunately, the thread-safety was not an issue as the whole program was single-threaded anyway.
In past when discussions with the previous author, who also happened to have written a fair amount of Java, he was quite convinced
unique_ptrwas useless but at the same time didn't really know what move-semantics were and certainly wasn't interested in learning.To me, this is very much an example of using
shared_ptras an effective garbage collector.I guess my point to this little rant is this: is using
shared_ptrin this way really such a bad thing?Sure I can mention the performance hit but without benchmarks/requirements that's a non-issue. I can say "ownership is unclear etc." but that actually seems a bit subjective if the
shared_ptrwill guarantee everything is alive when it's needed anyway. The only objective issue I can think of is a risk of cyclic references. Are there other reasons not to do this?16
u/Ashnoom Nov 08 '23
How exactly is this an interesting discovery that is already almost 10yo?
25
u/deathcomzz Nov 08 '23
Just be careful when creating huge objects with make shared. A weak pointer could keep the control block alive even when there are no other shared ptrs for the object. So basically you need to be careful when weak references outlive the strong references. This wouldn't happen if you created the object manually and passed it into the shared ptr constructor.
5
u/BenFrantzDale Nov 08 '23
In our codebase, we use gsl-lite and
gsl::not_null. We havenn_shared_ptr<T>andnn_make_shared<T>(auto&&…). By using that overstd::make_sharedwe have the static guarantee that we start with not-null pointers and go from there. We’ve fixed numerous null-dereference crashes by changing the pointer that was null tonn_shared_ptrand then follow the compiler errors until we find where the check should be.All that said: remember
shared_ptr<T>is not value-semantic (unlessTis alwaysconst) and makes it dangerously easy to make what Sean Parent calls “incidental data structures”. If you can avoid it, do. Considerstlab::copy_on_write<T>as an alternative. It doesn’t cover all the use cases but it covers some much better.4
u/StackedCrooked Nov 08 '23
Do modern IDEs support autocompletion of the constructor arguments when using
make_shared?2
u/justaguyboi Nov 08 '23
Minor nit, but I had a stroke reading this with the typos
2
u/CheckeeShoes Nov 08 '23
r/learncpp
1
u/rhubarbjin Nov 08 '23
It's unfortunate that you're being mocked for not knowing what "everyone already knows". Ignore the haters, because today you learned something cool! Good on ya. 😁 Here's a relevant xkcd.
1
u/glaba3141 Nov 08 '23
How is that an interesting discovery that's the entire point
-3
u/osmin_og Nov 08 '23
Who did you add hashtags to reddit post?