Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

3323 questions
43
votes
2 answers

Conversion from boost::shared_ptr to std::shared_ptr?

I got a library that internally uses Boost's version of shared_ptr and exposes only those. For my application, I'd like to use std::shared_ptr whenever possible though. Sadly, there is no direct conversion between the two types, as the ref counting…
Xeo
  • 129,499
  • 52
  • 291
  • 397
43
votes
4 answers

Why are two raw pointers to the managed object needed in std::shared_ptr implementation?

Here's a quote from cppreference's implementation note section of std::shared_ptr, which mentions that there are two different pointers(as shown in bold) : the one that can be returned by get(), and the one holding the actual data within the control…
btshengsheng
  • 703
  • 5
  • 12
43
votes
4 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
  • 3,250
  • 7
  • 42
  • 73
42
votes
6 answers

How do you make std::shared_ptr not call delete()

I have functions that take in std::shared_ptr as an argument so I am forced to use std::shared_ptr, but the object I am passing to the function is not dynamically allocated. How do I wrap the object in std::shared_ptr and have std::shared_ptr not…
Ken Li
  • 2,578
  • 4
  • 25
  • 26
42
votes
2 answers

is it better to use shared_ptr.reset or operator =?

I'm trying to wrap my head around the new idioms for C++11. It seems that with shared_ptr at least, there is a substantive difference between using new T() and make_shared(). But what of resetting a shared pointer to point to a new instance of…
Mordachai
  • 9,412
  • 6
  • 60
  • 112
41
votes
4 answers

How is the std::tr1::shared_ptr implemented?

I've been thinking about using shared pointers, and I know how to implement one myself--Don't want to do it, so I'm trying std::tr1::shared_ptr,and I have couple of questions... How is the reference counting implemented? Does it use a doubly linked…
purepureluck
  • 1,271
  • 2
  • 14
  • 16
40
votes
3 answers

Why do shared_ptr deleters have to be CopyConstructible?

In C++11 std::shared_ptr has four constructors which can be passed deleter objects d of type D. The signatures of these constructors are the following: template shared_ptr(Y * p, D d); template
jotik
  • 17,044
  • 13
  • 58
  • 123
40
votes
1 answer

Using std::move with std::shared_ptr

I have a function defined as follows: void foo(std::shared_ptr x) { ... }; If I declare a shared ptr to X: std::shared_ptr sourcePtr(new X(...)); I can then call foo as follows: foo(std::move(sourcePtr)); or foo(sourcePtr); I understand…
ksl
  • 4,519
  • 11
  • 65
  • 106
40
votes
14 answers

How to release pointer from boost::shared_ptr?

Can boost::shared_ptr release the stored pointer without deleting it? I can see no release function exists in the documentation, also in the FAQ is explained why it does not provide release function, something like that the release can not be done…
user152508
  • 3,053
  • 8
  • 38
  • 58
39
votes
1 answer

intrusive_ptr in c++11

Does C++11 have something equivalent to boost::intrusive_ptr? My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use…
Aarkan
  • 3,811
  • 6
  • 40
  • 54
38
votes
1 answer

How to initialize a shared_ptr that is a member of a class?

I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution? class A { public: A(); }; class B { public: B(A*…
Igor
  • 26,650
  • 27
  • 89
  • 114
38
votes
5 answers

shared_ptr vs scoped_ptr

scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr. So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know…
Narek
  • 38,779
  • 79
  • 233
  • 389
37
votes
4 answers

Why is std::shared_ptr::unique() deprecated?

What is the technical problem with std::shared_ptr::unique() that is the reason for its deprecation in C++17? According to cppreference.com, std::shared_ptr::unique() is deprecated in C++17 as this function is deprecated as of C++17 because…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
37
votes
3 answers

shared_ptr Assertion px != 0 failed

I have a fairly complex multi threaded application (server) that from time to time will crash due to an assert: /usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr< >::operator->() const [with T =…
Horacio
  • 2,727
  • 5
  • 26
  • 29
37
votes
5 answers

Explicitly deleting a shared_ptr

Simple question here: are you allowed to explicitly delete a boost::shared_ptr yourself? Should you ever? Clarifying, I don't mean delete the pointer held by the shared_ptr. I meant the actual shared_ptr itself. I know most people suggest to not do…
meteoritepanama
  • 6,092
  • 14
  • 42
  • 55