Click here to Skip to main content
14,699,759 members
Articles » Languages » C / C++ Language » Memory Management
Posted 19 May 2008


9 bookmarked


Rate me:
Please Sign up or sign in to vote.
2.65/5 (7 votes)
19 May 2008CPOL
Implementation of Shared Pointer


smart pointers are classes that store pointers to dynamically allocated (heap) objects. they behave much like built-in c++ pointers except that they automatically delete the object pointed to at the appropriate time. smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. they can also be used to keep track of dynamically allocated objects shared by multiple owners.

one of the most important techniques of implementing smart pointers is using reference count method.most of you have rolled your own smart pointers, and there is plenty of literature regarding the issues involved in reference counting. one of the most important details is how the reference count is implemented — intrusive, which means that you add support to the classes being reference counted, or non-intrusive, which means you don’t. my implementation is non-intrusive, and the implementation uses a simple integer reference counter.


you must know the basic concepts of parameter passing of c++ to understand the reference counting mechanism. in c++,we have two types of parameter passing:

1)pass by value.
2)pass by reference.
in pass by value,a temporary copy of the data type being passed is pass be reference, the address of the parameter being passed is sent so that any changes to the passed parameter is directly reflected to the state of the passed parameter.

now,if we pass and user-defined object as a parameter to a function,the copy-constructor of the function is called to create a temporary object,which is destroyed as soon as the function returns. i have used this concept to create the shared pointer implementation for automatic garbage collection.

using the code

this is a very simple implementation of the shared pointer concept. i have used a templated class which wraps a pointer to a reference counting object. this object wraps the pointer to the class passed as the template argument.

the implementation is very simple. it has a de-referencing operator which returns the wrapped pointer object when used. whenver, the shared pointer is passed as a argument to function or any situation in which a copy constructor is involved,its reference count is incremented. this reference count is decremented in the destructor which when it reaches 0,the wrapped pointer is freed.

///this class implements the reference counting mechanism for the
template <class t> class refcounter_impl {
    int m_counter;
    t* m_obj;
    void inc_ref_count() 
    void dec_ref_count()
    t * get_ptr() const
        return ((t *)this);
    void destroy_ref() 
            delete get_ptr();
    const t& operator=(const refcounter_impl<t>& ref)
        this->m_obj = ref.m_obj;
        m_counter = ref.m_counter;
    refcounter_impl(t* obj):m_counter(0),m_obj(obj){}
    refcounter_impl(const refcounter_impl<t>& ref)
        this->m_obj = ref.m_obj;
        m_counter = ref.m_counter;
//this is the shared_ptr implementation of the class template 
<class t>
class shared_ptr
    refcounter_impl<t>* m_refcounted_obj;    
    void assign(void *ptr)
            assign((refcounter_impl<t> *)null);
            assign(new refcounter_impl<t>(static_cast<t *>(ptr)));
    void assign(refcounter_impl<t> *refcount)
        m_refcounted_obj = refcount;        
    shared_ptr() :m_refcounted_obj(null){}
    shared_ptr(t* obj) :m_refcounted_obj(null)
    shared_ptr(const shared_ptr& ref)
    shared_ptr& operator=(const shared_ptr& ref)
        return *this;
    t* operator->()
            return m_refcounted_obj->get_ptr();
            return null;



  • this is a very simple implementation of the shared pointer technique,and i have kept this deliberately for intermediate users. advanced users might not refer this article but this would extremely useful for people with 2-3 years of development experience moving on to implementing bigger things.

points of interest

  • i particularly enjoyed to explore the c++ argument passing mechanism while writing this article.
  • you can download the folder to see the complete c++ code with an example of my implementation.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) Globallogic
India India
No Biography provided

Comments and Discussions

QuestionMaybe has memory leak? Pin
Member 3145669-May-12 23:08
MemberMember 3145669-May-12 23:08 
GeneralThanks! Pin
ferahl15-Jul-09 14:54
Memberferahl15-Jul-09 14:54 
Generalas of July 2, 2008, this is still broken Pin
KevinSeghetti2-Jul-08 13:57
MemberKevinSeghetti2-Jul-08 13:57 
Generaloh! Pin
ecyrbe19-May-08 10:25
Memberecyrbe19-May-08 10:25 
GeneralRe: oh! Pin
Abhi_Coder20-May-08 3:34
MemberAbhi_Coder20-May-08 3:34 
GeneralI'm sorry, this is simply not going to work Pin
Ivo Beltchev19-May-08 8:40
MemberIvo Beltchev19-May-08 8:40 
GeneralRe: I'm sorry, this is simply not going to work Pin
Abhi_Coder20-May-08 3:32
MemberAbhi_Coder20-May-08 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.