C++ Core Guidelines: Rules for Smart Pointers

Contents[Show]

There were a lot of C++ experts who said that smart pointers were the most important feature of C++11. Today, I will write about smart pointers in C++.

The C++ core guidelines has thirteen rules for smart pointers. Half of them deal with their owner semantic; half of them with the question: How should you pass a shared pointer to a function?

gold 513062 640

Here is an overview of the rules.

The first five rules (R.20 - R.24) are quite obvious. I have written a few posts about them. Let me paraphrase the rules and refer to my previous posts.

An std::unique_ptr is an exclusive owner of its resource; therefore, you can not copy it but only move it. In contrast, an std::shared_pointer shares ownership. If you copy or copy assign a shared pointer, the reference counter will automatically be increased; if you a delete or reset a shared pointer, the reference counter will be decreased. If the reference counter becomes zero, the underlying resource will be deleted. Because of this management overhead, you should use an std::unique_ptr, if possible (R.21).

This overhead becomes in particular true if you create an std::shared_ptr. Creating an std::shared_ptr requires the allocation of the resource and the reference counter which is in sum quite an expensive job; therefore, you should use the factory function std::make_shared (R.22). std::make_shared makes only one allocation. This is a big performance improvement for std::shared_ptr. In compared in the post "Memory and Performance Overhead of Shared Pointers" the differences between the creation and deletion of raw pointers and shared pointers including the factory functions std::make_shared and std::make_unique.

There is an additional, important reason to create an std::shared_ptr with std::make_shared, and an std::unique_ptr with std::make_unique: no memory leak (R.22 and R.23). Using two invocations of std::shared_ptr or std::unique_ptr in one expression can cause a memory leak, if an exception happens. Read the details about this issue in my last post: C++ Core Guidelines: Rules for Allocating and Deallocating (R.13).

To be honest, an std::weak_ptr is not a smart pointer. An std::weak_ptr is no owner and lends only the resource from its std::shared_ptr. Its interface is quite limited. By using the method lock on an std::weak_ptr, you can lift an std::weak_ptr to an std::shared_ptr. Of course, you have a question: Why do we need an std::weak_ptr? An std::weak_ptr helps to break the cycles of std::shared_ptr (R.24). These cycles are the reason, an std::shared_ptr will not automatically release its resource. Or to say it the other way around. If you have a cycle of shared pointers, you will have a memory leak. Read the details to std::weak_ptr and how you can use them to overcome memoy leaks with std::shared_ptr in my previous post std::weak_ptr.

Now I'm done with my summary of smart pointers. That is more or less general knowledge to smart pointers. This will not hold for the remaining rules. They deal with the question: How should you pass a shared pointer to a function?

R.30: Take smart pointers as parameters only to explicitly express lifetime semantics

This rule is a little bit tricky. If you pass a smart pointer as a parameter to a function and you use in this function only the underlying resource of the smart pointer, you do something wrong. In this case, you should use a pointer or a reference as a function parameter, because you don't the lifetime semantic of a smart pointer.

Let me give you an example to the quite sophisticated lifetime management of a smart pointer.

 

// lifetimeSemantic.cpp

#include <iostream>
#include <memory>

void asSmartPointerGood(std::shared_ptr<int>& shr){
  std::cout << "shr.use_count(): " << shr.use_count() << std::endl;  // (3)
  shr.reset(new int(2011));                                          // (5)
  std::cout << "shr.use_count(): " << shr.use_count() << std::endl;  // (4)
}

void asSmartPointerBad(std::shared_ptr<int>& shr){
  // doSomethingWith(*shr);
  *shr += 19;
}

int main(){
  
  std::cout << std::endl;
  
  auto firSha = std::make_shared<int>(1998);
  auto secSha = firSha;
  std::cout << "firSha.use_count(): " << firSha.use_count() << std::endl;  // (1)
  
  std::cout << std::endl;
  
  asSmartPointerGood(firSha);                                              // (2)
  
  std::cout << std::endl;
  
  std::cout << "*firSha: " << *firSha << std::endl;
  std::cout << "firSha.use_count(): " << firSha.use_count() << std::endl;
  
  std::cout << std::endl;
  
  std::cout << "*secSha: " << *secSha << std::endl;
  std::cout << "secSha.use_count(): " << secSha.use_count() << std::endl;
  
  std::cout << std::endl;
  
  asSmartPointerBad(secSha);                                              // (6)
  std::cout << "*secSha: " << *secSha << std::endl;
  
  std::cout << std::endl;
  
}

 

I will start with the good case for an std::shared_ptr. The reference counter in line (1) is 2 because I used the shared pointer firSha to copy-initalise secSha. Let's have a closer look at the invocation of the function asSmartPointerGood (2).  First (3), the reference count of shr is 2 and then, it becomes 1 in the line (4).  What happened in line (5)? I reset shr to the new resource: new int(2011). Consequentially, both the shared pointer firSha und secSha are immediately shared owner of different resources. You can observe the behaviour in the screenshot.

lifetimeSemantic

If you invoke reset on a shared pointer, magic happens under the hood.

  • If you invoke reset without an argument, the reference counter will be decreased by one.
  • If you invoke reset with an argument and the reference counter was at least 2, you will get two independent shared pointer owning different resources. This is a kind of deep copy of shared pointers.
  • If you invoke reset with or without an argument and the reference counter becomes 0, the resource will be released.

This magic is not necessary if you only interested in the underlying resource of the shared pointer; therefore, a pointer or a reference is the right kind of parameter for the function asSmartPointerBad (6).

Further information

Have a look also at a recent post by Bartek F. about a situation where weak_ptr prevents from full memory cleanup: How a weak_ptr might prevent full memory cleanup of managed object.

What's next?

There are six rules left for passing smart pointers to functions. So you know, what I will write about in my next post.

 

 

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner,  Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, Sudhakar Balagurusamy, lennonli, and Pramod Tikare Muralidhara.

 

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, and Dendi Suhubdy

 

Seminars

I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.

Bookable (Online)

Deutsch

English

Standard Seminars 

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

New

Contact Me

Modernes C++,

RainerGrimmSmall

Comments   

0 #1 Dmitry 2020-06-17 17:43
Hi thank you for the great article. Although everything very clear i had one question concerning this line "If you invoke reset without an argument, the reference counter will be decreased by one.". Probably I missunderstood something as in my case
std::shared_ptr tmp = std::make_shared( 10 );
std::cout
Quote
0 #2 Rainer Grimm 2020-06-18 05:58
Quoting Dmitry:
Hi thank you for the great article. Although everything very clear i had one question concerning this line "If you invoke reset without an argument, the reference counter will be decreased by one.". Probably I missunderstood something as in my case
std::shared_ptr tmp = std::make_shared( 10 );
std::cout

You have to use an html sequence (I put spaces in between & lt ;) to display a smaller-than symbol. In your case, the smaller-than symbol was interpreted as an opening html tag.
Quote

My Newest E-Books

Course: Modern C++ Concurrency in Practice

Course: C++ Standard Library including C++14 & C++17

Course: Embedded Programming with Modern C++

Course: Generic Programming (Templates)

Course: C++ Fundamentals for Professionals

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 7840

Yesterday 6384

Week 7840

Month 166271

All 5035585

Currently are 149 guests and no members online

Kubik-Rubik Joomla! Extensions

Latest comments