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
73
votes
3 answers

std::shared_ptr initialization: make_shared() vs shared_ptr(new Foo)

What's the difference between: std::shared_ptr p = std::shared_ptr( new int ); and std::shared_ptr p = std::make_shared< int >(); ? Which one should I prefer and why? P. S. Pretty sure this must have been answered already, but I…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
66
votes
1 answer

Why isn't there a std::shared_ptr specialisation?

The standard provides a template specialization of std::unique_ptr which correctly calls the delete[] from its destructor: void func() { std::unique_ptr< int[] > arr(new int[10]); ....... } With std::shared_ptr this specialisation is not…
mark
  • 7,381
  • 5
  • 36
  • 61
66
votes
3 answers

Getting a normal ptr from boost::shared_ptr?

I have something like boost::shared_ptr t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type. How do I get it from boost::shared_ptr?
user34537
66
votes
4 answers

std::shared_ptr: reset() vs. assignment

This is a basic question, but I did not find a previous post about it. The title of the following question sounds like it might be the same question as mine, but the question itself does not match the title: is it better to use shared_ptr.reset or…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
65
votes
2 answers

Error: expected type-specifier before 'ClassName'

shared_ptr circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
64
votes
5 answers

The cost of passing by shared_ptr

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following: class Dataset {...} void f( shared_ptr< Dataset const > pds ) {...} void g( shared_ptr< Dataset const…
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
63
votes
7 answers

std::shared_ptr thread safety

I've read that "Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." (MSDN: Thread Safety in the Standard C++ Library) Does that mean that changing shared_ptr…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
59
votes
2 answers

How to get the Object being pointed by a shared pointer?

I have a query. Can we get the object that a shared pointer points to directly? Or should we get the underlying RAW pointer through get() call and then access the corresponding object?
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
57
votes
4 answers

Is make_shared really more efficient than new?

I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared. As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within…
user1212354
  • 589
  • 1
  • 4
  • 6
57
votes
6 answers

NULL pointer with boost::shared_ptr?

What's the equivalent to the following: std::vector vec; vec.push_back(NULL); when dealing with boost::shared_ptr? Is it the following code? std::vector< boost::shared_ptr > vec; vec.push_back(boost::shared_ptr()); Note: I may push…
Frank
54
votes
4 answers

Should I use shared_ptr or unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr. I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively…
Clinton
  • 22,361
  • 15
  • 67
  • 163
54
votes
16 answers

What's your convention for typedef'ing shared_ptr?

I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example: typedef boost::shared_ptr FooPtr; Before settling on a convention, I'd like to see what others use. What is your convention? EDIT: To…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
53
votes
4 answers

Check for null in std::shared_ptr

I was wondering if i need to check whether sp is null before i use it. Correct me if I am wrong but creating an alias will not increase the ref counter and therefore by entering into the method we are working with a shared pointer which we don't…
user1400995
52
votes
7 answers

shared_ptr: horrible speed

When comparing two variants of pointers—classic vs. shared_ptr—I was surprised by a significant increase of the running speed of the program. For testing 2D Delaunay incremental Insertion algorithm has been used. Compiler settings: VS 2010…
Ian
  • 637
  • 2
  • 7
  • 7
52
votes
5 answers

How to pass deleter to make_shared?

Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use…
cavitsinadogru
  • 940
  • 2
  • 10
  • 17