Advanced C + + intelligent pointer

1 Basic Concepts

1.1 hazards of pointer:

Pointer not initialized
 Wild pointer:
Memory leak (request dynamic memory not released)

1.2 classification

1.3 essence

Encapsulate the pointer as a class object member, and delete the memory pointed by the pointer in the destructor.

1.4 different

Name Different
auto_ptr Delete now.
unique_ptr Delete now.
scoped_ptr Delete now.
shared_ptr The count is 0 for deletion.
weak_pt Not delete


2.1 role

Automatic release of dynamic allocation objects in scope

2.2 basic types

int *p=new int(10);
	cout<<*p<<endl;//This will cause memory leakage. Through Valgrind. / a.out, you can see that the application for a release of 0 leaks 4 bytes
	//Intelligent pointer
	miniSTL::auto_ptr<int> ap(p);
	cout<<*ap<<endl;//A p is the same as P. through Valgrind. / a.out, it can be seen that there is no leakage when ap plying for a release

2.3 types

Test *t=new Test;
	t->Func();//Through Valgrind. / a.out, it can be seen that the application for a release has 0 leaks and 1 byte
	miniSTL::auto_ptr<Test> apt(t);
	t->Func();//Through Valgrind. / a.out, it can be seen that there is no leakage when applying for a release

2.4 defects

2.4.1 two auto PTRs cannot have the same object at the same time

int *p=new int(10);
	 auto_ptr<int> ap1(p);
	const auto_ptr<int> ap2(p);//Erro: double free causes memory leak
	cout<<*ap1<<" ----"<<endl;

2.4.2auto? PTR cannot manage array pointers

#include <memory>
using namespace std;
int main(){
    int*pa=new int[10];

2.4.3 after the uto ﹣ PTR is copied or assigned, the management of the original pointer is lost. This situation is called pointer ownership transfer.


auto_ptr<int> ap3=ap1;
	cout<<*ap1<<endl;//Segment error (enunciation) 
			//The reason for the error is that ap1 gives the pointer to ap3, and in order to avoid two releases, it assigns itself a null value 


void Func(auto_ptr<int> ap){
    cout << *ap << endl;
int main(){
    int*p = new int(10);
    auto_ptr<int> ap(p);
    cout<< *ap <<endl;
    //Segment error
    cout<< *ap <<endl;

2.4.4auto ﹤ PTR cannot be used as a container object, because the elements in STL containers often need to support copying, assignment and other operations.

Insert a code slice here

3unique? PTR (instead of auto? PTR)

3.1 characteristics

Let the pointer not be assigned, not copied, and implement the function to privatize it

template<class T>
class unique_ptr{
T* m_p;
	unique_ptr(const unique_ptr& a);
	unique_ptr& operator=(const unique_ptr& a);


4.1 role

Similar to unique? PTR, cannot copy and assign in this scope.

4.2 difference from unique_ptr

Disable copy constructor assignment operator overload function

class auto_ptr{
T * m_p;
	auto_ptr(T* p):m_p(p){}
	auto_ptr(const auto_ptr& p):m_p(p.m_p)=delete
	auto_ptr& operator=(const auto_ptr& a)=delete


5.1 advantage

//The problem of smart pointer copy and assignment can be solved by using shared ﹐ PTR
	//Write time copy technology
	shared_ptr<Test> sp(new Test(10));
	shared_ptr<Test> sp2(sp);

Essence: sp and sp2 share an address but will not release twice

5.2 test

using namespace std;
class Test{
int m_n;
	Test(int n):m_n(n){

	void Func()const{
	friend ostream& operator<<(ostream& os,const Test& t){
		os<<"Test ("<<t.m_n<<")";
		return os;

void Func(shared_ptr<Test> p){

int main(){

	shared_ptr<Test> sp(new Test(10));
	shared_ptr<Test> sp2(sp);


As a result, it is proved that there is no deep copy by a construction and analysis

construct 10

5.3 problem: circular reference problem

using namespace std;
class B;
class A{
	A(){cout<<"A construct"<<endl;}
	~A(){cout<<"A destruct"<<endl;}
	//First time
	//Correction of the second application
	weak_ptr<B> m_b;

class B{
	B(){cout<<"B construct"<<endl;}
	~B(){cout<<"B destruct"<<endl;}
	shared_ptr<A> m_a;

int main(){
	shared_ptr<A> pa(new A);
	shared_ptr<B> pb(new B);	
	pb->m_a=pa;//If only one assignment is removed between two assignments, AB can be deconstructed
	/*First result
	[root@foundation66 c++]# ./a.out 
	A construct
	B construct
	The reason for the problem of circular reference is that if A wants to deconstruct, then the first member of A, m, B, and mb, if they want to deconstruct, then the first member of A, m, A, has A circular request
	//Solution: weakou PTR
	/*[root@foundation66 c++]# g++  8_4shared_ptr_problem.cpp -std=c++11
 	* [root@foundation66 c++]# ./a.out 
 	* A construct
 	* B construct
 	* B destruct
 	* A destruct



Solve the problem of shared ﹣ PTR circular reference

6.1 benefits

Break the recursive dependency
2. Use a shared resource without ownership, and do not add reference count
3 avoid dangling the pointer.

7 Summary

Published 21 original articles, praised 0, visited 102
Private letter follow

Posted on Thu, 13 Feb 2020 03:46:18 -0800 by Griven