前言
智能指针是一个保存指向动态内存(堆上内存)的指针的一个对象。它使用起来很像内建指针但是它会在自己析构的时候自动释放指针指向的内存。当遇到异常的时候,智能指针更加有优势,它们会确保管理的对象的内存被正确释放。
如下这个列子即使遇到异常,这个时候会释放所有栈上的内存,这个时候智能指针的对象会被析构从而释放 动态内存 对象所占用的内存。
void Fun () {
smart_ptr<T> ptr(new T);
throw some exception
}
分类
- scoped_ptr:不可拷贝与赋值,承载new,只能在 scoped_ptr 声明的作用域内使用。
- scoped_array:不可拷贝与赋值,承载new []
- shared_ptr:可拷贝,承载new。boost库中重要组成,重点学习。
- shared_array:可拷贝,承载new []。 weak_ptr:弱引用。
- intrusive_ptr:需要自实现计数功能的,引用计数智能指
注意
智能指针调用 T 类型的析构函数 或者 执行其相应的 operator delete 抛出异常的时候是未定义行为。并且大部分智能指针都要求T是一个complete type,除了 shared_ptr 不这么严格,它的大部分成员函数不要求 T是一个complete type。
异常安全
智能指针的所以成员函数基本不会抛出异常,即使抛出了也会确保不会有影响,恢复调用抛出异常的成员函数之前的状态
scoped_ptr
介绍
它有俩个特点
- scoped_ptr 因为放拷贝的原因,只能使用于当前作用域
- 资源不可转移
- 只可管理 new 申请的内存
demo
#include <iostream>
#include <boost/smart_ptr.hpp>
class SmallClass
{
public:
SmallClass(int x_val)
{
x = x_val;
std::cout << "SmallClass construct " << x << std::endl;
}
virtual ~SmallClass()
{
std::cout << "SmallClass destory " << x << std::endl;
}
int GetX()
{
return x;
}
private:
int x;
};
int main()
{
std::cout << "main start" << std::endl;
// scoped_ptr on basic
boost::scoped_ptr<int> int_ptr(new int);
*int_ptr = 100;
++*int_ptr;
std::cout << *int_ptr << std::endl;
// scoped_ptr on class
boost::scoped_ptr<SmallClass> sc_ptr(new SmallClass(0));
std::cout << sc_ptr->GetX() << std::endl;
sc_ptr.reset();
std::cout << "main end" << std::endl;
return 0;
}
实现
从下面的实现可以看出它是完完全全要求T是一个完整类型
template <class T>
void checked_delete(T * p) {
typedef char must_be_complete[ (sizeof (T)) ? 1:-1];
(void) sizeof(must_be_complete);
delete p;
}
template<class T> class scoped_ptr // noncopyable
{
private:
T * px;
scoped_ptr(scoped_ptr const &);
scoped_ptr & operator=(scoped_ptr const &);
typedef scoped_ptr<T> this_type;
void operator==( scoped_ptr const& ) const;
void operator!=( scoped_ptr const& ) const;
public:
typedef T element_type;
explicit scoped_ptr( T * p = 0 ): px( p ) // never throws
{
}
#ifndef BOOST_NO_AUTO_PTR
explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() )
{
}
#endif
~scoped_ptr() // never throws
{
boost::checked_delete( px );
}
void reset(T * p = 0) // never throws
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type(p).swap(*this);
}
T & operator*() const // never throws
{
BOOST_ASSERT( px != 0 );
return *px;
}
T * operator->() const // never throws
{
BOOST_ASSERT( px != 0 );
return px;
}
T * get() const BOOST_NOEXCEPT
{
return px;
}
// implicit conversion to "bool"
#include <boost/smart_ptr/detail/operator_bool.hpp>
void swap(scoped_ptr & b) BOOST_NOEXCEPT
{
T * tmp = b.px;
b.px = px;
px = tmp;
}
};
scoped_array
特点
同 scoped_ptr 基本一样,只不过可接受数组的new [],多了下标访问操作。
- 构造函数指针必须是 new[] 的结果,而不能是 new 表达式的结果
- 没有 *, -> 操作符重载,它真正的把所管理的对象当做成一个数组
- 析构函数使用 delete[] 释放资源,而不是 delete析构函数使用 delete[] 释放资源,而不是 delete
- 没有 begin(), end() 等类似容器迭代器操作函数没有 begin(), end() 等类似容器迭代器操作函数
- 不可与 STL 算法相结合 不可与 STL 算法相结合
demo
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(int argc, const char * argv[]) {
scoped_array<int> scopedarr(new int[100]);
scopedarr[0] = 100; // 赋值
// *(scopedarr+1) = 200; // error
// fill array with 100 value 2
fill_n(&scopedarr[0], 100, 2);
cout << scopedarr[2] << endl;
scopedarr[3] = scopedarr[0]+scopedarr[1];
cout << scopedarr[3] << endl;
cout << scopedarr.get()[3] << endl;
return 0;
}
shared_ptr 实现
template<class T> class shared_ptr
{
private:
// Borland 5.5.1 specific workaround
typedef shared_ptr<T> this_type;
public:
typedef typename boost::detail::sp_element< T >::type element_type;
shared_ptr() BOOST_NOEXCEPT : px( 0 ), pn() // never throws in 1.30+
{
}
template<class Y>
explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
{
boost::detail::sp_pointer_construct( this, p, pn );
}
//
// Requirements: D's copy constructor must not throw
//
// shared_ptr will release p by calling d(p)
//
template<class Y, class D> shared_ptr( Y * p, D d ): px( p ), pn( p, d )
{
boost::detail::sp_deleter_construct( this, p );
}
// As above, but with allocator. A's copy constructor shall not throw.
template<class Y, class D, class A> shared_ptr( Y * p, D d, A a ): px( p ), pn( p, d, a )
{
boost::detail::sp_deleter_construct( this, p );
}
// generated copy constructor, destructor are fine...
// ... except in C++0x, move disables the implicit copy
template<class Y>
explicit shared_ptr( weak_ptr<Y> const & r ): pn( r.pn ) // may throw
{
boost::detail::sp_assert_convertible< Y, T >();
// it is now safe to copy r.px, as pn(r.pn) did not throw
px = r.px;
}
template<class Y>
shared_ptr( weak_ptr<Y> const & r, boost::detail::sp_nothrow_tag )
BOOST_NOEXCEPT : px( 0 ), pn( r.pn, boost::detail::sp_nothrow_tag() )
{
if( !pn.empty() )
{
px = r.px;
}
}
shared_ptr( shared_ptr const & r ) BOOST_NOEXCEPT : px( r.px ), pn( r.pn )
{
}
// aliasing
template< class Y >
shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
{
}
// assignment
shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT
{
this_type(r).swap(*this);
return *this;
}
template<class Y>
shared_ptr & operator=( shared_ptr<Y> && r ) BOOST_NOEXCEPT
{
this_type( static_cast< shared_ptr<Y> && >( r ) ).swap( *this );
return *this;
}
void reset() BOOST_NOEXCEPT // never throws in 1.30+
{
this_type().swap(*this);
}
template<class Y> void reset( Y * p ) // Y must be complete
{
BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors
this_type( p ).swap( *this );
}
// never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
typename boost::detail::sp_dereference< T >::type operator* () const
{
BOOST_ASSERT( px != 0 );
return *px;
}
// never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
typename boost::detail::sp_member_access< T >::type operator-> () const
{
BOOST_ASSERT( px != 0 );
return px;
}
// never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT)
typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const
{
BOOST_ASSERT( px != 0 );
BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );
return px[ i ];
}
element_type * get() const BOOST_NOEXCEPT
{
return px;
}
// implicit conversion to "bool"
#include <boost/smart_ptr/detail/operator_bool.hpp>
bool unique() const BOOST_NOEXCEPT
{
return pn.unique();
}
long use_count() const BOOST_NOEXCEPT
{
return pn.use_count();
}
void swap( shared_ptr & other ) BOOST_NOEXCEPT
{
std::swap(px, other.px);
pn.swap(other.pn);
}
template<class Y> bool owner_before( shared_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
{
return pn < rhs.pn;
}
template<class Y> bool owner_before( weak_ptr<Y> const & rhs ) const BOOST_NOEXCEPT
{
return pn < rhs.pn;
}
void * _internal_get_deleter( boost::detail::sp_typeinfo const & ti ) const BOOST_NOEXCEPT
{
return pn.get_deleter( ti );
}
void * _internal_get_untyped_deleter() const BOOST_NOEXCEPT
{
return pn.get_untyped_deleter();
}
bool _internal_equiv( shared_ptr const & r ) const BOOST_NOEXCEPT
{
return px == r.px && pn == r.pn;
}
// Tasteless as this may seem, making all members public allows member templates
// to work in the absence of member template friends. (Matthew Langston)
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
private:
template<class Y> friend class shared_ptr;
template<class Y> friend class weak_ptr;
#endif
element_type * px; // contained pointer
boost::detail::shared_count pn; // reference counter
}; // shared_ptr
template<class T, class U> inline bool operator==(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
{
return a.get() != b.get();
}
#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
// Resolve the ambiguity between our op!= and the one in rel_ops
template<class T> inline bool operator!=(shared_ptr<T> const & a, shared_ptr<T> const & b) BOOST_NOEXCEPT
{
return a.get() != b.get();
}
#endif
#if !defined( BOOST_NO_CXX11_NULLPTR )
template<class T> inline bool operator==( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator==( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
{
return p.get() == 0;
}
template<class T> inline bool operator!=( shared_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT
{
return p.get() != 0;
}
template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, shared_ptr<T> const & p ) BOOST_NOEXCEPT
{
return p.get() != 0;
}
#endif
template<class T, class U> inline bool operator<(shared_ptr<T> const & a, shared_ptr<U> const & b) BOOST_NOEXCEPT
{
return a.owner_before( b );
}
template<class T> inline void swap(shared_ptr<T> & a, shared_ptr<T> & b) BOOST_NOEXCEPT
{
a.swap(b);
}
template<class T, class U> shared_ptr<T> static_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
{
(void) static_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = static_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
template<class T, class U> shared_ptr<T> const_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
{
(void) const_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = const_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
template<class T, class U> shared_ptr<T> dynamic_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
{
(void) dynamic_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = dynamic_cast< E* >( r.get() );
return p? shared_ptr<T>( r, p ): shared_ptr<T>();
}
template<class T, class U> shared_ptr<T> reinterpret_pointer_cast( shared_ptr<U> const & r ) BOOST_NOEXCEPT
{
(void) reinterpret_cast< T* >( static_cast< U* >( 0 ) );
typedef typename shared_ptr<T>::element_type E;
E * p = reinterpret_cast< E* >( r.get() );
return shared_ptr<T>( r, p );
}
// get_pointer() enables boost::mem_fn to recognize shared_ptr
template<class T> inline typename shared_ptr<T>::element_type * get_pointer(shared_ptr<T> const & p) BOOST_NOEXCEPT
{
return p.get();
}
// operator<<
#if !defined(BOOST_NO_IOSTREAM)
#if defined(BOOST_NO_TEMPLATED_IOSTREAMS) || ( defined(__GNUC__) && (__GNUC__ < 3) )
template<class Y> std::ostream & operator<< (std::ostream & os, shared_ptr<Y> const & p)
{
os << p.get();
return os;
}
#else
// in STLport's no-iostreams mode no iostream symbols can be used
#ifndef _STLP_NO_IOSTREAMS
template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, shared_ptr<Y> const & p)
{
os << p.get();
return os;
}
shared_count 实现
class shared_count
{
private:
sp_counted_base * pi_;
friend class weak_count;
public:
shared_count(): pi_(0) // nothrow
{
}
template<class Y> explicit shared_count( Y * p ): pi_( 0 )
{
try
{
pi_ = new sp_counted_impl_p<Y>( p );
}
catch(...)
{
boost::checked_delete( p );
throw;
}
}
template<class P, class D> shared_count( P p, D d ): pi_(0)
{
try
{
pi_ = new sp_counted_impl_pd<P, D>(p, d);
}
catch(...)
{
d(p); // delete p
throw;
}
}
template< class P, class D > shared_count( P p, sp_inplace_tag<D> ): pi_( 0 )
{
try
{
pi_ = new sp_counted_impl_pd< P, D >( p );
}
catch( ... )
{
D::operator_fn( p ); // delete p
throw;
}
}
template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
{
typedef sp_counted_impl_pda<P, D, A> impl_type;
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
A2 a2( a );
try
{
impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
pi_ = pi;
std::allocator_traits<A2>::construct( a2, pi, p, d, a );
}
catch(...)
{
d( p );
if( pi_ != 0 )
{
a2.deallocate( static_cast< impl_type* >( pi_ ), 1 );
}
throw;
}
if( pi_ != 0 )
{
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
std::allocator_traits<A2>::construct( a2, pi, p, d, a );
#else
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
#endif
}
else
{
d( p );
boost::throw_exception( std::bad_alloc() );
}
}
// auto_ptr<Y> is special cased to provide the strong guarantee
template<class Y>
explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
{
r.release();
}
template<class Y, class D>
explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
{
typedef typename sp_convert_reference<D>::type D2;
D2 d2( r.get_deleter() );
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
r.release();
}
~shared_count() // nothrow
{
if( pi_ != 0 ) pi_->release();
}
shared_count(shared_count const & r): pi_(r.pi_) // nothrow
{
if( pi_ != 0 ) pi_->add_ref_copy();
}
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
shared_count & operator= (shared_count const & r) // nothrow
{
sp_counted_base * tmp = r.pi_;
if( tmp != pi_ )
{
if( tmp != 0 ) tmp->add_ref_copy();
if( pi_ != 0 ) pi_->release();
pi_ = tmp;
}
return *this;
}
void swap(shared_count & r) // nothrow
{
sp_counted_base * tmp = r.pi_;
r.pi_ = pi_;
pi_ = tmp;
}
long use_count() const // nothrow
{
return pi_ != 0? pi_->use_count(): 0;
}
bool unique() const // nothrow
{
return use_count() == 1;
}
bool empty() const // nothrow
{
return pi_ == 0;
}
friend inline bool operator==(shared_count const & a, shared_count const & b)
{
return a.pi_ == b.pi_;
}
friend inline bool operator<(shared_count const & a, shared_count const & b)
{
return std::less<sp_counted_base *>()( a.pi_, b.pi_ );
}
void * get_deleter( sp_typeinfo const & ti ) const
{
return pi_? pi_->get_deleter( ti ): 0;
}
void * get_untyped_deleter() const
{
return pi_? pi_->get_untyped_deleter(): 0;
}
};
sp_counted_base 实现
inline int atomic_exchange_and_add( int * pw, int dv )
{
// int r = *pw;
// *pw += dv;
// return r;
int r;
__asm__ __volatile__
(
"lock\n\t"
"xadd %1, %0":
"=m"( *pw ), "=r"( r ): // outputs (%0, %1)
"m"( *pw ), "1"( dv ): // inputs (%2, %3 == %1)
"memory", "cc" // clobbers
);
return r;
}
inline void atomic_increment( int * pw )
{
//atomic_exchange_and_add( pw, 1 );
__asm__
(
"lock\n\t"
"incl %0":
"=m"( *pw ): // output (%0)
"m"( *pw ): // input (%1)
"cc" // clobbers
);
}
inline int atomic_conditional_increment( int * pw )
{
// int rv = *pw;
// if( rv != 0 ) ++*pw;
// return rv;
int rv, tmp;
__asm__
(
"movl %0, %%eax\n\t"
"0:\n\t"
"test %%eax, %%eax\n\t"
"je 1f\n\t"
"movl %%eax, %2\n\t"
"incl %2\n\t"
"lock\n\t"
"cmpxchgl %2, %0\n\t"
"jne 0b\n\t"
"1:":
"=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
"m"( *pw ): // input (%3)
"cc" // clobbers
);
return rv;
}
class sp_counted_base
{
private:
sp_counted_base( sp_counted_base const & );
sp_counted_base & operator= ( sp_counted_base const & );
int use_count_; // #shared
int weak_count_; // #weak + (#shared != 0)
public:
sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
{
}
virtual ~sp_counted_base() // nothrow
{
}
// dispose() is called when use_count_ drops to zero, to release
// the resources managed by *this.
virtual void dispose() = 0; // nothrow
// destroy() is called when weak_count_ drops to zero.
virtual void destroy() // nothrow
{
delete this;
}
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
virtual void * get_untyped_deleter() = 0;
void add_ref_copy()
{
atomic_increment( &use_count_ );
}
bool add_ref_lock() // true on success
{
return atomic_conditional_increment( &use_count_ ) != 0;
}
void release() // nothrow
{
if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
{
dispose();
weak_release();
}
}
void weak_add_ref() // nothrow
{
atomic_increment( &weak_count_ );
}
void weak_release() // nothrow
{
if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
{
destroy();
}
}
long use_count() const // nothrow
{
return static_cast<int const volatile &>( use_count_ );
}
};