[C++] 什么是智能指针(Smart Pointer)以及何时使用

答案 1

智能指针是一个类,它封装了一个原始的C++指针,以管理所指对象的生命期。没有单一的智能指针类型,但所有这些都尝试以实用的方式抽象原始指针。

智能指针应优先于原始指针。 如果你觉得你需要使用指针(首先要考虑你是否真的需要指针),你通常会想要使用智能指针,因为这可以缓解原始指针的许多问题,主要是忘记删除对象和泄漏内存。

如果使用原始指针,程序员必须在指针不再有用时显式地销毁该对象。

// Need to create the object to achieve some goal
MyObject* ptr = new MyObject(); 
ptr->DoSomething(); // Use the object in some way
delete ptr; // Destroy the object. Done with it.
// Wait, what if DoSomething() raises an exception...?

相比之下,智能指针定义了关于何时销毁对象的策略,你仍然需要创建对象,但是对象的销毁不需要再作考虑。

SomeSmartPtr<MyObject> ptr(new MyObject());
ptr->DoSomething(); // Use the object in some way.

// Destruction of the object happens, depending 
// on the policy the smart pointer class uses.

// Destruction would happen even if DoSomething() 
// raises an exception

使用中最简单的策略涉及智能指针包装对象的域,例如由boost :: scoped_ptrstd :: unique_ptr实现的对象。

void f()
{
    {
       boost::scoped_ptr<MyObject> ptr(new MyObject());
       ptr->DoSomethingUseful();
    } // boost::scopted_ptr goes out of scope -- 
      // the MyObject is automatically destroyed.
      // ptr->Oops(); // Compile error: "ptr" not defined
                      // since it is no longer in scope.
}

请注意,scoped_ptr实例不能复制。 这可以防止指针被(错误地)多次删除。 但是,你可以将引用传递给你调用的其他函数。

当你想要将对象的生命周期与特定的代码块相关联时,或者如果你将其作为成员数据嵌入到另一个对象(另一个对象的生命周期)中时,域指针很有用。 该对象一直存在,直到退出包含的代码块,或者直到包含的对象本身被销毁为止。

更复杂的智能指针策略涉及指针引用计数, 允许指针被复制。 当销毁对象的最后一个“引用”时,将删除该对象。 此策略由boost :: shared_ptrstd :: shared_ptr实现。

void f()
{
    typedef std::shared_ptr<MyObject> MyObjectPtr; // nice short alias
    MyObjectPtr p1; // Empty
    {
        MyObjectPtr p2(new MyObject());
        // There is now one "reference" to the created object
        p1 = p2; // Copy the pointer.
        // There are now two references to the object.
    } // p2 is destroyed, leaving one reference to the object.
} // p1 is destroyed, leaving a reference count of zero. 
  // The object is deleted.

当对象的生命周期变得十分复杂, 并且不与特定的代码段或另一个对象直接绑定时,引用计数指针非常有用。但引用计数指针有一个缺点 - 即有可能会创建悬浮引用:

// Create the smart pointer on the heap
MyObjectPtr* pp = new MyObjectPtr(new MyObject())
// Hmm, we forgot to destroy the smart pointer,
// because of that, the object is never destroyed!

引用计数指针也有可能会创建环状引用(circular references):

struct Owner {
   boost::shared_ptr<Owner> other;
};
boost::shared_ptr<Owner> p1 (new Owner());
boost::shared_ptr<Owner> p2 (new Owner());
p1->other = p2; // p1 references p2
p2->other = p1; // p2 references p1
// Oops, the reference count of of p1 and p2 never goes to zero!
// The objects are never destroyed!

为了应对这个问题,Boost和C++11都定义了 weak_ptr以定义一个shared_ptr的弱(未计数的)引用。

更新

这个答案相当老了,描述的是当时“很好”的东西,也就是Boost库提供的智能指针。自从C++ 11以来,标准库已经提供了足够多的智能指针类型, 所以你应当优先使用 std::unique_ptr, std::shared_ptrstd::weak_ptr

还有一种智能指针叫做std::auto_ptr, 它非常像一种域指针( scoped pointer),除此之外,它还有一种异常危险的能力,即允许被复制,这种能力也会意外地转移所有权!std::auto_ptr在最新的标准中已被废弃,所以,你不应当再使用它,而是应使用std::unique_ptr

std::auto_ptr<MyObject> p1 (new MyObject());
std::auto_ptr<MyObject> p2 = p1; // Copy and transfer ownership. 
                                 // p1 gets set to empty!
p2->DoSomething(); // Works.
p1->DoSomething(); // Oh oh. Hopefully raises some NULL pointer exception.

答案 2

  • 什么是智能指针?
    它是一种可以像指针一样使用的值,但提供了自动内存管理的附加功能:当指针不再使用时,它指向的内存被释放(请参阅维基百科上更详细的定义)。

  • 我什么时候应该使用智能指针呢 ?
    在代码中涉及跟踪一块内存的所有权,分配或取消分配; 使用智能指针通常可以省掉这些操作。

  • 但是我应该在哪些情况下使用哪个智能指针呢?
    当你不打算对同一对象持有多个引用时,请使用std :: unique_ptr。 例如,将它用作指向内存的指针,该指针在进入某个域时被分配,并在退出该域时被解除分配。
    当你想从多个地方引用你的对象, 并且不希望在所有这些引用都消失之前释放它,使用std :: shared_ptr
    Use std::weak_ptr when you do want to refer to your object from multiple places - for those references for which it’s ok to ignore and deallocate (so they’ll just note the object is gone when you try to dereference).
    当你想从多个地方引用你的对象,这些引用忽略并且解除分配都ok(当你试图解引用时,他们会注意到对象已被销毁),使用std :: weak_ptr
    不要使用boost :: smart指针或std :: auto_ptr,除非在特殊情况下你必须并且能够研究这两种指针。

  • 那么我何时应该使用常规指针呢?
    主要在忽略内存所有权的代码中。 这通常是在从其他地方获取了指针,并且不进行分配,解除分配或存储比其执行更长的指针的副本的函数中。


[1] https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one

猜你喜欢

转载自blog.csdn.net/ftell/article/details/80395597