Joint Implementation of Reference Counting Smart Pointer

After going through some implementation for references to smart pointer counting, I found this type of implementation.

template<typename Type> 
class SmartRefCountPointer{
    Type* obj;
    size_t* count;  // <<--- Why pointer/ why is count on heap
}

      

Can you explain why this counter is being moved to the heap and not the stack? I would really appreciate if you can give any waiver.

+3


source to share


4 answers


The counter must be shared with other instances SmartRefCountPointer

that point to the same object.



The whole point of pointers counted by ref is that there is one place to keep track of the number of references. Thus, this single location must be a global variable or a heap location. The implementation you are showing is selected for a later release.

+2


source


Here is one of the implementations I found for links counting smart pointers in one of the books.

template <typename T> class SmartPointer {
public:
    explicit SmartPointer(T* memory);
    SmartPointer(const SmartPointer& other);
    SmartPointer& operator =(const SmartPointer& other);
    ~SmartPointer();
    T& operator * () const;
    T* operator -> () const;
private:
    struct Intermediary {
        T* resource;
        size_t refCount;
    };
    Intermediary* data;
};

      

You can see what Intermediary

is allocated on the heap, not the stack. The reason for this is as follows -



Imagine that you have two smart pointers pointing to the same share. If one of the smart pointers went out of scope and the data

type Intermediary

was defined on the stack, then it data

will be cleared and therefore the other smart pointer will not know the reference count or resource. In order for an object to data

exist even after one of the smart pointers is out of scope, you must allocate it on the heap. This way, it data

persists even when one of the smart pointers sharing the resource goes out of scope.

data

cleared when the reference count goes to 0 along with the managed resource itself.

+1


source


Anyone SmartRefCountPointer

pointing to the same object must also update the same counter. This is why you cannot keep the counter as a member of the smart pointer class and must use a pointer (or reference) for it, which can be passed to the new smart pointer to a copy. It is usually allocated by the first instance of a smart pointer, which is processed by the object pointer (or constructs it itself) and is removed when the reference count drops to zero.

+1


source


Reference Pointer Rules:

  • When you copy it the reference count increases
  • When you remove it, the reference count is decremented
  • If you assigned it to another (operator =), decrease the reference count to the one you were holding and get the counter for the one you are currently assigning and increase it.
  • If you reset to a new object, you need a new counter.
  • If at any time you decrement the reference count, it drops to 0, the underlying object needs to be deleted.

Now let's see how this works for copy and assignment: there are many copies of this reference-counted pointer, and each one has the same counter with the same value. Thus, not only is the "object" shared by having many pointers to it, but also its counter. So it is also a pointer.

When in your example I do ++ (* count), the pointer itself remains unchanged, but the value it points to has increased by 1, not just on this shared_ptr but on everyone else pointing to the same object.

My 3rd and 4th rules above (which might need to be pointed to a different counter) also show why this cannot be a link.

0


source







All Articles