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:
时间: 2024-10-29 19:07:39