Sunday, August 14, 2016

C++11’s Smart Pointers - Automatic memory management

Introduction:
There are three kinds of smart pointers in c++ 11.
  1. Shared pointer (shared_ptr)
  2. Weak pointer (weak_ptr)
  3. Unique pointer (unique_ptr)

  • They should be used only with heap memory, means only new constructor. if you use them with stack memory, a runtime error will occur.
  • They auto delete the object once they go out of scope (except weak pointer)
  • Avoid using raw pointers to refer to the same object. Don’t mix them together.
  • All are listed in <memory>

Shared pointer
  • Implements shared ownership, means any number of these smart pointers jointly own the object. The owned object is destroyed only when its last owning smart pointer is destroyed.
  • Once the managed object is gone, the shared_ptr = nullptr or 0.
  • A function can take and return a shared_ptr as value.
shared_ptr<Thing> do_something (shared_ptr<Thing> p)

  • reset() or nullptr: Decrements the reference count and delete the pointed-to object if required and then set shared_ptr = nullptr
class Thing {
public:
void func();
}
void foo () {
shared_ptr<Thing> p1 {new Thing}; // p1 owns Thing
shared_ptr<Thing> p1 = p1; // p1 and p2 share the ownership of Thing
p1 -> func(); //call member function like built in pointer
cout <<  *p1 // dereference just like built in pointer
p1.reset(); // decrement shared_ptr count, delete if required
p2 = nullptr; // decrement shared_ptr count, delete if required
}

  • How to get raw pointer from shared_pointer (Don’t do it, dangerous)
shared_ptr<Thing> p1 = make_shared<Thing>(); //same as new, but efficient
Thing *raw_ptr = p1.get();
Thing *raw_ptr = sp; //Error

  • Inheritance and shared_ptr
class Base { };
class Derived: public Base{ };
shared_ptr<Derived>  dp1 {new Derived};
shared_ptr<Base>  bp1 = dp1;
shared_ptr<Base>  bp2 {dp1};
shared_ptr<Base>  dp1 {new Derived};

  • Casting shared_ptr
shared_ptr<Base>  base_ptr  {new Base};
shared_ptr<Derived>  derived_ptr;
derived_ptr = static_pointer_cast<Derived> (base_ptr)

Other casting functions: static_pointer_cast, dynamic_pointer_cast, const_pointer_cast

  • Better alternative of new function
  • shared_ptr<Thing> p1 = new Thing(32, “hello”); //inefficient
Two memory allocation, one for Thing object and one for manager object
created by the shared_ptr construction

  • shared_ptr<Thing> p1 = make_shared<Thing>(32, “hello”);
same as new, but efficient. Only one memory allocation that is big enough to hold both the manager object and new object.

Weak Pointer
  • Weak pointers just observe the managed object. They do not keep it alive or affect its lifetime. So even if the last weak_ptr goes out of scope or disappear, the pointed-to object can still exist.
  • weak_ptr does not support * or -> (no dereference allowed).
Neither you can access the pointer to the object with it (No get() function available)
  • weak_ptr can be used to determine whether the object exists and to generate a shared_ptr that can be used to refer to it. Using lock() function.
Example:
void do_something(weak_ptr<Thing> wp) {
shared_ptr<Thing> sp = wp.lock(); //get shared_ptr from weak_ptr
if(sp) { //do your stuff }
else { // Thing object is gone }
}

  • Initializing a weak_ptr
    • Default value is empty
    • You can point a weak_ptr to an object only by copy or assignment from a shared_ptr or an existing weak_ptr to the object.
    • Unlike shared_ptr, you can not reset a weak_ptr by assignment to nullptr. Use reset() function to set a weak_ptr back to the empty state in which it is pointing to nothing.

shared_ptr<Thing> sp1 {new Thing};
weak_ptr<Thing> wp1 {sp1}; //construct wp1 from a shared_ptr
weak_ptr<Thing> wp2; // empty weak_ptr pointing to nothing
wp2 = sp1; // wp2 now points to new Thing object
weak_ptr<Thing> wp3 {wp2}; //construct wp3 from a weak_ptr
weak_ptr<Thing> wp4;
wp4 = wp2; //wp4 also now points to the same new Thing object

  • Get shared_ptr from weak_ptr / Check if shared_object exist or not?
shared_ptr<Thing> sp = wp.lock(); //get shared_ptr from weak_ptr

Note: You can not refer to the object directly with a weak_ptr (already
mentioned in the second bullet of this weak_ptr section), you need to get a shared_ptr from it first with lock() function (already mentioned in the fourth bullet of this weak_ptr section).

The lock() function examines the state of the manager object to determine whether the managed object still exists, and provides an empty shared_ptr if it does not, and a shared_ptr if it does. See example in the fourth bullet of this weak_ptr section).

  • Key points:
    • weak_ptr does not support * or -> (no dereference allowed).
    • No get() function available
    • lock() function to get shared_ptr
    • No nullptr assignment allowed, use reset() function.

Unique Pointer
  • An object is owned by exactly one unique_ptr.
  • Unlike shared_ptr or built-in pointer, you can not copy or assign a unique_ptr to another unique_ptr.
  • When the unique_ptr goes out of scope, the pointed-to object gets deleted and this happens regardless of how we leave the function, either by a return or an exception being thrown somewhere.

void foo() {
unique_ptr<Thing> p { make_unique<Thing> }; // p owns the Thing
p -> do_something();
another_function(); // might throw an exception
} // p gets destroyed. Destructor destroys the Thing.

  • VVI: Since copy construction is not allowed, if you want to pass a unique_ptr as a function argument, do it by reference.
unique_ptr<Thing> p1 { make_unique<Thing> }; // p owns the Thing
unique_ptr<Thing> p2 {p1}; //error, copy-construction is not allowed
unique_ptr<Thing> p3; //an empty unique_ptr
p3 = p1; //error, copy assignment is not allowed
  • Transferring ownership
    • Create a thing and returns a unique_ptr to it.
unique_ptr<Thing> create_Thing() {
unique_ptr<Thing> tmp_ptr { new Thing }
return tmp_ptr; //tmp_ptr will surrender ownership
}

void foo () {
unique_ptr<Thing> p1 { create_Thing() }; // p1 owns the Thing
unique_ptr<Thing> p2; // default unique_ptr; owns nothing
p2 = create_Thing(); // p2 now owns the second Thing
}

    • Explicit transfer of ownership between unique_ptr using std::move()
unique_ptr<Thing> p1 {new Thing}; // p1 owns the Thing
unique_ptr<Thing> p2; // p2 owns nothing
// invoke move assignment explicitly
p2 = std::move(p1); // now p2 owns it, p1 owns nothing

// invoke move construction explicitly
unique_ptr<Thing> p3 {std::move(p2)}; //now p3 owns it, p1 and p2 own nothing

  • Use reset() and nullptr in the same way as shared_ptr
  • Use make_unique for memory allocation instead of new. It is especially designed for unique_ptr. The reasoning is same for both unique_ptr and shared_ptr.

Reference:
Kieras, D.: Using C++11’s Smart Pointers. University of Michigan. Tutorial (2016) 1 - 14

Thursday, July 21, 2016

Where do I begin

I have one really weird ritual and I think it's coming to an end finally with last academic examinations of mine life. So basically minutes before leaving for examination I blast the speaker with just one particular song in loop. It didn't / doesn't exactly do anything except it allows me to get consumed with my panic, then I feel ready - What's the worst that could happen - max I might fail, but does it really matter, I gave my best that's what really matters to me. That one particular song obviously kept changing with time. Now as I am approaching towards the last theory paper of my life, this all seems so nostalgic or another thing, so recent!

During those high school days, it was this - yes it was those audio cassette tapes :D



During bachelor's, it was Lakshya. Rommie used to get really pissed off because of my timing as it used to be his last minute revision time and there I, singing at top of my lungs blasting woofer. He will never forget or possibly forgive me for that I am sure :D



All those GATE, interviews, tofel, these master examinations, this little girl became / is a ritual who basically says - everything will be fine if I just keep trying.  She is on my cell since a very very long time.


Nonetheless, I am at peace with my best and worst mistakes, success or failures, I feel warm and contented in my heart of heart that throughout this journey I lived every day, that I tried to give my best - A new life is waiting at the end of the road, I will meet her there.

Monday, June 20, 2016

Story of a little wave

There is one book which never fails to remind me every time the whole passion of my human spirit, my silent shoulder of solace amongst all the chaos and mayhem of life.

Excerpt from "Tuesdays With Morrie" by Mitch Albom:

“I heard a nice little story the other day,” Morrie says. He closes his eyes for a moment and I wait. “Okay. The story is about a little wave, bobbing along in the ocean, having a grand old time. He’s enjoying the wind and the fresh air—until he notices the other waves in front of him, crashing against the shore. “‘My God, this is terrible,’ the wave says. ‘Look what’s going to happen to me!’ “Then along comes another wave. It sees the first wave, looking grim, and it says to him, ‘Why do you look so sad?’ “The first wave says, ‘You don’t understand! We’re all going to crash! All of us waves are going to be nothing! Isn’t it terrible?’ “The second wave says, ‘No, you don’t understand. You’re not a wave, you’re part of the ocean.’” I smile. Morrie closes his eyes again. “Part of the ocean,” he says, “part of the ocean. “I watch him breathe, in and out, in and out.”

I have my own interpretation. What does it mean to you, it's up to you to think. I can't really suggest.

Wednesday, April 13, 2016

I Choose Violence

04.24.16 So the agony is almost over, the Game of Thrones Season 6 premiere night is just around the corner. Since the Red wedding, have been waiting for someone to avenge Lady Stark. Lord, I sedately adored her. That's the enigma with watching a series like GOT, obsession seems normal. Even, recently a professor introduced a Game of thrones edition course, have to admit that he is way beyond cool.

Though I am more concerned about my newborn affection for Cersai after Mother's Mercy. It would be so ruffling if I fall for her. But since when life has followed linear equations. We don’t get to choose who we love, do we? I just wish her to be a little bit less Cersai though. But again Cersai being Cersai, I know she will kick arse, that I want.

Anyway, after a long winter finally, summer has arrived. Check it out and don't worry Jon Snow will be resurrected.

Wednesday, March 16, 2016

Final weeks - Let's keep calm!

Me: Isn't 1000+ slides too much prof?


Professor: Don't worry, questions in final will be easy given you aren't dumb.


Me: LOL, That's how you failed 75% of class last year :-/

Update [May 2017, a year later]: Well, failed that examination by 0.3 (passed:15.625%, it didn't really feel so bad when ~84% of the class failed :-/). Funny thing, a few months later, applied one of prof technique at work and dude was it a riot, eventually, a whole module adopted that same implication.

Looking back: One of the best course ever taken ... It's totally okay to fail sometimes:D
Mother used to say, "Every Hard-fought Struggle Pays For What It’s Worth, Someday. You Just Don’t Know Which Day, Yet!"

Wednesday, December 16, 2015

My Infinite Playlist - '15

'15 you are truly insane. There were times when I hated you most for your stubbornness. Those waking up in the early morning, those late night struggles with sleep and slides, those schedules - almost all of your traits were crazy enough to drive me insane with your excuses. I sometimes hated you for your bullying gut. You almost dragged me to some amazing places where you wanted us to go but I was shy to explore. You inspired me to be good, to do something good with life, gave me a purpose. You are more than a friend to me.

I guess there were times too when you hated me too for my lazy ass. But believe me, I sincerely worked everyday to prove you that I was worth of your love and affection. My dearest, I grew with you every passing single moment. I despised you and the same time loved you. I truly loved messing with you, I know sometimes it made you annoyed but you patiently bore with a broad smile on your face. I fell in love with our awkward beautiful bonding.

Now as the time to say Goodbye is under the horizon, I want to say something to you even though I can't find anything that can properly express my feeling and our relationship but there is something I want to dedicate to you that says something about us - "The relation of growing together".


Most probably this is my last post directed towards bidding adieu to passing years.