RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    Auto Pointers

    Pointers are still a nightmare for most of the C++ beginners. The confusion where to deallocate, deep copy or shallow copy results in memory leaks and occasional program crashes. A possible remedy to this is auto pointers.

    Image courtesy : http://www.usscouts.org
    

    Auto pointers provide some kind of automatic garbage collection. That means the programmer doesn’t have the headache of deleting the pointer, thereby ensuring that there won’t be memory leaks due to his carelessness.

    Unlike normal pointers, there can be only one owner for an auto pointer at a time, though ownership can be transferred.

    An auto pointer can be created using the syntax

    auto_ptr<T> variable name (new  T), where T is the class type to which the pointer belongs.

    An example is

    auto_ptr<int> myInt(new int(10));

    To transfer the ownership, we can simply assign the above to another auto_ptr variable.

    auto_ptr<int> myNewInt = myInt;

    In this case, myNewInt points to the location where 10 is stored and myInt = NULL:

    To assign an auto_ptr to a normal pointer, we can use the auto_ptr method release. Hence, the above functionality can be obtained in the following way also.

    int* myNormalInt = myInt.release();

    On calling release, the auto_ptr is set to NULL where as the memory location pointed by it is not destroyed, but assigned to the new variable.

    To access the memory location pointed by the auto_ptr variable, we can use the method get().

    A small example is described below.

    First we create a simple class

    
    // A simple class
    class CMyClass
    {
    public:
       // Constructor
       CMyClass(int no)
       :m_No(no)
       {
       }
       // Display the member variable
       void Display()
       {
          cout << "No is << " << m_No << "\n";
       }
    private:
       int m_No;
    };
    

    Now various operations using auto_ptr are shown below.

    
    #include "stdafx.h"
    #include <iostream>
    #include <memory>
    #include "assert.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       // Create an auto_ptr
       auto_ptr<CMyClass> myClass(new CMyClass(10));
       // Assign the auto pointer to a new auto_ptr
       auto_ptr<CMyClass> myNewClass = myClass;
       // myClass will be NULL now
       assert(myClass.get() == NULL);
       // myNewClass now points to the location
       // where myClass pointed to
       myNewClass->Display();
       // myNewClass can be assigned to a normal
       // pointer as below
       CMyClass* myNormalClass = myNewClass.release();
       // myNewClass now points to NULL
       assert(myNewClass.get() == NULL);
       // myNormalClass now points to the location
       // myNewClass pointed to
       myNormalClass->Display();
       // Finally, we can delete myNormalClass.
       // Note that the auto_ptr variables do
       // not need deletion
       delete myNormalClass;
       myNormalClass = NULL;
       return 0;
    }
    

    Leave a Reply