Image for post
Image for post

In the previous post, we discussed Smart Pointers — what they are and how they are better than normal pointers w.r.t. managing dynamically allocated memory. We also learned a smart pointer class present in STL i.e.unique_ptr and how we can implement our own smart pointer class.

In continuation, we are going to discuss shared_ptrand how we can write a class that works just like the standard shared_ptr class. shared_ptr is a reference-counted smart pointer i.e. it can share ownership of a dynamically allocated object with other shared_ptr instances. To put it in another way, several shared_ptr objects can own (point…


Image for post
Image for post

One of the peculiar things of C++ language is dynamic memory management. Unlike Java or C#, C++ doesn’t have an inbuilt Garbage Collector. Bjarne Stroustrup, the creator of C++, was asked, “Why doesn’t C++ have garbage collection?”. His answer is, “I don’t like garbage. I don’t like littering. My ideal is to eliminate the need for a garbage collector by not producing any garbage”. This is true in the sense that C++ itself doesn’t create any garbage, it’s the developer who creates garbage. So it is their responsibility to manage (deallocate) the dynamically allocated memory. Pointers are a way to…


Image for post
Image for post

C++ is an interesting language with several subtle features that are good to know and can help in understanding the underlying implementation of your favorite STL classes. One of the most common classes used from STL is the ‘string’. You would have certainly used #include <string>, but have you pondered on what it takes to write your own string class that works just like the STL string?

So, let us write our own string class. First and foremost, we need our class body. I am gonna call it my_string. Then, it must have a char buffer to hold our data…

Karan Kakwani

Keep Learning!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store