码迷,mamicode.com
首页 > 其他好文 > 详细

std::tr1::shared_ptr内存使用

时间:2017-05-09 20:38:56      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:pre   typename   weak   public   type   tom   point   struct   typedef   

shared_ptr定义如下:

template<typename _Tp>
class shared_ptr
{private:
    _Tp*         _M_ptr;         // contained pointer
    shared_count _M_refcount;    // reference counter
};

shared_count定义如下:

class shared_count
{
public:
    template<typename _Ptr, typename _Deleter> 
    shared_count(_Ptr __p, _Deleter __d) : _M_pi(0) 
    { 
        _M_pi = new _Sp_counted_base_impl<_Ptr, _Deleter>(__p, __d); 
    }
private:
    _Sp_counted_base* _M_pi;                                                                                                                                                  
};

_Sp_counted_base定义如下:

class _Sp_counted_base
{
private:
    _Atomic_word _M_use_count;        // #shared
    _Atomic_word _M_weak_count;       // #weak + (#shared != 0)
    __gnu_cxx::mutex_type _M_mutex;
};

template<typename _Ptr, typename _Deleter>
class _Sp_counted_base_impl : public _Sp_counted_base
{
private:
    _Ptr     _M_ptr; // copy constructor must not throw
    _Deleter _M_del; // copy constructor must not throw
};

sizeof(_Sp_counted_base) = 48,在_Sp_counted_base_Impl中,_Ptr是对象指针,而_M_del结构如下:

template<typename _Tp>
struct _Sp_deleter
{
    typedef void result_type;
    typedef _Tp* argument_type;

    void operator()(_Tp* p) const
    {
        delete p;
    }                                                                                                                                                                         
};

因此shared_ptr对象的体积为72:

 

std::tr1::shared_ptr内存使用

标签:pre   typename   weak   public   type   tom   point   struct   typedef   

原文地址:http://www.cnblogs.com/lvzhiqiang/p/6832306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!