How Do Shared Pointers Work

How do shared pointers work?

Basically, shared_ptr has two pointers: a pointer to the shared object and a pointer to a struct containing two reference counts: one for "strong references," or references that have ownership, and one for "weak references," or references that don't have ownership.

When you copy a shared_ptr, the copy constructor increments the strong reference count. When you destroy a shared_ptr, the destructor decrements the strong reference count and tests whether the reference count is zero; if it is, the destructor deletes the shared object because no shared_ptrs point to it anymore.

The weak reference count is used to support weak_ptr; basically, any time a weak_ptr is created from the shared_ptr, the weak reference count is incremented, and any time one is destroyed the weak reference count is decremented. As long as either the strong reference count or the weak reference count is greater than zero, the reference count struct will not be destroyed.

Effectively, as long as the strong reference count is greater than zero, the shared object will not be deleted. As long as the strong reference count or the weak reference count is not zero, the reference count struct will not be deleted.

What is a smart pointer and when should I use one?

UPDATE

This answer is rather old, and so describes what was 'good' at the time, which was smart pointers provided by the Boost library. Since C++11, the standard library has provided sufficient smart pointers types, and so you should favour the use of std::unique_ptr, std::shared_ptr and std::weak_ptr.

There was also std::auto_ptr. It was very much like a scoped pointer, except that it also had the "special" dangerous ability to be copied — which also unexpectedly transfers ownership.

It was deprecated in C++11 and removed in C++17, so you shouldn't use it.

std::auto_ptr<MyObject> p1 (new MyObject());
std::auto_ptr<MyObject> p2 = p1; // Copy and transfer ownership.
// p1 gets set to empty!
p2->DoSomething(); // Works.
p1->DoSomething(); // Oh oh. Hopefully raises some NULL pointer exception.

OLD ANSWER

A smart pointer is a class that wraps a 'raw' (or 'bare') C++ pointer, to manage the lifetime of the object being pointed to. There is no single smart pointer type, but all of them try to abstract a raw pointer in a practical way.

Smart pointers should be preferred over raw pointers. If you feel you need to use pointers (first consider if you really do), you would normally want to use a smart pointer as this can alleviate many of the problems with raw pointers, mainly forgetting to delete the object and leaking memory.

With raw pointers, the programmer has to explicitly destroy the object when it is no longer useful.

// Need to create the object to achieve some goal
MyObject* ptr = new MyObject();
ptr->DoSomething(); // Use the object in some way
delete ptr; // Destroy the object. Done with it.
// Wait, what if DoSomething() raises an exception...?

A smart pointer by comparison defines a policy as to when the object is destroyed. You still have to create the object, but you no longer have to worry about destroying it.

SomeSmartPtr<MyObject> ptr(new MyObject());
ptr->DoSomething(); // Use the object in some way.

// Destruction of the object happens, depending
// on the policy the smart pointer class uses.

// Destruction would happen even if DoSomething()
// raises an exception

The simplest policy in use involves the scope of the smart pointer wrapper object, such as implemented by boost::scoped_ptr or std::unique_ptr.

void f()
{
{
std::unique_ptr<MyObject> ptr(new MyObject());
ptr->DoSomethingUseful();
} // ptr goes out of scope --
// the MyObject is automatically destroyed.

// ptr->Oops(); // Compile error: "ptr" not defined
// since it is no longer in scope.
}

Note that std::unique_ptr instances cannot be copied. This prevents the pointer from being deleted multiple times (incorrectly). You can, however, pass references to it around to other functions you call.

std::unique_ptrs are useful when you want to tie the lifetime of the object to a particular block of code, or if you embedded it as member data inside another object, the lifetime of that other object. The object exists until the containing block of code is exited, or until the containing object is itself destroyed.

A more complex smart pointer policy involves reference counting the pointer. This does allow the pointer to be copied. When the last "reference" to the object is destroyed, the object is deleted. This policy is implemented by boost::shared_ptr and std::shared_ptr.

void f()
{
typedef std::shared_ptr<MyObject> MyObjectPtr; // nice short alias
MyObjectPtr p1; // Empty

{
MyObjectPtr p2(new MyObject());
// There is now one "reference" to the created object
p1 = p2; // Copy the pointer.
// There are now two references to the object.
} // p2 is destroyed, leaving one reference to the object.
} // p1 is destroyed, leaving a reference count of zero.
// The object is deleted.

Reference counted pointers are very useful when the lifetime of your object is much more complicated, and is not tied directly to a particular section of code or to another object.

There is one drawback to reference counted pointers — the possibility of creating a dangling reference:

// Create the smart pointer on the heap
MyObjectPtr* pp = new MyObjectPtr(new MyObject())
// Hmm, we forgot to destroy the smart pointer,
// because of that, the object is never destroyed!

Another possibility is creating circular references:

struct Owner {
std::shared_ptr<Owner> other;
};

std::shared_ptr<Owner> p1 (new Owner());
std::shared_ptr<Owner> p2 (new Owner());
p1->other = p2; // p1 references p2
p2->other = p1; // p2 references p1

// Oops, the reference count of of p1 and p2 never goes to zero!
// The objects are never destroyed!

To work around this problem, both Boost and C++11 have defined a weak_ptr to define a weak (uncounted) reference to a shared_ptr.

When you use shared pointers what is the mechanism that releases the memory from the shared pointer itself

The place where you store the shared pointer is up to you, the same way any other variable.

For instance, if you have it as a local variable in a function, then it will likely be held in the stack.

How to Manually call a Destructor on a Smart Pointer?

You can release the ownership of the object by calling reset() on the shared_ptr. If that is the last one holding the pointer, the shared_ptr's deleter member will be used to destroy the object.

https://en.cppreference.com/w/cpp/memory/shared_ptr/reset

How to correctly use smart pointers inside of a class

You do not need to use shared_ptr<>.

Actually smart pointer are here to 'solve' ownership on object, thus when an object has a single owner, unique_ptr<> should be used, and when ownership is shared, shared_ptr are used. In you situation, the ownership is clear, each node owns its left and right members, thus unique_ptr can be used.

For the tree traversal problem, don't mess with smart pointers as you are not requesting any ownership, but just looking at values, thus raw pointers are ok.

So you may end up with something like this:

#include <memory>
#include <iostream>

struct binaryNode {
binaryNode(int value) : value(value) {}
void insert(int value);

int value = 0;
std::unique_ptr<binaryNode> right;
std::unique_ptr<binaryNode> left;
};

void binaryNode::insert(int value){

binaryNode* node = this;
while(true){
if(value > node->value){
if(node->right != nullptr){
node = node->right.get();
}else{
node->right = std::make_unique<binaryNode>(value);
break;
}
}else if(value < node->value){
if(node->left != nullptr){
node = node->left.get();
}else{
node->left = std::make_unique<binaryNode>(value);
break;
}
}else{
return;
}
}
}

void printTree(const binaryNode &node){
std::cout << node.value << std::endl;
if (node.left)
printTree(*node.left);
if (node.right)
printTree(*node.right);
}

int main(){
auto bn = std::make_unique<binaryNode>(9);
bn->insert(4);
bn->insert(20);
bn->insert(1);
bn->insert(6);
bn->insert(15);
bn->insert(170);
printTree(*bn);
return 0;
}

You may notice that the print does not need to take a pointer, it can work on reference.

How does a reference-counting smart pointer's reference counting work?

I've seen two different non-intrusive approaches to this:

  1. The smart pointer allocates a small
    block of memory to contain the
    reference counter. Each copy of the
    smart pointer then receives a
    pointer to the actual object and a
    pointer to the reference count.
  2. In addition to an object pointer,
    each smart pointer contains a
    previous and next pointer, thereby
    forming a doubly-linked list of
    smart pointers to a particular
    object. The reference count is
    implicit in the list. When a smart
    pointer is copied, it adds itself to
    the list. Upon destruction, each
    smart pointer removes itself from
    the list. If it's the last one in
    the list it then frees the
    referenced object as well.

If you go here and scroll to the bottom, there is an excellent diagram which explains these methods much more clearly.



Related Topics



Leave a reply



Submit