Smart pointer in C++

Problem

Write a smart pointer (smart_ptr) class.

Solution

As discussed here, smart pointers are C++ objects that simulate simple pointers by implementing operator-> and the unary operator*. In addition to sporting pointer syntax and semantics, smart pointers often perform useful tasks—such as memory management or locking—under the covers, thus freeing the application from carefully managing the lifetime of pointed-to objects. You can read more about it on the provided link. Also, if you want to go into the details - refer http://ootips.org/yonat/4dev/smart-pointers.html.

Lets answer the solution:

template <class T>  
class smart\_ptr {  
    T \* ptr;  
    int count;  
    public:  
    smart\_ptr() {  
        ptr = new T;  
        count = 1;  
    }  
       
    ~smart\_ptr() {  
        count = count - 1;  
        if(count == 0)  
            delete ptr;  
    }  
       
    smart\_ptr(const smart\_ptr & other) {  
        ptr = other.ptr;  
        count++;  
    }  
       
    smart\_ptr operator=(const smart\_ptr & other) {  
        smart\_ptr result = new smart\_ptr(other);  
        return result;  
    }  
};  

Smart_ptr is the same as a normal pointer, but it provides safety via automatic memory. It avoids dangling pointers, memory leaks, allocation failures etc. The smart pointer must maintain a single reference count for all instances.

template <class T> class SmartPointer {  
public:  
    SmartPointer(T \* ptr) {  
        ref = ptr;  
        ref\_count = (unsigned\*)malloc(sizeof(unsigned));  
        \*ref\_count = 1;  
    }  
       
    SmartPointer(SmartPointer<T> & sptr) {  
        ref = sptr.ref;  
        ref\_count = sptr.ref\_count;  
        ++\*ref\_count;  
    }  
       
    SmartPointer<T> & operator=(SmartPointer<T> & sptr) {  
        if (this != &sptr) {  
            ref = sptr.ref;  
            ref\_count = sptr.ref\_count;  
            ++\*ref\_count;  
        }  
        return \*this;  
    }     
       
    ~SmartPointer() {  
        --\*ref\_count;  
        if (\*ref\_count == 0) {  
            delete ref;  
            free(ref\_count);  
            ref = NULL;  
            ref\_count = NULL;  
        }  
    }  
       
    T & operator\*() {return \*ptr;}  
    T \* operator->() {return ptr;}  
protected:    
    T \* ref;  
    unsigned \* ref\_count;  
};  

The way that we are using a unsigned pointer to the reference counting is similar as declare it to be static in Java, i.e., all the instances of smart_ptr will share one single copy of the reference counting.

**References **


See also