Better Programming

Advice for programmers.

Member-only story

Effective C++

C++ Smart Pointer Explained Through Intuitive Visuals

Part 3 of my C++ pointer series

6 min readApr 27, 2022

--

A pointer to T and another to the control block, where the latter is the pointer to memory, and the latter does the reference counting for std::shared_ptr.
· Overview
· Item 18: Unique_ptr for exclusive-ownership resource management.
An Exclusive Relationship
Raw Pointers
The smart pointer
Custom deleters
Custom interface
Tips for Smart Pointers
· Item 19: Shared_ptr for shared-ownership resource management.
Analogy: Simple reference counting
Detour: Physical class layout
· Item 21: Prefer unique_ptr and shared_ptr to direct use of new.
Goal: No raw new or delete
Preferred Usage
· Conclusion
· References

Overview

Let us continue our discussion of smart pointers: an object in C++ that reaps the benefits of raw pointers while overcoming their shortcomings. This is the third part in the series (see Part I [1] and Part II [2]).

We visually explore concepts of smart pointers in C++. We will reference a book all C++ developers should be familiar with, i.e., Effective Modern C++ by Scott Meyers [3].

All visualizations used throughout the blog were created by the author using LucidChart.

Item 18:

--

--

Joseph Robinson, Ph.D.
Joseph Robinson, Ph.D.

Written by Joseph Robinson, Ph.D.

Lead AI Engineer at BitHuman. Phd (NEU ‘20). Focus: applied ML w emphasis on vision, big data, automatic face understanding; https://www.jrobsvision.com

Responses (2)