c ++ smart pointers independent and shared learning articles

unique_ptr smart pointers articles

Summary:

As a smart pointer, it was designed to address the most critical goal is native pointer lack of security issues

Declaration syntax:

std :: unique_ptr <type> variable name} {initialization value;

Precautions: best fit to initialize std :: make_unique unique_ptr

For example as follows:

The first:

	// 初始化一个数据类型空间,括号内为初始化的值
	std::unique_ptr<int> upa{ std::make_unique<int>(150) };
	std::cout << "upa = " << *upa << std::endl;

result:
Here Insert Picture Description

The second:

	//初始化许多个数据类型空间,圆括号内为初始化空间个数,这里即可放10个int
	std::unique_ptr<int[]> upb{ std::make_unique<int[]>(10) };
	upb[0] = 10; upb[1] = 20; //一看就懂
	std::cout << "upb[0] = " << upb[0] <<  " upb[1] = " << upb[1]<< std::endl;

result:
Here Insert Picture Description

Important features:

Features of this unique pointer with his name, it can not make more than two unique pointer to the same memory address.

To the wrong example:

	std::unique_ptr<int> upa{ std::make_unique<int>(20) };  //第一个unique指针
	std::unique_ptr<int> upb{ upa };  //初始化时指向第一个,直接报错,编译过不去

Common methods:

1、get()

Description: Returns the unique native pointer! How to understand? please look below:

	
	std::unique_ptr<int> upa{ std::make_unique<int>(20) };
	int* pa = upa.get();  //定义一个原生指针接收它就可以了
	std::cout << "pa=" << *pa << std::endl;

result:
Here Insert Picture Description

2、reset()

Introduction: release unique memory space pointer, and the pointer to nullptr (c ++ 11 and above)

For example as follows:

	std::unique_ptr<int> upa{ std::make_unique<int>(20) };
	upa.reset();  //释放内存并且指向nullptr
	std::cout << upa << std::endl;

result:
Here Insert Picture Description

3、release()

Description: Returns the memory address of the original unique pointer, but the pointer to the unique space, how to understand?

For example as follows:

	std::unique_ptr<int> upa{ std::make_unique<int>(20) };
	std::cout << "原upa地址=" << upa << std::endl;
	int * a = upa.release();
	std::cout << "a地址="<< a << std::endl;
	std::cout << "新upa地址=" << upa << std::endl;

result:
Here Insert Picture Description

4、std::move(unique_ptr)

Description: In order to solve the uniqueness unique and can not be transferred to another unique characteristic of

For example as follows:

	std::unique_ptr<int> upa{ std::make_unique<int>(20) };
	std::unique_ptr<int> upb{};
	std::cout << "原upa==" << upa << std::endl;
	upb = std::move(upa);
	std::cout << "upb==" << upb << std::endl;
	std::cout << "新upa==" << upa << std::endl;

Results:
Here Insert Picture Description
Precautions: method it is not a class, but the standard library function std; and get different is that he is transferred directly to Li Gang, a unique pointer, and get transferred to a native pointer!

sharedx_ptr smart pointers articles

Summary:

Is different from the unique, he can define a number of shared pointers point to the same address; intelligent in what place it? Only when the last shared pointer release, this memory will be released! And can record the current address how many smart pointer calls

Declaration syntax:

std :: sharedx_ptr <type> variable name} {initialization value;

Precautions: best fit to initialize std :: make_shared shared_ptr

For example as follows:

	//第一种,跟unique一样的形式,只是把unique换成shared
	std::shared_ptr<int> spa{ std::make_shared<int>(50) }; 
	
	//第二种  这种方法不行,用第三种替换
	//std::shared_ptr<int[]> spb{ std::make_shared<int[]>(4) };
	
	//第三种 //用new申请数组形式替代即可
	std::shared_ptr<int[]> spc{ new int[4]{1,2,3,4} };

Common methods:

1、use_count()

Description: returns the current address how many calls shared_ptr

Code:

	std::shared_ptr<int> spa{ std::make_shared<int>(50) }; 
	std::cout << spa.use_count() << std::endl;   //1
	std::shared_ptr<int> spb{ spa };
	std::cout << spa.use_count() << std::endl; //2
	std::shared_ptr<int> spc{ spa };
	std::cout << spa.use_count() << std::endl;  //3

result:
Here Insert Picture Description

2、unique()

Summary:

原型: bool std::shared_ptr.unique();

Description: If the only shared_ptr pointing a pointer to the shared area, returns true, false otherwise

Code:

	std::shared_ptr<int> spa{ std::make_shared<int>(50) }; 
	std::cout << spa.unique() << " "; //唯一
	std::shared_ptr<int> spb{ spa }; 
	std::cout << spa.unique() << " "; //不唯一
	std::shared_ptr<int> spc{ spa };
	std::cout << spa.unique() << " "; //不唯一

result:
Here Insert Picture Description

3、reset()

Description: Set the current share pointer nullptr, if the last one, it will free up memory

Code:

	std::shared_ptr<int> spa{ std::make_shared<int>(50) }; 
	std::shared_ptr<int> spb{ spa };
	std::shared_ptr<int> spc{ spa };
	spa.reset(); std::cout << spb << " "; //还没释放内存
	spb.reset(); std::cout << spc << " ";//还没释放内存
	spc.reset(); std::cout << spc << " ";//释放内存

Results:
Here Insert Picture Description
Thanks again for your efforts and hard work! ! I feel that good old iron little bit like oh! I was sailing potatoes, thank you!

Published 10 original articles · won praise 14 · views 1715

Guess you like

Origin blog.csdn.net/u010092716/article/details/104334751