C++ Articles - Pointers and Smart Pointers
 
Pointers

It is a concept in C and C++.  A Pointer is an address, which is pointing to some other Address.

Usually we create some variable and which holds some value.

A pointer holds the address of some ordinary variables. Using pointer we can access or modify the value of the variable to which this pointer is pointing.
Syntax:

Declaration:

           Int *ptr
           Double *ptr;
           Char* ptr;

Initialization:

          C  Syntax:

           Int * ptr = ( int* ) malloc ( sizeof ( int )  ;      

This will allocate raw memory of size integer type( 32-bit systems 4 bytes) from heap.
          Once the usage of the pointer is done this allocated memory needs to be  freed so that it can be used by other part of  program. This can be done using

            Free(ptr);

Note-          Raw memory needs to be type casted explicitly to the pointer type.

          OR

          We can make that pointer to point some integer type.

          Int number = 10;

          Int *ptr = &number;

           C++ Syntax:

           Int *ptr = new int;

           Along with allocating raw memory of size integer type( 32-bit systems 4 bytes) from heap, it also calls the constructor for that particular class in case of User defined and other class pointers.

Once the usage of the pointer is done this allocated memory needs to be  freed so that it can be used by other part of  program. This can be done using

Delete ptr;

Note-          No need to be type casted explicitly.


Smart Pointers

The C, C++ pointers explained above are called and “DUMB” pointers. This is because pointers needs to be initialization and deleting  (memory freeing) needs to be explicitely done by user.  If some how user fails to delete the memory and goes out of scope some memory will be leaked i. e. reserved and can not be used throughout the program.

           They just point to location without any concerns.

Example code:

Class MyPointers {

        Public:

 Void PerformTask();
};


MyPointers *pMP;


If ( pMP ) {               

  pPMP-> PerformTask ();
}

 On creation of pointers it’s not NULL but it points to some unknown location. Thus code written above may cause significant problems.

This is why pointer needs to be initializing properly along with creation. This is where smart pointers come into picture.


Pointers with Brain

Smart pointer is nothing but a C++ class wrapper around pointer. By providing this wrapper we can make sure certain operations are carried out all the time. For example one can check always if pointer is initialized properly so that we can avoid the system crashes.

Commonly templates are used for implementing smart pointers.

Code Example:

Template<class T>  class SmartPointer  {
 T  *  m_pPointer;

 Public:

        SmartPointer ( T * pPointer ) {

          ASSERT ( pPointer != NULL );
          m_pPoitner = pPointer;
       }

        SmartPointer &  operator=(const SmartPointer & pPointer) {

                Return *this;
        }

       T*  operator->()const {

        Return m_pPointer;  
       }
}


using Smart Pointer:

void main()   {

      SmartPointer<MyPointer> mySmartPtr ( & pMP ) ;
      pMP.PerformTask();    // provide by default as template object
      pMp->PerfomrTask();  // overloaded operator
}