Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C++ smart pointer implementations keep the reference counter on the heap together with the pointee?

Reading Alexandrescu and wikipipidia I see the pointee and the reference counter are stored on the heap. Then there is mention that reference counting is inefficient as counter must be allocated on the heap? Why isn't it stored on the stack?

like image 445
Claudio Torres Avatar asked Feb 14 '12 19:02

Claudio Torres


People also ask

Do smart pointers allocate on the heap?

As shown in the example, a smart pointer is a class template that you declare on the stack, and initialize by using a raw pointer that points to a heap-allocated object.

Which smart pointer should be used when you want shared ownership but not reference counting?

If you find that you need to share data or utilize reference counting, you should use a std::shared_ptr . If you need to reference shared data but don't want to contribute to the reference count, use a std::weak_ptr .

What is a role of the reference counter in shared_ptr?

The shared reference counter counts the number of owners. Copying a std::shared_ptr increases the reference count by one. Destroying a std::shared_ptr decreases the reference count by one. If the reference count becomes zero, the resource will automatically be released.

What is smart pointer when should we use it asked me to implement unique_ptr of my own?

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. 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 .


3 Answers

Because you would lose it as soon as the current instance of the smart pointer goes out of scope.

A smart pointer is used to simulate automatic storage objects that were allocated dynamically. The smart pointers themselves are managed automatically. So when one is destroyed, anything it stores in automatic storage is also destroyed. But you don't want to lose the reference counter. So you store it in dynamic storage.

like image 75
Luchian Grigore Avatar answered Oct 12 '22 21:10

Luchian Grigore


It can't be stored on the stack because then a copy of the object would also result in a copy of the refcount, which would defeat its purpose.

like image 41
StackedCrooked Avatar answered Oct 12 '22 22:10

StackedCrooked


As others have pointed out, the stack isn't an appropriate place to keep the reference count because the object may outlive the current stack frame (in which case the reference count would go away!)

It's worth noting that some of the inefficiencies associated with putting the reference count on the heap can be overcome by storing it "together" with the object itself. In boost, this can be accomplished by using boost::make_shared (for shared_ptr's) or boost::intrusive_ptr.

like image 38
Edward Loper Avatar answered Oct 12 '22 20:10

Edward Loper