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
22
votes
4 answers

Assigning existing values to smart-ptrs?

I am just learning about smart pointers, and I am having trouble assigning a pre-existing location of a variable to the standard library's shared pointer. For example, lets say you have an int x, which you do not know the value of. With normal…
bluepanda
  • 351
  • 1
  • 2
  • 7
22
votes
5 answers

What is the maximum reference count in std::shared_ptr? What happens if you try to exceed it?

If we assume that std::shared_ptr stores a reference count (which I realize the standard does not require, but I am unaware of any implementations that don't), that reference count has a limited number of bits, and that means there is a maximum…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
21
votes
3 answers

Is there a way to make member function NOT callable from constructor?

I have member function (method) which uses std::enable_shared_from_this::weak_from_this() In short: weak_from_this returns weak_ptr to this. One caveat is it can't be used from constructor. If somebody would use my function from constructor of…
Korri
  • 571
  • 3
  • 10
21
votes
9 answers

shared_ptr Real life use-cases

shared_ptr is to be used when we have a scenario where it is desirable to have multiple owners of a dynamically allocated item. Problem is, I can't imagine any scenario where we require multiple owners. Every use-case I can image can be solved with…
code
  • 1,056
  • 10
  • 22
21
votes
2 answers

Why allow shared_ptr?

This answer cites N4082, which shows that the upcoming changes to std::shared_ptr will allow both T[] and T[N] variants: Unlike the unique_ptr partial specialization for arrays, both shared_ptr and shared_ptr will be valid and both will…
monkey0506
  • 2,489
  • 1
  • 21
  • 27
21
votes
3 answers

How does shared_ptr<> safely allow casting to bool?

I was looking into how std::tr1::shared_ptr<> provides the ability to cast to bool. I've got caught out in the past when trying to create a smart pointer that can be casted to bool as the trivial solution, ie operator bool() { return…
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
21
votes
2 answers

Is there a way to cast shared_ptr to shared_ptr?

I want to keep the smart behavior of std::shared_ptr. So is there a way to cast a shared void pointer to another type while without confusing the reference counting? I can't get the raw pointer and create a new shared pointer from it.
danijar
  • 32,406
  • 45
  • 166
  • 297
21
votes
3 answers

Why does enable_shared_from_this have a non-virtual destructor?

I have a pet project with which I experiment with new features of C++11. While I have experience with C, I'm fairly new to C++. To train myself into best practices, (besides reading a lot), I have enabled some strict compiler parameters (using GCC…
Stéphan Kochen
  • 19,513
  • 9
  • 61
  • 50
21
votes
2 answers

Does std::make_shared() use custom allocators?

Consider this code: #include #include class SomeClass { public: SomeClass() { std::cout << "SomeClass()" << std::endl; } ~SomeClass() { std::cout << "~SomeClass()" << std::endl; } void*…
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
20
votes
3 answers

enable_shared_from_this - empty internal weak pointer?

I'm using enable_shared_from_this and then inherit from Base. When trying to use shared_from_this() in Derived's constructor (not initializer list), I get an exception. Turns out that the internal weak pointer is null and doesn't point to this…
Puppy
  • 144,682
  • 38
  • 256
  • 465
20
votes
7 answers

What's the best strategy for typedef'ing shared pointers?

I have a quick question regarding the use of typedefs for lengthy templates. The crux: I've found myself in something of a pickle—there doesn't seem to be a good place to place typedefs except local to client functions. While there are similar SO…
Marc
  • 233
  • 2
  • 7
20
votes
6 answers

Create shared_ptr to stack object

In my method a Player object is created like: Player player(fullName,age); My teacher gave us a piece of code with a constructor that takes a shared_ptr to a player object. //constructor of the class SomeClass(const std::shared_ptr client,…
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
20
votes
3 answers

Atomic shared_ptr for lock-free singly linked list

I'm wondering if it is possible to create a lock-free, thread-safe shared pointer for any of the "common" architectures, like x64 or ARMv7 / ARMv8. In a talk about lock-free programming at cppcon2014, Herb Sutter presented a (partial) implementation…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
20
votes
3 answers

std::shared_ptr upcasting to base class - best method?

Which conversion is better, and what is the difference? class Base {}; class Derived : public Base, public std::enable_shared_from_this {}; int main(int argc, const char * argv[]) { std::shared_ptr ptr1 =…
Piotr Wach
  • 913
  • 2
  • 9
  • 22
20
votes
4 answers

Examine boost shared_ptr with gdb

Following is my source code: #include #include class MyClass { public: MyClass() { i=10; } private: int i; }; int main(int argc, const char *argv[]) { …
m.divya.mohan
  • 2,261
  • 4
  • 24
  • 34