Skip to content

shared pointers

June 21, 2012

Shared pointers allow you to hold on to several copies of the same pointer, without having to keep explicit track of membership. Rather than one object owning the pointer, a shared pointer counts the number of references there are to the actual pointer, and deletes the object when that counter reaches zero.

The declaration of the shared pointer class is:

template <class T>
class shared_ptr;

The shared pointer class is a shared pointer to something—it has a strong type just like any other pointer.

The most basic way to use the shared pointer is to create one, and then pass it around. The simplest constructor is:

template <class Y> explicit shared_ptr(Y* p);

You use this by simply writing something like:

shared_ptr<int> x(new int(5));

Note that the constructor is explicit because you don’t want to accidentally create a shared pointer by passing it to a function without realising. If it wasn’t explicit, you could pass a pointer to a function that expects as argument a shared pointer, and that would convert it to the shared pointer and suddenly your pointer that you don’t want to be managed might get deleted when you don’t want it to be.

It is preferable to use std::make_shared, which creates a shared pointer and allocates both the memory for the object and the pointer’s data block with only one call to new. The example above would be better written as:

auto x = std::make_shared<int>(5);

Another way to use a shared pointer is with a custom deleter, which is a function that you give the shared pointer to delete the actual pointer. Suppose that you had a pointer to some memory allocated with malloc, and you needed to delete it with free, you could do the following:

typedef std::shared_ptr<char> CcharPtr;

  return CcharPtr(get_c_pointer(), &free);

It is also possible to construct a shared pointer that takes its pointer from a std::unique_ptr, the construct for this looks like:

template< class Y, class Deleter > 
shared_ptr( std::unique_ptr<Y,Deleter>&& r );

Weak Pointers

A class that is associated with the shared pointer is the weak pointer. A weak pointer points to the same object as a shared pointer does, but a weak pointer doesn’t strictly “own” the pointer. If the shared pointer count gets to zero even whilst the weak pointer count is greater than zero, the object will be deleted. This can be used to break the cycle in cyclical dependencies for example. If you are using a weak pointer, you first have to check if its object has already been destroyed, then request a shared pointer of the object. For example, suppose that you have a weak pointer object p, you can do the following:

if (p.expired())
  throw "pointer already destroyed";
auto sp = p.lock();

//now use sp here
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: