Question Details

No question body available.

Tags

c++ memory-management shared-ptr smart-pointers unique-ptr

Answers (12)

May 13, 2026 Score: 4 Rep: 49,061 Quality: Medium Completeness: 50%

Use a std::uniqueptr when there is exactly one place in the code where the object needs to be destroyed. Use a std::sharedptr when you do not know which part of the code or which instance of an object will need to destroy the object. It's is all down to what the design demands. If multiple windows hold a shared resource and you do not know which order the user will close the windows, that indicates a std::sharedptr. IMO most of the time you need a std::uniqueptr because your clever design means that the resource is managed in exactly one place.

May 13, 2026 Score: 2 Rep: 129,239 Quality: Low Completeness: 40%

Performance is the wrong metric for the comparison. They are semantically different. std::uniqueptr is to be used when the managed object has a single owner. std::sharedptr is to be used when ownership is shared.

May 13, 2026 Score: 2 Rep: 129,239 Quality: Low Completeness: 50%

After almost 10 years I am working with C++11 and beyond I haven't actually met a single case where I had to use std::sharedptr.

I don't think every use of std::sharedptr can be avoided by a cleaner design, actually I doubt it. But a lazy design can definitely lead to over- and misuse of std::sharedptr.

I think a good question to ask the design is "What would I do if no smart pointers were available?" Certaintly a handwritten ref counted pointer wrapper was never the goto solution. On the other hand managing a dynamically allocated resource that is created in one place and destroyed in one place has always been a rather frequent task that often causes pain, and std::uniqueptr just hits the nail.

May 13, 2026 Score: 1 Rep: 50,878 Quality: Low Completeness: 0%

Did you try googling the question? There are many posts answering this.

May 13, 2026 Score: 1 Rep: 230,184 Quality: Low Completeness: 0%

Unique ownership versus multiple owners. The former is a zero-cost abstraction, whereas the later requires extra things to handle the shared ownership.

May 13, 2026 Score: 1 Rep: 37,744 Quality: Low Completeness: 90%

There are several duplicates discussing this, e.g.:

In a nutshell:

  • The difference is whether the ownership is held by a single owner, or shared.

  • The general guideline is to prefer std::uniqueptr when possible (it's simpler and cheaper performance-wise) and resort to std::sharedptr only when you really need to share ownership.

May 13, 2026 Score: 1 Rep: 1 Quality: Low Completeness: 40%

Check this for understand differences Differences between uniqueptr and sharedptr

And to performances I am not a specialist, you can probably find documentation like this but it will depends of use in your application
https://www.modernescpp.com/index.php/memory-and-performance-overhead-of-smart-pointer/

May 13, 2026 Score: 1 Rep: 5,530 Quality: Low Completeness: 40%

You should prefer uniqueptr as your first, best choice.

If you run into a situation that you absolutely need to have federated ownership, then sharedptr should be considered.

If you are tempted to used enablesharedfrom_this, that is a design smell to me. It means the object is self-aware of how it is owned. There are caveats and responsibilities that come along with that.

May 13, 2026 Score: 0 Rep: 28,361 Quality: Low Completeness: 0%

Seems like this should be a regular question, not an "open ended question" - assuming you want an answer as opposed to a discussion?

May 13, 2026 Score: 0 Rep: 13,282 Quality: Low Completeness: 20%

A uniqueptr doesn’t require the dynamically allocated data to be deleted. It just deletes the allocated data in the destructor.

The sharedptr has a double ptr and a reference count to control the number of references owning the pointed to data. The pointed to data is incremented on each assignment and decremented in each destructor. The allocated data is only destroyed when the reference count reaches zero.

So, despite their name is similar, the use and the complexity differs to make their use cases disjoint. You must know, based on the use case, which one is to be used in your use cases.

May 13, 2026 Score: 0 Rep: 106 Quality: Low Completeness: 10%

Our team has a policy of single threaded owning pointers are always uniqueptrs and non-owning pointers are always raw pointers, along with a policy of no naked new/deletes. For multithreaded code, if you can determine ownership statically (which may difficult or infeasible, in which case you'll need to determine if it's worth it) you should use uniqueptrs, otherwise use shared_ptr.

May 13, 2026 Score: 0 Rep: 49,061 Quality: Low Completeness: 40%

Indeed. Even in my contrived example of multiple windows for a shared resource, I would probably put it inside whatever object manages those windows and use a std::uniqueptr. Though I have seen std::sharedptr used with multiple threads, when you don't know which thread will die last so that you don't need to worry about destroying it manually afer all the threads finish.