C + + smart pointer shared_ptr


shared_ptr is an intelligent pointer class provided by C++11. It is smart enough to automatically delete relevant pointers when it is not used anywhere, so as to help completely eliminate the problems of memory leakage and dangling pointers.
It follows the concept of shared ownership, that is, different shared_ptr objects can be associated with the same pointer, and this is achieved internally using a reference counting mechanism.
Each shared_ The PTR object internally points to two memory locations:
1. Pointer to the object.
2. Pointer used to control reference count data.
How shared ownership works with the help of reference counts:
1. When the new shared_ When a PTR object is associated with a pointer, the reference count associated with this pointer is increased by 1 in its constructor.
2. When any shared_ When a PTR object is out of scope, it subtracts the reference count of the associated pointer by 1 in its destructor. If the reference count becomes 0, there are no other shared_ The PTR object is associated with this memory, in which case it uses the delete function to delete the memory.

Create shared_ptr object

Create shared using the original pointer_ PTR object

std::shared_ptr<int> p1(new int());

The above line of code creates two pieces of memory on the heap: 1: store int. 2: Memory for reference counting, and manage the shared attached to this memory_ The count of PTR objects. The initial count will be 1.

Check shared_ Reference count of PTR object


Create an empty shared_ptr object

Because shared with parameters_ PTR constructor is of explicit type, so STD:: shared cannot be used like this_ ptr p1 = new int(); Implicitly call its constructor. Create a new shared_ The best way to PTR objects is to use STD:: make_ shared:

std::shared_ptr<int> p1 = std::make_shared<int>();

std::make_shared allocates memory for both the int object and the data used for reference counting at one time, while the new operator only allocates memory for int.

Detach associated raw pointer

To make shared_ To cancel the association of PTR objects with related pointers, you can use the reset() function:
reset() without parameters:


In this case, it will internally point to the new pointer, so its reference count will change to 1 again.
Reset with nullptr:

p1 = nullptr; 

shared_ptr is a pseudo pointer
shared_ptr acts as a normal pointer. We can compare * and - > with shared_ptr objects can also be used together with other shared objects_ PTR objects are compared;

instance analysis

#include <iostream>
#Include < memory > / / this header file needs to be included

int main()
	// Using make_shared creates an empty object
	std::shared_ptr<int> p1 = std::make_shared<int>();
	*p1 = 78;
	std::cout << "p1 = " << *p1 << std::endl; // Output 78

	// Number of print References: 1
	std::cout << "p1 Reference count = " << p1.use_count() << std::endl;

	// The second shared_ptr objects point to the same pointer
	std::shared_ptr<int> p2(p1);

	// The following two outputs are: 2
	std::cout << "p2 Reference count = " << p2.use_count() << std::endl;
	std::cout << "p1 Reference count = " << p1.use_count() << std::endl;

	// Compare the smart pointer, p1 equals p2
	if (p1 == p2) {
		std::cout << "p1 and p2 are pointing to same pointer\n";

	std::cout<<"Reset p1 "<<std::endl;

	// reset is called without parameters, without associated pointers, and the number of references is 0
	std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
	// reset is called with parameters, and the number of references is 1
	p1.reset(new int(11));
	std::cout << "p1  Reference Count = " << p1.use_count() << std::endl;

	// Reset the object to NULL and the reference count to 0
	p1 = nullptr;
	std::cout << "p1  Reference Count = " << p1.use_count() << std::endl;
	if (!p1) {
		std::cout << "p1 is NULL" << std::endl; // output
	return 0;


Shared Xiaoming C + + smart pointer_ PTR explanation and example

Tags: C++

Posted on Thu, 07 Oct 2021 14:37:58 -0400 by jola