参考:https://www.cnblogs.com/lsgxeva/p/7788061.html 和 《Linux多线程服务端编程》
C++11——智能指针
C++11中有unique_ptr、shared_ptr与weak_ptr等智能指针(smart pointer),定义在<memory>中。
可以对动态资源进行管理,保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调用。
1.unique_ptr
- unique_ptr持有对对象的独有权,同一时刻只能有一个unique_ptr指向给定对象(通过禁止拷贝语义、只有移动语义来实现)。
- unique_ptr指针本身的生命周期:从unique_ptr指针创建时开始,直到离开作用域。
- 离开作用域时,若其指向对象,则将其所指对象销毁(默认使用delete操作符,用户可指定其他操作)。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>
void mytest()
{
std::unique_ptr<int> up1(new int(11)); // 无法复制的unique_ptr
//unique_ptr<int> up2 = up1; // err, 不能通过编译
std::cout << *up1 << std::endl; // 11
std::unique_ptr<int> up3 = std::move(up1); // 现在p3是数据的唯一的unique_ptr
std::cout << *up3 << std::endl; // 11
//std::cout << *up1 << std::endl; // err, 运行时错误
up3.reset(); // 显式释放内存
up1.reset(); // 不会导致运行时错误
//std::cout << *up3 << std::endl; // err, 运行时错误
std::unique_ptr<int> up4(new int(22)); // 无法复制的unique_ptr
up4.reset(new int(44)); //"绑定"动态对象
std::cout << *up4 << std::endl; // 44
up4 = nullptr;//显式销毁所指对象,同时智能指针变为空指针。与up4.reset()等价
std::unique_ptr<int> up5(new int(55));
int *p = up5.release(); //只是释放控制权,不会释放内存
std::cout << *p << std::endl;
//cout << *up5 << endl; // err, 运行时错误
delete p; //释放堆区资源
return;
}
int main()
{
mytest();
system("pause");
return 0;
}
2.shared_ptr
- shared_ptr使用引用计数,控制对象的生命期。shared_ptr是强引用(想象成用铁丝绑住堆上的对象),只要有一个指向x的shared_ptr存在,该x对象就不会析构。当对指向对象x的最后一个shared_ptr析构或reset()的时候,x保证会被销毁。
- shared_ptr/weak_ptr的“计数”在主流平台上是原子操作,没有用锁,性能不俗。
- shared_ptr/weak_ptr的线程安全级别与std::string和STL容器一样,需要考虑加锁。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>
void mytest()
{
std::shared_ptr<int> sp1(new int(22));
std::shared_ptr<int> sp2 = sp1;
std::cout << "cout: " << sp2.use_count() << std::endl; // 打印引用计数
std::cout << *sp1 << std::endl;
std::cout << *sp2 << std::endl;
sp1.reset(); // 显示让引用计数减一
std::cout << "count: " << sp2.use_count() << std::endl; // 打印引用计数
std::cout << *sp2 << std::endl; // 22
return;
}
int main()
{
mytest();
system("pause");
return 0;
}
3.weak_ptr
- weak_ptr也是一个引用计数型智能指针,但是它不增加对象的引用次数,即弱引用。weak_ptr不控制对象的生命期,但它知道对象是否活着(想象成用棉线轻轻拴住堆上的对象)。如果对象还活着,那么它可以提升为有效的shared_ptr;如果对象已经死了,提升会失败,返回一个空的shared_ptr。“提升”(调用lock()函数)行为是线程安全的。没有重载 * 和 -> 但可以使用lock获得一个可用的shared_ptr对象。
- weak_ptr和shared_ptr搭配使用解决shared环形死锁。
- weak_ptr实现”弱回调“。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>
void check(std::weak_ptr<int> &wp)
{
std::shared_ptr<int> sp = wp.lock(); // 转换为shared_ptr<int>
if (sp != nullptr)
{
std::cout << "still: " << *sp << std::endl;
}
else
{
std::cout << "still: " << "pointer is invalid" << std::endl;
}
}
void mytest()
{
std::shared_ptr<int> sp1(new int(22));
std::shared_ptr<int> sp2 = sp1;
std::weak_ptr<int> wp = sp1; // 指向shared_ptr<int>所指对象
std::cout << "count: " << wp.use_count() << std::endl; // count: 2
std::cout << *sp1 << std::endl; // 22
std::cout << *sp2 << std::endl; // 22
check(wp); // still: 22
sp1.reset();
std::cout << "count: " << wp.use_count() << std::endl; // count: 1
std::cout << *sp2 << std::endl; // 22
check(wp); // still: 22
sp2.reset();
std::cout << "count: " << wp.use_count() << std::endl; // count: 0
check(wp); // still: pointer is invalid
return;
}
int main()
{
mytest();
system("pause");
return 0;
}