参考:
- https://www.cnblogs.com/DswCnblog/p/5628195.html
- https://www.jianshu.com/p/3abef7d9a5ee
- https://blog.csdn.net/luoshabugui/article/details/86130728
std::unique_ptr 是 C++11 起引入的智能指针。为什么必须要在 C++11 起才有该特性,主要还是 C++11 增加了move语义,否则无法对对象的所有权进行传递。
// 示例:
int main()
{
// 创建一个unique_ptr实例
unique_ptr<int> pInt(new int(5));
cout << *pInt;
}
// 示例:
int main()
{
// 创建一个unique_ptr实例
unique_ptr<int> pInt(new int(5));
unique_ptr<int> pInt2(pInt); // 报错
unique_ptr<int> pInt3 = pInt; // 报错
}
// 示例:
int main()
{
unique_ptr<int> pInt(new int(5));
unique_ptr<int> pInt2 = std::move(pInt); // 转移所有权
//cout << *pInt << endl; // 出错,pInt为空
cout << *pInt2 << endl;
unique_ptr<int> pInt3(std::move(pInt2));
}
// 示例:
unique_ptr<int> clone(int p)
{
unique_ptr<int> pInt(new int(p));
return pInt; // 返回unique_ptr
}
int main() {
int p = 5;
unique_ptr<int> ret = clone(p);
cout << *ret << endl;
}
{
//创建一个指向int的空指针
std::unique_ptr<int> fPtr1;
std::unique_ptr<int> fPtr2(new int(4));
auto fPtr3 = std::make_unique<int>();
//fPtr2释放指向对象的所有权,并且被置为nullptr
std::cout << "fPtr2 release before:" << fPtr2.get() << std::endl;
int *pF = fPtr2.release();
std::cout << "fPtr2 release before:" << fPtr2.get() << " and pF value:" << *pF << std::endl;
//所有权转移,转移后fPtr3变为空指针
std::cout << "move before fPtr1 address:" << fPtr1.get() << " fPtr3 address:" << fPtr3.get() << std::endl;
fPtr1 = std::move(fPtr3);
std::cout << "move after fPtr1 address:" << fPtr1.get() << " fPtr3 address:" << fPtr3.get() << std::endl;
std::cout << "move before fPtr1 address:" << fPtr1.get() << std::endl;
fPtr1.reset();
std::cout << "move after fPtr1 address:" << fPtr1.get() << std::endl;
}
# 输出:
fPtr2 release before:00EFB120
fPtr2 release before:00000000 and pF value:4
move before fPtr1 address:00000000 fPtr3 address:00EFEC60
move after fPtr1 address:00EFEC60 fPtr3 address:00000000
move before fPtr1 address:00EFEC60
move after fPtr1 address:00000000
void Func()
{
int *p = new int(5);
// ...(可能会抛出异常)
delete p;
}
这是我们传统的写法:当我们动态申请内存后,有可能我们接下来的代码由于抛出异常或者提前退出(if语句)而没有执行delete操作。
解决的方法是使用 unique_ptr 来管理动态内存,只要 unique_ptr 指针创建成功,其析构函数都会被调用,确保动态资源被释放。
void Func()
{
unique_ptr<int> p(new int(5));
}
unique_ptr<int> Func(int p)
{
unique_ptr<int> pInt(new int(p));
return pInt; // 返回unique_ptr
}
int main() {
int p = 5;
unique_ptr<int> ret = Func(p);
cout << *ret << endl;
// 函数结束后,自动释放资源
}
int main()
{
vector<unique_ptr<int>> vec;
unique_ptr<int> p(new int(5));
vec.push_back(std::move(p)); // 使用移动语义
}
int main()
{
unique_ptr<int[]> p(new int[5] {1, 2, 3, 4, 5});
p[0] = 0; // 重载了operator[]
}
#include
#include
#include
struct Foo
{
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
void bar() { std::cout << "Foo::bar\n"; }
};
void f(const Foo &)
{
std::cout << "f(const Foo&)\n";
}
struct D
{
void operator()(Foo* foo)
{
std::cout << "D operator()" << std::endl;
delete foo;
}
};
void TestAutoDestroy()
{
//1. 普通的new对象.
std::cout << "TestDestroy...................." << std::endl;
{
std::unique_ptr<Foo> p1(new Foo);
}
//2. 普通的new[]对象.
{
std::unique_ptr<Foo[]> p2(new Foo[4]);
}
//3. 自定义的deleter.
{
std::unique_ptr<Foo, D> p3(new Foo);
}
}
void TestOwner()
{
std::cout << "TestOwner...................." << std::endl;
//1. new object.
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
if (p1)
p1->bar();
{
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
f(*p2);
p1 = std::move(p2); // ownership returns to p1
p2->bar();
std::cout << "destroying p2...\n";
}
p1->bar();
}
void TestArrayOwner()
{
std::cout << "TestArrayOwner...................." << std::endl;
//1. new[] object.
std::unique_ptr<Foo[]> p1(new Foo[4]); // p1 owns Foo
if (p1)
p1[0].bar();
{
std::unique_ptr<Foo[]> p2(std::move(p1)); // now p2 owns Foo
f(p2[0]);
p1 = std::move(p2); // ownership returns to p1
p2[0].bar();
std::cout << "destroying p2...\n";
}
p1[0].bar();
}
int main()
{
TestAutoDestroy();
TestOwner();
TestArrayOwner();
}
# 输出:
TestDestroy....................
Foo::Foo
Foo::~Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::Foo
D operator()
Foo::~Foo
TestOwner....................
Foo::Foo
Foo::bar
f(const Foo&)
Foo::bar
destroying p2...
Foo::bar
Foo::~Foo
TestArrayOwner....................
Foo::Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::bar
f(const Foo&)
Foo::bar
destroying p2...
Foo::bar
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo