This topic will elaborate Smart Pointer concepts and move on to Reference Counting. There is a sample code to implement the same.
Background
Smart pointer or auto-pointer is a simple wrapper around a regular pointer, provides all meaningful operations to its embedded pointer (derefencing and indirection).
It's smartness lies in it's destructor which takes care of deleting it's embedded pointer and makes user not to worry from freeing programmer dynamically allocated memory.
The beauty of the logic behind designing Smart Pointer is that the Stack is safer than Heap. Variables on the Stack are automatically destructed when they go out of scope. Whereas Variables on Heap are freed only by an explicit call to Delete.
So, the trick is to represent Heap based object, accessed through a dumb pointer by a stack based object and when this stack based object goes out of scope, its Destructor is called, which in turn takes care of freeing the memory associated with the dumb pointer.
For Example here instead of writing
How smart pointer will take care of these types of programmer errors:
int main()
{
MyClass *p = new (MyClass);
MyClass *q = p;
p->DoSomething();
delete (p);
q->DoSomething(); // q is now a dangling pointer
}
When we assign pointer p to q as in the above code and then delete pointer p, it simply deletes the object pointed by pointer "p", irrespective of there is another pointer "q" referencing to it and then corrupts the heap.
There are set of solutions to this problem. But we can solve this in a simple way by providing reference counting facility to the embedded pointer.
Reference Counting does maintain a count of all the pointers that are referencing to the object and deletes the object only when this Reference Count becomes Zero or when the last object which is using this pointer gets destroyed.
Also you can use reference counting in any class that manages a shared resource, and not just for smart pointers or auto pointers.
Logic behind Reference Counting implementation is as below:
1. When the object that is being managed is created, its reference count
is set to one.
2. When the managing object - the smart pointer - is copied or assigned,
the reference count is incremented.
3. When a smart pointer is destructed, as when a member variable's owning
object is destructed, or a value goes out of scope, or an allocated
smart pointer is deleted, the reference count is decremented.
4. Once when the reference Count becomes zero, then the pointer gets deleted.
This is also called as Shared Ownership Pattern.
Here is a sample code which implements Smart Pointer with Reference Counting:
The code
Here class Pointer is a Real Pointer, where-as class Smart Pointer is a wrapper around Pointer.
#include
class Pointer
{
public:
//standard construction and destruction:
/*default constructor */
Pointer () {
cout<<"Pointer::default constructor = \n";
}
/*Copy constructot */
Pointer (const Pointer& rhs) {
cout<<"Pointer::copy constructor = \n";
myVal_ = rhs.myVal_;
}
/*Assignment operator overload */
Pointer& operator = (const Pointer& rhs) {
cout<<"Pointer::operator = \n";
if(this != &rhs)
{
myVal_ = rhs.myVal_;
return (*this);
}
}
/*Destructor */
~Pointer (void){
cout<<"Pointer::Destructor\n";
}
/*reference counting related methods */
void acquire(void){
++refCount_;
} /* increase refCount */
unsigned int release(void){
return(--refCount_);
} /*decrease refCount */
/*accessor*/
unsigned int count(void){
cout<<"Reference count value is ="<
return (refCount_); /*return current count */
}
private:
/* Class private data */
int myVal_;
unsigned int refCount_; /* reference counting related data */
}; /* End of Pointer class */
/****** Here is our own SmartPointer class for Pointer class ****** ***/
class SmartPointer {
public:
/* standard construction and destruction: */
/* Default construction: */
SmartPointer(Pointer *ptr):ptr_(ptr){
cout<<"SmartPointer::default constructor = \n";
ptr_->acquire();
}
/* Copy construction: */
SmartPointer(const SmartPointer &rhs):ptr_(rhs.ptr_){
cout<<"SmartPointer::copy constructor = \n";
ptr_->acquire();
}
/* Assignment operator: */
SmartPointer& operator = (const SmartPointer &rhs){
cout<<"SmartPointer::operator = \n";
if (this != &rhs)
{
ptr_->release();
ptr_ = rhs.ptr_;
ptr_->acquire();
return(*this);
}
}
/* Destructor: */
~SmartPointer (void){
cout<<"SmartPointer::Destructor\n";
if (NULL != ptr_)
{ if (0 == ptr_->release())
{
ptr_->count();
delete(ptr_);
ptr_ = NULL;
}
}
}
Pointer* operator -> (void) {
return (ptr_);
}
Pointer& operator * () const {
return(*ptr_);
}
private:
Pointer * ptr_;
}; // SmartPointer
// ******* client uses SmartPointer as a Stack Variable here ********
int main()
{
{
/* sp1 acquires refCount now. so refCount = 1 */
SmartPointer sp1 = SmartPointer( new Pointer());
sp1->count();
{
//sp2 referes sp1 and acquires refCount now. so refCount = 2
SmartPointer sp2 = sp1;
sp1->count();
} // at this point sp2 relases refCount. so refCount = 1
sp1->count();
} // at this point sp1 relases refCount. so refCount = 0 and so here the ptr_ gets deleted
return(0);
}
1 comment:
yeh singh lion kya hai.
isko Singh "The Lion" bana.
Post a Comment