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
13
votes
1 answer

Boost shared_ptr dereference cost

I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr. On the dereferencing part, I expected shared_ptr and raw_ptr to be equal, but results show that shared_ptr is about twice as slow. For the test, I am…
Bart Janssens
  • 186
  • 1
  • 7
13
votes
1 answer

Is a virtual destructor needed for your Interface, if you always store it in a shared_ptr?

Since boost::/std::shared_ptr have the advantage of type-erasing their deleter, you can do nice things like #include typedef std::shared_ptr gc_ptr; int main(){ gc_ptr p1 = new int(42); gc_ptr p2 = new float(3.14159); gc_ptr…
Xeo
  • 129,499
  • 52
  • 291
  • 397
13
votes
3 answers

Downcasting shared pointer to derived class with additional functionality - is this safe?

Consider the following outline: class Base { /* ... */ }; class Derived : public Base { public: void AdditionalFunctionality(int i){ /* ... */ } }; typedef std::shared_ptr pBase; typedef std::shared_ptr pDerived; int…
jedwards
  • 29,432
  • 3
  • 65
  • 92
13
votes
2 answers

How to do function overloading with std::shared_ptr and another type of std::shared_ptr?

Try out this following code: #include #include class C { public: void F(std::function)>){} void F(std::function)>){} }; int main(){ C c; …
Ale Morales
  • 2,728
  • 4
  • 29
  • 42
13
votes
2 answers

Does make_shared do a default initialization (zero-init) for each member variable

Take an ordinary struct (or class) with Plain Old Data types and objects as members. Note that there is no default constructor defined. struct Foo { int x; int y; double z; string str; }; Now if I declare an instance f on the stack…
selbie
  • 100,020
  • 15
  • 103
  • 173
13
votes
9 answers

Fully thread-safe shared_ptr implementation

Does anybody know of a fully thread-safe shared_ptr implementation? E.g. boost implementation of shared_ptr is thread-safe for the targets (refcounting) and also safe for simultaneous shared_ptr instance reads, but not writes or for read/write. (see…
Magnus Hiie
  • 494
  • 1
  • 4
  • 14
13
votes
4 answers

Can you use a boost::shared_ptr as a key for a map?

I may need to rethink my overall design a bit more, but as it stands, it looks like I may want to do something like: class A; class B; std::map, B> APtrToBMap; I've tried this, and it does seem to work in a simple case…
Taeolas
  • 265
  • 2
  • 7
13
votes
3 answers

How to pass a default parameter for std::shared_ptr

I have a function of type virtual void foo(bla, bla, bla, std::shared_ptr logger) = 0; And I want to pass a default parameter with NULL pointer, something like: virtual void foo(bla, bla, bla, std::shared_ptr
Eric Abramov
  • 462
  • 4
  • 19
13
votes
1 answer

Does enable_shared_from_this and make_shared provide the same optimization

As I understand make_shared(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from_this provides the same optimization? So: class T :…
Dmitry Poroh
  • 3,705
  • 20
  • 34
13
votes
1 answer

Differences between tr1::shared_ptr and boost::shared_ptr?

Are there any difference between tr1::shared_ptr and boost::shared_ptr? If so, what?
Paul Merrill
  • 580
  • 4
  • 14
13
votes
6 answers

Why does copying a const shared_ptr& not violate const-ness?

Even though my code compiles fine, this is something that has been bugging me, and I wasn't able to find an answer on stackoverflow. The following generic constructor is one way to pass a shared_ptr to a class instance in the constructor. MyClass…
Thomas
  • 4,696
  • 5
  • 36
  • 71
13
votes
3 answers

Suicide object implementation leveraging `std::weak_ptr`

I'm considering using "suicide objects" to model entities in a game, that is, objects able to delete themselves. Now, the usual C++03 implementation (plain old delete this) does nothing for other objects potentially refering to the suicide object,…
Quentin
  • 62,093
  • 7
  • 131
  • 191
13
votes
3 answers

Disadvantages of shared_ptr

With shared_ptr included in c++11, one could achieve a semi garbage-collected enviroment. Does the (inflationary?) usage come along with some disadvantages? I could imagine a class model, where you create a class in which you typedef your class at…
Matze
  • 533
  • 7
  • 16
13
votes
2 answers

Smart pointers as map key

I have the following code to test smart pointer as key for std::map, I run the code on Mac and Linux, but I observed different output, is it a bug or have I done anything wrong? #include #include #include #include…
neevek
  • 11,760
  • 8
  • 55
  • 73
13
votes
3 answers

c++ copy constructor with shared_ptr members

From cplusplus.com: Rarely you will come across a class that does not contain raw pointers yet the default copy constructor is not sufficient. An example of this is when you have a reference-counted object. boost::shared_ptr<> is …
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79