Singleton class ensures that there will be only one object of the class in lifetime of a program. Normally we can create as many objects as we want from a particular class. But you’ll be able to create at most one object from a singleton class. Where to Use Singleton Class? There are situations where … Continue reading “Singleton Class in C++”
Category: Programming
What is std::shared_ptr? Examples
std::shared_ptr is a smart pointer that manages shared ownership of an object through pointer. Multiple std::shared_ptr (s) can hold the same object pointer. The actual object gets deleted when all the std::shared_ptr (s) go out of scope or get deleted/reset. It was introduced in C++11. It helps C++ programmers cleaning up memory in very convenient … Continue reading “What is std::shared_ptr? Examples”
C Program to Merge Two Sorted Arrays into one Sorted Array
Here we’ll write a C function to merge two sorted arrays into one. The merged array will also be sorted. Here we’ll use two sorted integer arrays that are sorted in ascending order. After merging the result array will contain all the numbers from the original arrays and will be sorted in ascending order. The … Continue reading “C Program to Merge Two Sorted Arrays into one Sorted Array”
Move std::unique_ptr to Pass to a Function or Assign to Another Variable
The std::unique_ptr has unique ownership of the object it manages. That means that more than one std::unique_ptr object can not contain the managed object pointer. That’s why we can not assign a std::unique_ptr to another one. For the same reason, we can not pass std::unique_ptr as an argument of a function. But we can always … Continue reading “Move std::unique_ptr to Pass to a Function or Assign to Another Variable”
Custom Deleter in std::unique_ptr
In my previous article, we discussed how std::unique_ptr helps to avoid memory leak. By default it calls the default deleter (C++ delete operator) when the std::unique_ptr goes out of context. But many times default deleter is not good enough for the type of pointer we deal with. For example, if you work with a file … Continue reading “Custom Deleter in std::unique_ptr”