Reference Counting Pointers

Started by
7 comments, last by EmrldDrgn 16 years, 1 month ago
I've heard a lot about reference counting "smart pointers" as a way to prevent memory leaks, and I'm a little confused. I'm envisioning something that basically holds a regular ("dumb") pointer and an int, which is incremented when a new instance of the class is created. What I'm confused about is, how does the pointer know when either a) a new object of the class is created or b) another smart pointer goes out of scope, without the use of some global object. Can anyone explain this to me?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Advertisement
a) you assign the new object to it.
b) it uses a common object, not a global one. The common object is passed around when you copy the smart pointer.

It depends on the programming language. In C++ reference counted smart pointers are class objects and the reference counts are adjusted whenever the constructor, destructor or assignment operators of the smart pointer objects are invoked. In some other languages, the reference counts are managed by the language runtime.
Well, you could have a variable within the class, lets call x, whenever you attach another reference to your pointer you could increment x by 1. Then when, your smart pointer goes out of scope,you could decrement x(in your destructor), then you'd check if x is 0. If x is 0, then nothing else is referencing to your data, and you can delete, else, someone else is referencing, and you wouldnt delete it.

Hope that helps.
Note: I am assuming you are talking about implementing a reference counted pointer in C++.

First point: if you choose to write one yourself, once you are done throw it away and use a tried and tested version, like boost::shared_ptr

The reference-counted-pointer does not hold a pointer and an int. Instead, it holds a pointer to the object *and* a *pointer* to an integer. The integer, because it is referenced through the pointer, is shared among all the reference-counted-pointers just like the object. A simple example:
template<class Type>class shared_ptr{public:    shared_ptr()    :        pointer(0),        count(0)    {    }    shared_ptr( Type *ptr )    :        pointer(ptr),        count(0)    {        if(pointer)        {            count = new int(1);        }    }        shared_ptr( const shared_ptr &other )    :        pointer(other.pointer),        count(other.count)    {        if(pointer)        {            ++(*count);        }    }    // operator=() too    ~shared_ptr()    {        if(pointer)        {            --(*count);            if((*count) == 0)            {                 delete pointer;                 delete count;            }        }    }private:    Type *pointer;    int *count;};


The above was neither compiled nor tested in any way could easily have an error in it, it is just to show the idea.
Quote:Original post by rip-off
The above was neither compiled nor tested in any way could easily have an error in it, it is just to show the idea.


And just to show how hard it is to get right: the Type * constructor in that source is not exception safe.
Thank you all for the explainations - this is just what I wanted. Yes, I was talking about a reference pointer implementation in C++, and the idea of storing a pointer to the reference count hadn't occurred to me.

I know I should use boost:: pointers, this is really just a learning exercise. I like to understand at least one way a given concept could work before I make heavy use of it. This makes sense though - thanks.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
Of course, the source code for all the boost libraries is freely available. You can just look at how boost implements it's shared_ptr class.
Yes, but reading and understanding are sometimes very different things, especially for those of us who are less-experienced. I like to implement my own... this is just how I learn things.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.

This topic is closed to new replies.

Advertisement