这篇文章试图说明如何使用auto_ptr和shared_ptr,从而使得动态分配对象的使用和管理更安全,方便。除了一般的使用说明外,更主要是说明它们之间的异同 —— 满足需求的不同和开销上的差异。
文章的多数知识都来源于:
1. Exceptional C++(Herb)Item 37 auto_ptr
2. Exceptional C++ Style(Herb)和C++ Coding Standard(Herb,Andrei)其中一些关于使用shared_ptr的论述
3. GC++和VC++ 8.0 auto_ptr的源代码
4. Boost库shared_ptr的源码和文档
auto_ptr和shared_ptr都是智能指针的一种实现,所谓智能指针,多数情况下都是指这样的一些对象:
1. 内部有一个动态分配对象的指针,拥有该对象的使用权和所有权(独占或共享)。
2. 重载*和->操作,行为上跟所拥有的对象的指针一致。
3. 当自身的生命期结束的时候,会做一些跟拥有对象相关的清理动作。
auto_ptr
auto_ptr是现在标准库里面一个轻量级的智能指针的实现,存在于头文件 memory中,之所以说它是轻量级,是因为它只有一个成员变量(拥有对象的指针),相关的调用开销也非常小。
下面的代码来自于VS2015中的VC++里面的源码:
#if _HAS_AUTO_PTR_ETC // TEMPLATE CLASS auto_ptr template<class _Ty> class auto_ptr; template<class _Ty> struct auto_ptr_ref { // proxy reference for auto_ptr copying explicit auto_ptr_ref(_Ty *_Right) : _Ref(_Right) { // construct from generic pointer to auto_ptr ptr } _Ty *_Ref; // generic pointer to auto_ptr ptr }; template<class _Ty> class auto_ptr { // wrap an object pointer to ensure destruction public: typedef auto_ptr<_Ty> _Myt; typedef _Ty element_type; explicit auto_ptr(_Ty *_Ptr = 0) _THROW0() : _Myptr(_Ptr) { // construct from object pointer } auto_ptr(_Myt& _Right) _THROW0() : _Myptr(_Right.release()) { // construct by assuming pointer from _Right auto_ptr } auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0() { // construct by assuming pointer from _Right auto_ptr_ref _Ty *_Ptr = _Right._Ref; _Right._Ref = 0; // release old _Myptr = _Ptr; // reset this } template<class _Other> operator auto_ptr<_Other>() _THROW0() { // convert to compatible auto_ptr return (auto_ptr<_Other>(*this)); } template<class _Other> operator auto_ptr_ref<_Other>() _THROW0() { // convert to compatible auto_ptr_ref _Other *_Cvtptr = _Myptr; // test implicit conversion auto_ptr_ref<_Other> _Ans(_Cvtptr); _Myptr = 0; // pass ownership to auto_ptr_ref return (_Ans); } template<class _Other> _Myt& operator=(auto_ptr<_Other>& _Right) _THROW0() { // assign compatible _Right (assume pointer) reset(_Right.release()); return (*this); } template<class _Other> auto_ptr(auto_ptr<_Other>& _Right) _THROW0() : _Myptr(_Right.release()) { // construct by assuming pointer from _Right } _Myt& operator=(_Myt& _Right) _THROW0() { // assign compatible _Right (assume pointer) reset(_Right.release()); return (*this); } _Myt& operator=(auto_ptr_ref<_Ty> _Right) _THROW0() { // assign compatible _Right._Ref (assume pointer) _Ty *_Ptr = _Right._Ref; _Right._Ref = 0; // release old reset(_Ptr); // set new return (*this); } ~auto_ptr() _NOEXCEPT { // destroy the object delete _Myptr; } _Ty& operator*() const _THROW0() { // return designated value #if _ITERATOR_DEBUG_LEVEL == 2 if (_Myptr == 0) _DEBUG_ERROR("auto_ptr not dereferencable"); #endif /* _ITERATOR_DEBUG_LEVEL == 2 */ return (*get()); } _Ty *operator->() const _THROW0() { // return pointer to class object #if _ITERATOR_DEBUG_LEVEL == 2 if (_Myptr == 0) _DEBUG_ERROR("auto_ptr not dereferencable"); #endif /* _ITERATOR_DEBUG_LEVEL == 2 */ return (get()); } _Ty *get() const _THROW0() { // return wrapped pointer return (_Myptr); } _Ty *release() _THROW0() { // return wrapped pointer and give up ownership _Ty *_Tmp = _Myptr; _Myptr = 0; return (_Tmp); } void reset(_Ty *_Ptr = 0) { // destroy designated object and store new pointer if (_Ptr != _Myptr) delete _Myptr; _Myptr = _Ptr; } private: _Ty *_Myptr; // the wrapped object pointer }; #endif /* _HAS_AUTO_PTR_ETC */ _STD_END
里面有个auto_ptr_ref的数据结构,我们可以把它忽略,这个只是内部使用的代理结构,用于一些隐式的const变化,我们客户端代码通常不会直接使用到它。
我们可以看到除了构造函数,拷贝构造函数,赋值函数,析构函数和两个重载操作符(*,->)外,还有get,release和reset三个函数,它们的作用分别是:
1. get,获得内部对象的指针
2. release,放弃内部对象的所有权,将内部指针置为空
3. reset,销毁内部对象并接受新的对象的所有权(如果使用缺省参数的话,也就是没有任何对象的所有权)
下面的例程来自Exceptional C++,Item 37:
// Example 2: Using an auto_ptr // void g() { T* pt1 = new T; // right now, we own the allocated object // pass ownership to an auto_ptr auto_ptr<T> pt2( pt1 ); // use the auto_ptr the same way // we'd use a simple pointer *pt2 = 12; // same as "*pt1 = 12;" pt2->SomeFunc(); // same as "pt1->SomeFunc();" // use get() to see the pointer value assert( pt1 == pt2.get() ); // use release() to take back ownership T* pt3 = pt2.release(); // delete the object ourselves, since now // no auto_ptr owns it any more delete pt3; } // pt2 doesn't own any pointer, and so won't // try to delete it... OK, no double delete // Example 3: Using reset() // void h() { auto_ptr<T> pt( new T(1) ); pt.reset( new T(2) ); // deletes the first T that was // allocated with "new T(1)" } // finally, pt goes out of scope and // the second T is also deleted
从上面的例子来看,auto_ptr的使用很简单,通过构造函数拥有一个动态分配对象的所有权,然后就可以被当作对象指针来使用,当auto_ptr对象被销毁的时候,它也会自动销毁自己拥有所有权的对象(嗯,标准的RAAI做法),release可以用来手动放弃所有权,reset可用于手动销毁内部对象。
但实际上,auto_ptr是一个相当容易被误用并且在实际中常常被误用的类。原因是由于它的对象所有权占用的特性和它非平凡的拷贝行为。
auto_ptr的对象所有权是独占性的!
这决定了不可能有两个auto_ptr对象同时拥有同一动态对象的所有权,从而也导致了auto_ptr的拷贝行为是非对等的,其中伴随着对象所有权的转移。
我们仔细观察auto_ptr的源码就会发现拷贝构造和赋值操作符所接受的参数类型都是非const的引用类型(auto_ptr<_Ty>& ),而不是我们一般应该使用的const引用类型,查看源码我们会发现:
auto_ptr(auto_ptr<_Ty>& _Right) _THROW0() : _Myptr(_Right.release()) { // construct by assuming pointer from _Right auto_ptr } template<class _Other> auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0() { // assign compatible _Right (assume pointer) reset(_Right.release()); return (*this); }
拷贝过程中被拷贝的对象(_Right)都会被调用release来放弃所包括的动态对象的所有权,动态对象的所有权被转移了,新的auto_ptr独占了动态对象的所有权。也就是说被拷贝对象在拷贝过程中会被修改,拷贝物与被拷贝物之间是非等价的。这意味着如下的代码是错误的(例程来自 Exceptional C++ Item 37):
// Example 6: Never try to do work through // a non-owning auto_ptr // void f() { auto_ptr<T> pt1( new T ); auto_ptr<T> pt2; pt2 = pt1; // now pt2 owns the pointer, and // pt1 does not pt1->DoSomething(); // error: following a null pointer }
同时也不要将auto_ptr放进标准库的容器中,否则在标准库容器无准备的拷贝行为中(标准库容器需要的拷贝行为是等价的),会导致难以发觉的错误。(请参考Exceptional C++ Item 37获得更多信息)
auto_ptr特殊的拷贝行为使得使用它来远距离传递动态对象变成了一件十分危险的行为,在传递的过程中,一不小心就会留下一些实际为空但程序本身却缺少这样认知的auto_ptr对象。
简单的说来,auto_ptr适合用来管理生命周期比较短或者不会被远距离传递的动态对象,使用auto_ptr来管理动态分配对象,最好是局限于某个函数内部或者是某个类的内部。也就是说,动态对象的产生,使用和销毁的全过程是处于一个小的受控的范围,而不会在其中加入一些适应未来时态的扩展。
接下来再来谈谈 真正的智能指针,即:shared_ptr
shared_ptr
shared_ptr是Boost库所提供的一个智能指针的实现,正如其名字所蕴意的一样:
An important goal of shared_ptr is to provide a standard shared-ownership pointer.
shared_ptr的一个重要目的就是为了提供一个标准的共享所有权的智能指针。
—— Boost库文档
没错,shared_ptr就是为了解决auto_ptr在对象所有权上的局限性(auto_ptr是独占的),在使用引用计数的机制上提供了可以共享所有权的智能指针,当然这不会没有任何额外的代价……
首先一个shared_ptr对象除了包括一个所拥有对象的指针(px)外,还必须包括一个引用计数代理对象(shared_count)的指针(pn)。而这个引用计数代理对象包括一个真正的多态的引用计数对象(sp_counted_base)的指针(_pi),真正的引用计数对象在使用VC编译器的情况下包括一个虚表,一个虚表指针,和两个计数器。
下图中result是一个shared_ptr对象,我们可以清楚看到它展开后所包含的数据:
假设我们有多个(5个以上)shared_ptr共享一个动态对象,那么每个shared_ptr的开销比起只使用原生指针的开销大概在3,4倍左右(这还是理想状况,忽略了动态分配带来的俑余开销)。如果只有一个shared_ptr独占动态对象,空间上开销更是高度十数倍!而auto_ptr的开销只是使用原生指针的两倍。
时间上的开销主要在初始化和拷贝操作上,*和->操作符重载的开销跟auto_ptr是一样的。
当然开销并不是我们不使用shared_ptr的理由,永远不要进行不成熟的优化,直到性能分析器告诉你这一点,这是Hurb提出的明智的建议。以上的说明只是为了让你了解强大的功能背后总是伴随着更多的开销,shared_ptr应该被使用,但是也不要过于滥用,特别是在一些auto_ptr更擅长的地方。
下面是shared_ptr的类型定义:
// TEMPLATE CLASS shared_ptr template<class _Ty> class shared_ptr : public _Ptr_base<_Ty> { // class for reference counted resource management public: typedef shared_ptr<_Ty> _Myt; typedef _Ptr_base<_Ty> _Mybase; constexpr shared_ptr() _NOEXCEPT { // construct empty shared_ptr } template<class _Ux> explicit shared_ptr(_Ux *_Px) { // construct shared_ptr object that owns _Px _Resetp(_Px); } template<class _Ux, class _Dx> shared_ptr(_Ux *_Px, _Dx _Dt) { // construct with _Px, deleter _Resetp(_Px, _Dt); } constexpr shared_ptr(nullptr_t) _NOEXCEPT { // construct empty shared_ptr } template<class _Dx> shared_ptr(nullptr_t, _Dx _Dt) { // construct with nullptr, deleter _Resetp((_Ty *)0, _Dt); } template<class _Dx, class _Alloc> shared_ptr(nullptr_t, _Dx _Dt, _Alloc _Ax) { // construct with nullptr, deleter, allocator _Resetp((_Ty *)0, _Dt, _Ax); } template<class _Ux, class _Dx, class _Alloc> shared_ptr(_Ux *_Px, _Dx _Dt, _Alloc _Ax) { // construct with _Px, deleter, allocator _Resetp(_Px, _Dt, _Ax); } template<class _Ty2> shared_ptr(const shared_ptr<_Ty2>& _Right, _Ty *_Px) _NOEXCEPT { // construct shared_ptr object that aliases _Right this->_Reset(_Px, _Right); } shared_ptr(const _Myt& _Other) _NOEXCEPT { // construct shared_ptr object that owns same resource as _Other this->_Reset(_Other); } template<class _Ty2, class = typename enable_if<is_convertible<_Ty2 *, _Ty *>::value, void>::type> shared_ptr(const shared_ptr<_Ty2>& _Other) _NOEXCEPT { // construct shared_ptr object that owns same resource as _Other this->_Reset(_Other); } template<class _Ty2> explicit shared_ptr(const weak_ptr<_Ty2>& _Other, bool _Throw = true) { // construct shared_ptr object that owns resource *_Other this->_Reset(_Other, _Throw); } #if _HAS_AUTO_PTR_ETC template<class _Ty2> shared_ptr(auto_ptr<_Ty2>&& _Other) { // construct shared_ptr object that owns *_Other.get() this->_Reset(_STD move(_Other)); } #endif /* _HAS_AUTO_PTR_ETC */ shared_ptr(_Myt&& _Right) _NOEXCEPT : _Mybase(_STD move(_Right)) { // construct shared_ptr object that takes resource from _Right } template<class _Ty2, class = typename enable_if<is_convertible<_Ty2 *, _Ty *>::value, void>::type> shared_ptr(shared_ptr<_Ty2>&& _Right) _NOEXCEPT : _Mybase(_STD move(_Right)) { // construct shared_ptr object that takes resource from _Right } template<class _Ux, class _Dx, class = typename enable_if<is_convertible< typename unique_ptr<_Ux, _Dx>::pointer, _Ty *>::value, void>::type> shared_ptr(unique_ptr<_Ux, _Dx>&& _Right) { // construct from unique_ptr _Resetp(_Right.release(), _Right.get_deleter()); } template<class _Ux, class _Dx> _Myt& operator=(unique_ptr<_Ux, _Dx>&& _Right) { // move from unique_ptr shared_ptr(_STD move(_Right)).swap(*this); return (*this); } _Myt& operator=(_Myt&& _Right) _NOEXCEPT { // take resource from _Right shared_ptr(_STD move(_Right)).swap(*this); return (*this); } template<class _Ty2> _Myt& operator=(shared_ptr<_Ty2>&& _Right) _NOEXCEPT { // take resource from _Right shared_ptr(_STD move(_Right)).swap(*this); return (*this); } ~shared_ptr() _NOEXCEPT { // release resource this->_Decref(); } _Myt& operator=(const _Myt& _Right) _NOEXCEPT { // assign shared ownership of resource owned by _Right shared_ptr(_Right).swap(*this); return (*this); } template<class _Ty2> _Myt& operator=(const shared_ptr<_Ty2>& _Right) _NOEXCEPT { // assign shared ownership of resource owned by _Right shared_ptr(_Right).swap(*this); return (*this); } #if _HAS_AUTO_PTR_ETC template<class _Ty2> _Myt& operator=(auto_ptr<_Ty2>&& _Right) { // assign ownership of resource pointed to by _Right shared_ptr(_STD move(_Right)).swap(*this); return (*this); } #endif /* _HAS_AUTO_PTR_ETC */ void reset() _NOEXCEPT { // release resource and convert to empty shared_ptr object shared_ptr().swap(*this); } template<class _Ux> void reset(_Ux *_Px) { // release, take ownership of _Px shared_ptr(_Px).swap(*this); } template<class _Ux, class _Dx> void reset(_Ux *_Px, _Dx _Dt) { // release, take ownership of _Px, with deleter _Dt shared_ptr(_Px, _Dt).swap(*this); } template<class _Ux, class _Dx, class _Alloc> void reset(_Ux *_Px, _Dx _Dt, _Alloc _Ax) { // release, take ownership of _Px, with deleter _Dt, allocator _Ax shared_ptr(_Px, _Dt, _Ax).swap(*this); } void swap(_Myt& _Other) _NOEXCEPT { // swap pointers this->_Swap(_Other); } _Ty *get() const _NOEXCEPT { // return pointer to resource return (this->_Get()); } typename add_lvalue_reference<_Ty>::type operator*() const _NOEXCEPT { // return reference to resource return (*this->_Get()); } _Ty *operator->() const _NOEXCEPT { // return pointer to resource return (this->_Get()); } bool unique() const _NOEXCEPT { // return true if no other shared_ptr object owns this resource return (this->use_count() == 1); } explicit operator bool() const _NOEXCEPT { // test if shared_ptr object owns no resource return (this->_Get() != 0); } private: template<class _Ux> void _Resetp(_Ux *_Px) { // release, take ownership of _Px _TRY_BEGIN // allocate control block and reset _Resetp0(_Px, new _Ref_count<_Ux>(_Px)); _CATCH_ALL // allocation failed, delete resource delete _Px; _RERAISE; _CATCH_END } template<class _Ux, class _Dx> void _Resetp(_Ux *_Px, _Dx _Dt) { // release, take ownership of _Px, deleter _Dt _TRY_BEGIN // allocate control block and reset _Resetp0(_Px, new _Ref_count_del<_Ux, _Dx>(_Px, _Dt)); _CATCH_ALL // allocation failed, delete resource _Dt(_Px); _RERAISE; _CATCH_END } template<class _Ux, class _Dx, class _Alloc> void _Resetp(_Ux *_Px, _Dx _Dt, _Alloc _Ax) { // release, take ownership of _Px, deleter _Dt, allocator _Ax typedef _Ref_count_del_alloc<_Ux, _Dx, _Alloc> _Refd; typedef _Wrap_alloc<_Alloc> _Alref0; typename _Alref0::template rebind<_Refd>::other _Alref(_Ax); _TRY_BEGIN // allocate control block and reset _Refd *_Pref = _Alref.allocate(1); _Alref.construct(_Pref, _Px, _Dt, _Ax); _Resetp0(_Px, _Pref); _CATCH_ALL // allocation failed, delete resource _Dt(_Px); _RERAISE; _CATCH_END } public: template<class _Ux> void _Resetp0(_Ux *_Px, _Ref_count_base *_Rx) { // release resource and take ownership of _Px this->_Reset0(_Px, _Rx); _Enable_shared(_Px, _Rx); } };
大多数成员函数都跟auto_ptr类似,但是没有了release(请看注释),reset用来放弃所拥有对象的所有权或拥有对象的变更,会引起原有对象的引用计数的减少。
Note: Boost文档里面的QA说明了为什么不提供release函数 Q. Why doesn't shared_ptr provide a release() function? A. shared_ptr cannot give away ownership unless it's unique() because the other copy will still destroy the object. Consider: shared_ptr<int> a(new int); shared_ptr<int> b(a); // a.use_count() == b.use_count() == 2 int * p = a.release(); // Who owns p now? b will still call delete on it in its destructor. Furthermore, the pointer returned by release() would be difficult to deallocate reliably, as the source shared_ptr could have been created with a custom deleter.
use_count返回引用计数的个数,unique拥于确认是否是独占所有权(use_count为1),swap用于交换两个shared_ptr对象(即交换所拥有的对象),有一个bool类型转换操作符使得shared_ptr可用于需要的bool类型的语境下,比如我们通常用if(pointer)来判断某个指针是否为空。
Boost库里面有很多shared_ptr的使用例程,文档里面也列举了许许多多shared_ptr的用途,其中最有用也最常用的莫过于传递动态分配对象,有了引用计数机制,我们现在可以安全地将动态分配的对象包裹在shared_ptr里面跨越模块,跨越线程的边界传递。shared_ptr为我们自动管理对象的生命周期,嗯,C++也可以体会到Java里面使用引用的美妙之处了。
另外,还记得Effective C++里面(或者其它的C++书籍),Scott Meyer告诉你的:在一个由多个模块组成的系统里面,一个模块不用试图自己去释放另外一个模块分配的资源,而应该遵循谁分配谁释放的原则。正确的原则但是有时难免有时让人忽略(过于繁琐),将资源包装在shared_ptr里面传递,而shared_ptr保证了在资源不再被拥有的时候,产生资源的模块的delete语句会被调用。
shared_ptr是可以拷贝和赋值的,拷贝行为也是等价的,并且可以被比较,这意味这它可被放入标准库的一般容器(vector,list)和关联容器中(map)。
shared_ptr可以用来容纳多态对象,比如所下面的例子:
class Base { } class Derived : public Base { } shared_ptr<Base> sp_base(new Derived);
甚至shared_ptr也具备多态的行为:
Derived* pd = new Derived; shared_ptr<Derived> sp_derived(pd); shared_ptr<Base> sp_base2(sp_derived);
上面的语句是合法的,shared_ptr会完成所需的类型转换,当shared_ptr的模版参数Base的确是Derived的基类的时候。
最后是一个小小的提醒,无论是使用auto_ptr还是shared_ptr,都永远不要写这样的代码:
A* pa = new A; xxx_ptr<A> ptr_a_1(pa); xxx_ptr<A> ptr_a_2(pa);
很明显,在ptr_a_1和ptr_a_2生命周期结束的时候都会去删除pa,pa被删除了两次,这肯定会引起你程序的崩溃,当然,这个误用的例子比较明显,但是在某种情况下,可能会一不小心就写出如下的代码(嗯,我承认我的确做过这样的事情):
void DoSomething(xxx_ptr<A>) { //do something } class A { doSomething() { xxx_ptr<A> ptr_a(this); DoSomething(ptr_a); } }; int main() { A a; a.doSomething(); //continue do something with a, but it was already destory }
在函数a.doSomething()里面发生了什么事情,它为了调用DoSomething所以不得不把自己包装成一个xxx_ptr,但是忘记在函数结束的时候,xxx ptr_a被销毁的同时也销毁了自己,程序或者立刻崩溃或者在下面的某个时间点上崩溃!
所以你在使用智能指针做为函数参数的时候请小心这样的误用,有时候使用智能指针作为函数参数不一定是一个好注意。比如请遵循下面的建议,请不要在属于类型A的接口的一部分的非成员函数或者跟A有紧密联系的辅助函数里面使用xxx_ptr<A>作为函数的参数类型。
后续附录:根据以上代码所写的下面代码,并没有崩溃,请测试并思考为什么呢?
#include <QCoreApplication> #include "boost/smart_ptr.hpp" #include "iostream" using namespace std; using namespace boost; class A; void DoSomething(shared_ptr<A> _sptr) { // cout<<"DoSomething"<<endl; } class A { public: void doSomething() { shared_ptr<A> ptr_a(this); DoSomething(ptr_a); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); A a_; a_.doSomething(); a_.doSomething(); a_.doSomething(); a_.doSomething(); a_.doSomething(); return a.exec(); }