Dealing with smart pointers is a daily task for any C++ programmer and how to pass them around can get tricky when you are looking to achieve optimal performance. Here are some tips and general guidelines to follow for passing shared_ptr and unique_ptr as function arguments and at the time of return.

As always, prefer unique_ptr to shared_ptr, unless you intend to share ownership.

Introduction

  1. Always ask yourself “Do I really need to pass a smart pointer ?”
  2. std::unique_ptr usage
  3. std::shared_ptr usage
  4. How to pass correctly pass raw pointer/reference?
  5. For Returning smart pointers, what is the norm?

Always ask yourself “Do I really need to pass a smart pointer ?”

You should always assess…


Problem: We are given a number N and we have to find the smallest possible palindrome greater than N that can be formed by using the same set of digits as in N. This problem is asked in many interviews and require an understanding step-by-step approach, also have applications in other manipulation problems.

Let’s dive in!!

Table of Contents

  1. Background and example
  2. Expectations from the optimal solution
  3. Breaking down and visualizing the problem
  4. Solution and approach
  5. Source code

Background and Example

What is a palindrome? A word, phrase, or sequence that reads the same backward as forwards, e.g. madam or 12321.

Let’s say we’re given the number 4697557964 which…


Did you ever think who understands google.com when you type that in your address bar? 🤔

How does this magic happen? ✨ There isn’t a delivery boy to hand over an HTML page directly. Let’s understand the crux of this superfast procedure 🚀 starting from typing a URL address to getting a page in your browser. 💻

If you ever need to set up a blog/website, this is a must-know article that contains all the understanding of DNS, the lookup process, types of DNS records i.e. all you need to know explained within a single article.

DNS: Internet’s own complex library

Table of Contents

Here’s the outline of…


A lambda function is a function that you can write inline in your source code In this article, I’ll first explain why lambdas are great, what are closures vs how lambdas are different from closures? — with some examples — and then I’ll walk through all of the details of what you can do with lambda.

What are closures vs how lambdas are different from closures?

They look similar!!

Table of Contents

  1. Just your everyday functions
  2. Lambda Expressions
  3. What are closures
  4. Lambdas vs. Closures
  5. Distinction by Examples
  6. Close analog to a closure

Just your everyday functions

When most people think of functions, they think of named functions such as -


How virtual vs. multiple inheritance affect class object construction order?

I’ll try to keep this post short and simple 😊

Table of Contents

  1. An example class having virtual and/or multiple inheritance
  2. What are the rules for object construction order?
  3. Understanding the object construction order rules
  4. Let’s apply the rules, shall we?
  5. Bonus Question

Example: A class having virtual and/or multiple inheritance

class Xis declared here as inherited by D1, D2 which in turn are inherited by other class in a complex manner by other classes.


Using In-member initialization, using constructors smartly and using class members functions in a safe and proper way to avoid mistakes

Clean code!

Table of Contents

  1. Use member initializers in the same order as their declaration
  2. Prefer in-class member initializer over constant initializations OR over default constructor.
  3. Don’t cast away const, ever!
  4. Use delegating constructors to represent common actions for all constructors of a class.

1. Use member initializers in the same order as their declaration

Member variables are always initialized in the order they are declared in the class definition.

The order in which you write them in the constructor initialization list is ignored 🥴

Make sure the constructor code doesn’t confusingly specify different orders. For…


Some puzzles from various talks, blog posts, and other bits

Table of Contents

  1. Puzzle 1: Capturing unique_ptr by reference
  2. Puzzle 2: Capturing the temporary by the reference
  3. Puzzle 3: Making mistakes with std functions usage
  4. Puzzle 4: Throwing away the dead

Puzzle 1: Capturing unique_ptr by reference

unique_ptr<A> myFun()
{
unique_ptr<A> pa(new A());
return pa;
}

const A& rA = *myFun();

This code compiles but rA contains garbage. Why is this code invalid?

Note: if we assign the return of myFun to a named unique_ptr variable before dereferencing it, it works fine.

Answer:

The unique_ptr will pass the ownership to another unique_ptr, but in this code, there is nothing to…


Let’s discuss what coroutines are in general and how C++20 is introducing them

Table of Contents

1. Prerequisite Terminology
2. What are coroutines?
3. Coroutines vs Subroutines?
4. Coroutines vs Threads
5. Applications of coroutines
6. Example in Python
7. How to simulate coroutines in traditional C++
8. Coroutines in C++20
9. Restrictions

Prerequisite Terminology

  1. Cooperative Multitasking (a.k.a non-preemptive multitasking) — If multitasking participant process or thread voluntarily let go of control periodically or when idle or logically blocked. This type of multitasking is called “cooperative” because all programs must cooperate for the entire scheduling scheme to work.
  2. Subroutine — Any regular function that you…


How to use a custom deleter with an unique_ptr and shared_ptr

Related post: https://medium.com/pranayaggarwal25/a-tale-of-two-allocations-f61aa0bf71fc

Table of Contents

  1. Introduction
  2. The true unknown face of smart pointers
  3. What is std::default_delete indeed?
  4. Ways to specify custom deleters
  5. Using custom deleter with shared_ptr
  6. Using custom deleter with unique_ptr
  7. Storage of custom deleters
  8. Restrictions that come with custom deleters

Introduction

Why and when would we need something like that?

Case 1: In order to fully delete an object sometimes, we need to do some additional action. …


Also known as Forwarding reference

Prerequisites: Rvalue references

Table of Contents:

1. Introduction
2. The double life of “&&”
3. How to identify a Universal Reference
4. Universal Reference in templates
5. Universal Reference in auto
6. How does the magic of Univeral Reference work?
7. Handle with care
8. What is the need? The perfect forwarding problem
9. How to work with Universal References?
10. Universal Reference in “typedef”
11. Universal Reference in “decltype”
12. The ultimate Truth

Introduction

Every rvalue ref is denoted by ‘&&’ but vice versa is not true.

Rvalue reference ⇒ “&&
&& Rvalue reference.

&&” in source…

Pranay Kumar

Computer Scientist @ Adobe Systems https://pranayaggarwal.github.io/

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