Sunday 15 May 2011

Intoduction and implemntation to Smart Pointer in C++


Smart  Pointer in C++(in genral Computer Science) is an Object which simulates a pointer. As the name suggests Smart Pointers are Smart i.e they have all the flexibility of a pointer and also have the features of a object like constructors and destructors. The features of an Object can be used to introduce the automatic garbage collection for a Smart Pointer and also retaining all the features of a pointer.Smart isn't it?

Use of smart pointers helps us solve all the conventional problems we had to takecare when programming with pointers.It also helps prevent memory leaks and danglling pointers.

The below code provides an simple implementation of smart pointer class and how it can be used.
The code has been written using templates to make the class SmartPointer genric and usable for multiple data types. The use of the SmartPointer has been explained by taking simple float data type. But the SmartPointer implementation is not just restricted to the basic data types it can be used for custom class objects aswell.

 1 
 2 template <class T>
 3 class SmartPointer
 4 {
 5 private:
 6         T *pointerData;
 7 
 8 public:
 9         SmartPointer(T* pData) //constructor 
10         {
11                 pointerData = pdata;
12         }
13 
14         ~SmartPointer() //Destructor
15         {
16 
17                 //isValid() helps us prevent the destructor being called by 
multiple refrence for the same pointer.
18                 if(isValid())
19                 {
20                         delete pointerData;
21                         release(); //pointerData has to be made Null for 
isValid() function to serve its purpose.
22                 }
23         }
24 
25         T& operator* ()
26         {
27                 return *pointerData;
28         }
29 
30         T* operator-> ()
31         {
32                 return pointerData;
33         }
34 
35         bool isValid()
36         {
37                 //its usually a good practice to use null on LHS than RHS,
 as it avoids unintentionnal null assignment.
38                 if(NULL != pointerData)
39                 {
40                         return true;
41                 }
42                 else
43                 {
44                         return false;
45                 }
46         }
47 
48         T* getPointer()
49         {
50                 return *pointerData;
51         }
52 
53         void release()
54         {
55                 pointerData = NULL;
56         }
57 
58 }
59 
60 int main()
61 {
62         //Declare a float and dynamically allocate the object using the 
smart pointer class
63         SmartPointer<float> floatPointer(new float);
64 
65         *floatPointer = 3.4; //Value is assigned to the above declared float
66 
67         return 0;
68 }//The smart pointer is out of scope and the stack unwinds calling the
 destructor of SmartPointer class. 
69   

More to come Smart Pointers with Reference counting(coding it now :P)

Something worth noting when using Smart pointers in ur design is Smart pointers objects shouldn't be stored in STL containers. The reason being The C++ Standard, it  says that an STL element must be "copy-constructable" and "assignable." This means you will be able to safely assign or copy one object to another and get two independent and logically identical copies i.e  the state of the original object shouldn't change when it is copied to a target object. but in the case of Smart pointers copying or assigning one smartpointer to another makes changes to the original and also the changes reflect from the copy(Obvious as both refer to same object). 

No comments:

Post a Comment