Sayfalar

6 Ekim 2014 Pazartesi

Smart Pointers

Smart Pointers


            Smart pointers come with C++11 standarts.They help us to handling about memory managment topics.In modern languages there are garbage collector that manage the object life times and object's memories.In c++ when we create a object we must be delete the object , otherwise the memory which is used this object will be useless.There are four smart pointer in C++11 , but there are another smart pointers in boot libraries.Since they are not standart I wont cover in this article.

       We must give the flag -std=C++11 in GCC and add the library <memory> when we use the smart pointers.

auto_ptr

     First smart_pointer is auto_ptr.It is not as smart as other smart pointers.When we create object we must be delete when we finished job about this object.


Obje* ptr = new Obje();
ptr->Fonksiyonlar();

delete ptr ;

         But when we forget the deleting there will be extra memory usage.
When define the auto_ptr ;
auto_ptr<T>ptr(new T); We use the this declaration.T is the type of the pointer and ptr is the name.With Ptr-> we can access the object elements and functions.When a auto_ptr assign a object and this object's life time over it deleted automaticlay. It have two useful functions : reset () and release (). reset is kill the object and create a new one , but release is only finished the ownership.

prt.reset(new T); // reset usage
T* p3= ptr.release() ; // ownership finish

delete p3; // delete the pointer 


We cannot use the auto_ptr for STL containers and we shouldn't assign a auto_ptr to another one.Because when assign the first one lose the ownership.
There are a idiom for not losing ownership it is const idiom we can only add the const.

const auto_ptr<Test> pt (new Test(2)); // like a normal auto_ptr

shared_ptr

It is for sharing a object .We can assign two pointers to a object without losing a ownership.We can assign two shared_ptr but never two shared_ptr points to each other because of strong reference counting they cannot be deleted.Declaration of shared_ptr is like auto_ptr
shared_ptr<T> sptr(new T); 
We can use the shared_ptr for the arrays but we must de declare a deletion operation like ;
shared_ptr<int []>p (new int[10],[](int* p) {delete [] p;});


       We a shared object points a object strong reference will one if another one do it will be 2.When the strong reference is zero object will deleted.It is good to sharing a object.But when cyclic reference happen the object cannot be deleted because strong reference never will be 0.For example;
void main()
{
std::shared_ptr<A> x=std::make_shared<A>();


x->ptr = x;// never deleted
}



We can handle this situation with using another smart_pointer weak_ptr

weak_ptr
       weak_ptr using for breaking cyclic reference.But we(programmers) must be investigare where the cyclic reference. weak_ptr cannot be exist without a shared_ptr.weak_ptr take a shared_ptr as a parameter when it is declareted.Like;

weak_ptr<T> wptr(sptr); // sptr : a shared_ptr


We cannot use the -> or . İn weak_ptr.If a weak_ptr assign a shared_ptr strong reference wont increase.So we can the break cyclic reference.
It have two functions : lock and expired ().expired return is expired or not.lock return a shared_ptr whic it assigned.

 We can solve the danglin pointers with using weak_ptr;

unique_ptr

      Last one is unique_ptr. It is like shared_ptr but but there is no sharing thing.a unique_ptr can assign to another one.We can use the unique_ptr for arrays or contaioners without create a delete operation.It have two function reset and release. Reset is reset all thing resource,ownership but release is only finish the ownership.Here a sample code;