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

2auto_ptr

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);
	cout<<*p<<endl;
	 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];
    auto_ptr<int>ap(pa);
}

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.

assignment

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 
	cout<<*ap3<<endl;//normal

Copy

void Func(auto_ptr<int> ap){
    cout << *ap << endl;
}
int main(){
    int*p = new int(10);
    auto_ptr<int> ap(p);
    cout<< *ap <<endl;
    Func(ap);
    //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;
private:
	unique_ptr(const unique_ptr& a);
	unique_ptr& operator=(const unique_ptr& a);
};

4scoped_ptr

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;
public:
	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
}

5shared_ptr

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);
	cout<<*sp<<endl;
	cout<<*sp2<<endl;

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

5.2 test

#include<iostream>
#include<memory>  
using namespace std;
class Test{
int m_n;
public:
	Test(int n):m_n(n){
		cout<<"construct"<<m_n<<endl;
	}
	Test(){
		cout<<"construct"<<m_n<<endl;
	}

	~Test(){
		cout<<"destructor"<<m_n<<endl;
	}
	void Func()const{
		cout<<"func"<<endl;
	}
	friend ostream& operator<<(ostream& os,const Test& t){
		os<<"Test ("<<t.m_n<<")";
		return os;
	}
};

void Func(shared_ptr<Test> p){
	cout<<p<<endl;

}
int main(){

	shared_ptr<Test> sp(new Test(10));
	shared_ptr<Test> sp2(sp);
	cout<<*sp<<endl;
	cout<<*sp2<<endl;
	Func(sp);
	cout<<(*sp)<<endl;


}

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

construct 10
Test(10)
Test(10)
Test(10)
Test(10)
destructor

5.3 problem: circular reference problem

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

};
class B{
public:
	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);	
	
	pa->m_b=pb;
	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
 	*/

}

6weak_ptr

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