Grown Up Pointers

Shared_pointers

#include<iostream>
int main(){
    int *b=new int(12); //initialize the pointer
    int *c;
    c=b;       //make  pointer c points to same pointer
    delete b; //delete the pointers
    std::cout<<*c;
}

Above program will produce an error on running. Why? because the object to which c was pointing got deleted will c is still pointing to it. In this case, we knew that same object was referred by c so, we could have avoided deleting object before that but in big programs, it’s difficult to check which-which object is referenced more than once and it’s difficult to code when to delete them. another option would be that we could forget about deleting the objects let them live. This would cause the problem of overpopulation (memory leak) as they will be immortal till dawn of the memory (till we shut the system of).

#include<iostream>
#include<memory>
int main(){

    std::shared_ptr b(new int(12)); //initialize the pointer
    std::shared_ptr c=b;         //make  pointer c points to same pointer
    std::cout<<b<<" "<<c<<" "<<c.use_count()<<std::endl; //use_count is used to check number of pointers pointing to same object
    b.reset(new int(1));
    std::cout<<b<<" "<<c<<" "<<c.use_count();
}

So, now We have another species of pointers that keep note of how many pointers are referring to the same object. So, what it does is it doesn’t give us right to delete objects (Although it can be done but to keep things simple let’s assume that) but it does delete itself when wants that poor object.so, We don’t have to get our hands dirty.

What shared_ptrs do that is the last pointer that is using the object takes the responsibility to delete the objects ass compared with the irresponsible normal pointer where the coders have to take care of cleaning the memory. So, we can say that shared_ptrs are the grown up version of the pointers that knows that he have to clean up their own room (dynamically allocated memory space). Plus as the shared_ptr are grown up version they take the double space.

Unique_pointers

It pointers way of saying that “Joe don’t share food”. No two unique pointers can point to the same object but you can move the ownership from one pointer to another using p1 = std::move(p2); in case of the normal pointer you can

int *p1=new int(1);
int *p2;
p2=p1;

Which is not allowed as It might create above explained problem.

Weak_pointers

#include<iostream>
int main(){
    int *b=new int(12); //initialize the pointer
    int *c;
    c=b;       //make  pointer c points to same pointer
    delete b; //delete the pointers
    std::cout<<*c;
}

It would be nice and good if for above case we would check that if object exit before deferring to them and save yourself from the segmentation fault. It would be great if that pointer has no responsibility for deleting it. It just checks whether there is an object or not. If there is object then it take tempory ownership of that object and then use them and after using them release the ownership. And here comes the weak_ptr which want to use object but take no responsibility for cleaning it up.

#include<iostream>
#include<memory>

    int main(){
    auto sp = std::make_shared(2); //make shared pointer
    std::weak_ptr gw = sp; //make weak pointer pointing to same object.
    if (!gw.expired()) {
        auto spt = gw.lock() // Has to be copied into a shared_ptr before usage
        std::cout << *spt << "\n"; //using the pointer.
    }
    else {
       std::cout << "gw is expired\n";
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s