什么是智能指针,我应该什么时候使用它?
原文由 Alex Reynolds 发布,翻译遵循 CC BY-SA 4.0 许可协议
什么是智能指针。
长版,原则上:
https://web.stanford.edu/class/archive/cs/cs106l/cs106l.1192/lectures/lecture15/15_RAII.pdf
现代 C++ 习语:
RAII: Resource Acquisition Is Initialization.
● When you initialize an object, it should already have
acquired any resources it needs (in the constructor).
● When an object goes out of scope, it should release every
resource it is using (using the destructor).
关键:
● There should never be a half-ready or half-dead object.
● When an object is created, it should be in a ready state.
● When an object goes out of scope, it should release its resources.
● The user shouldn’t have to do anything more.
原始指针违反 RAII :当指针超出范围时需要用户手动删除。
RAII 解决方案是:
Have a smart pointer class:
● Allocates the memory when initialized
● Frees the memory when destructor is called
● Allows access to underlying pointer
对于需要复制和共享的智能指针,请使用 shared_ptr:
● use another memory to store Reference counting and shared.
● increment when copy, decrement when destructor.
● delete memory when Reference counting is 0.
also delete memory that store Reference counting.
对于不拥有原始指针的智能指针,请使用weak_ptr:
● not change Reference counting.
shared_ptr 用法:
correct way:
std::shared_ptr<T> t1 = std::make_shared<T>(TArgs);
std::shared_ptr<T> t2 = std::shared_ptr<T>(new T(Targs));
wrong way:
T* pt = new T(TArgs); // never exposure the raw pointer
shared_ptr<T> t1 = shared_ptr<T>(pt);
shared_ptr<T> t2 = shared_ptr<T>(pt);
始终避免使用原始指针。
对于必须使用原始指针的场景:
https://stackoverflow.com/a/19432062/2482283
对于非 nullptr 的原始指针,请改用引用。
not use T*
use T&
对于可能为 nullptr 的可选引用,请使用原始指针,这意味着:
T* pt; is optional reference and maybe nullptr.
Not own the raw pointer,
Raw pointer is managed by some one else.
I only know that the caller is sure it is not released now.
原文由 lbsweek 发布,翻译遵循 CC BY-SA 4.0 许可协议
3 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
更新
这个答案相当陈旧,因此描述了当时“好”的东西,即 Boost 库提供的智能指针。从 C++11 开始,标准库已经提供了足够的智能指针类型,因此您应该倾向于使用
std::unique_ptr
、std::shared_ptr
和std::weak_ptr
。还有
std::auto_ptr
。它非常像一个作用域指针,除了它还具有被复制的“特殊”危险能力——这也会意外地转移所有权。它在 C++11 中被弃用并在 C++17 中被删除,所以你不应该使用它。
旧答案
智能指针是包装“原始”(或“裸”)C++ 指针的类,用于管理所指向对象的生命周期。没有单一的智能指针类型,但它们都试图以实用的方式抽象原始指针。
智能指针应该优先于原始指针。如果你觉得你需要使用指针(首先考虑你是否 真的 这样做),你通常会想要使用智能指针,因为这可以缓解原始指针的许多问题,主要是忘记删除对象和内存泄漏。
使用原始指针,程序员必须在对象不再有用时显式销毁它。
相比之下,智能指针定义了关于何时销毁对象的策略。您仍然必须创建对象,但您不再需要担心销毁它。
使用中最简单的策略涉及智能指针包装对象的范围,例如由
boost::scoped_ptr
或std::unique_ptr
实现。注意
std::unique_ptr
实例不能被复制。这可以防止指针被多次(错误地)删除。但是,您可以将对它的引用传递给您调用的其他函数。std::unique_ptr
当您想将对象的生命周期与特定代码块联系起来,或者如果您将其作为成员数据嵌入到另一个对象中时,该对象的生命周期非常有用。该对象一直存在,直到退出包含代码块,或者直到包含对象本身被销毁。更复杂的智能指针策略涉及对指针的引用计数。这确实允许复制指针。当对象的最后一个“引用”被销毁时,该对象被删除。该政策由
boost::shared_ptr
和std::shared_ptr
实施。当对象的生命周期要复杂得多并且不直接绑定到特定代码段或另一个对象时,引用计数指针非常有用。
引用计数指针有一个缺点——创建悬空引用的可能性:
另一种可能性是创建循环引用:
为了解决这个问题,Boost 和 C++11 都定义了
weak_ptr
来定义对shared_ptr
的弱(未计数)引用。