什么是智能指针,什么时候应该使用?


当前回答

更新:

对于过去使用的C++类型,这个答案已经过时了。std::auto_ptr在新标准中被弃用并删除。应该使用std::shared_ptr而不是boost::shared_pt,这是标准的一部分。

与智能指针原理背后的概念的联系仍然是最相关的。

现代C++具有以下智能指针类型,不需要boost智能指针:

std::shared_ptr标准::weak_ptr标准::unique_ptr

答案中还提到了该书的第二版:《C++模板:完整指南第二版》,作者:David Vandvoorde Nicolai、M.Josuttis、Douglas Gregor


旧答案:

智能指针是一种类似指针的类型,具有一些附加功能,例如自动内存释放、引用计数等。

智能指针页面上有一个小介绍-什么,为什么,哪个?。

其中一种简单的智能指针类型是std::auto_ptr(C++标准第20.4.5章),它允许在内存超出范围时自动释放内存,并且在抛出异常时比简单的指针用法更健壮,尽管灵活性较低。

另一种方便的类型是boost::shared_ptr,它实现引用计数,并在没有对对象的引用时自动释放内存。这有助于避免内存泄漏,并且易于用于实现RAII。

David Vandvoorde、Nicolai M.Josuttis的《C++模板:完整指南》一书第20章对这一主题进行了深入探讨。智能指针。涉及的一些主题:

防止异常Holders,(注意,std::auto_ptr是此类智能指针的实现)资源获取是初始化(这通常用于C++中的异常安全资源管理)持有人限制参考计数并发计数器访问销毁和处置

其他回答

更新:

对于过去使用的C++类型,这个答案已经过时了。std::auto_ptr在新标准中被弃用并删除。应该使用std::shared_ptr而不是boost::shared_pt,这是标准的一部分。

与智能指针原理背后的概念的联系仍然是最相关的。

现代C++具有以下智能指针类型,不需要boost智能指针:

std::shared_ptr标准::weak_ptr标准::unique_ptr

答案中还提到了该书的第二版:《C++模板:完整指南第二版》,作者:David Vandvoorde Nicolai、M.Josuttis、Douglas Gregor


旧答案:

智能指针是一种类似指针的类型,具有一些附加功能,例如自动内存释放、引用计数等。

智能指针页面上有一个小介绍-什么,为什么,哪个?。

其中一种简单的智能指针类型是std::auto_ptr(C++标准第20.4.5章),它允许在内存超出范围时自动释放内存,并且在抛出异常时比简单的指针用法更健壮,尽管灵活性较低。

另一种方便的类型是boost::shared_ptr,它实现引用计数,并在没有对对象的引用时自动释放内存。这有助于避免内存泄漏,并且易于用于实现RAII。

David Vandvoorde、Nicolai M.Josuttis的《C++模板:完整指南》一书第20章对这一主题进行了深入探讨。智能指针。涉及的一些主题:

防止异常Holders,(注意,std::auto_ptr是此类智能指针的实现)资源获取是初始化(这通常用于C++中的异常安全资源管理)持有人限制参考计数并发计数器访问销毁和处置

http://en.wikipedia.org/wiki/Smart_pointer

在计算机科学中,智能指针是一种抽象数据类型模拟指针,同时提供其他功能,如自动垃圾收集或边界检查。这些附加功能旨在减少因误用指针,同时保持效率。智能指针通常跟踪指向它们的对象内存管理的目的。这个指针的误用是一个主要原因错误:恒定分配,释放和引用由编写的程序执行使用指针很可能会发生一些内存泄漏。智能指针试图阻止内存通过制造资源泄漏自动解除分配:当指向对象的指针(或一系列指针)被破坏例如,因为它超出了范围,指向的对象也被破坏。

Chris、Sergdev和Llyod提供的定义是正确的。我更喜欢一个更简单的定义,只是为了让我的生活简单:智能指针只是一个重载->和*运算符的类。这意味着你的对象在语义上看起来像一个指针,但你可以让它做一些更酷的事情,包括引用计数、自动销毁等。sharedptr和autoptr在大多数情况下都是足够的,但它们也有自己的一些小特点。

以下是类似答案的链接:http://sickprogrammersarea.blogspot.in/2014/03/technical-interview-questions-on-c_6.html

智能指针是一个动作、外观和感觉都像普通指针但提供更多功能的对象。在C++中,智能指针被实现为封装指针并重写标准指针运算符的模板类。与常规指针相比,它们有许多优点。它们被保证初始化为空指针或指向堆对象的指针。检查通过空指针的定向。无需删除。当指向对象的最后一个指针消失时,对象将自动释放。这些智能指针的一个重要问题是,与常规指针不同,它们不尊重继承。智能指针对多态代码没有吸引力。下面给出了智能指针的实现示例。

例子:

template <class X>
class smart_pointer
{
          public:
               smart_pointer();                          // makes a null pointer
               smart_pointer(const X& x)            // makes pointer to copy of x

               X& operator *( );
               const X& operator*( ) const;
               X* operator->() const;

               smart_pointer(const smart_pointer <X> &);
               const smart_pointer <X> & operator =(const smart_pointer<X>&);
               ~smart_pointer();
          private:
               //...
};

此类实现了指向X类型对象的智能指针。对象本身位于堆上。以下是如何使用它:

smart_pointer <employee> p= employee("Harris",1333);

与其他重载运算符一样,p的行为类似于常规指针,

cout<<*p;
p->raise_salary(0.5);

什么是智能指针。

长版本,原则上:

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

对于非空指针的原始指针,请改用引用。

not use T*
use T&  

对于可能为null的可选引用,请使用原始指针,这意味着:

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.