boost源码剖析1----内存管理scoped_ptr

       内存管理一直是C++程序员最头疼的的问题,C++继承了C灵活又高效的指针,使用起来一不小心就会导致内存泄露,在boost之前,智能指针是一个不错的解决方案,不过boost提供了更强大的解决方案,包括,scoped_ptr,scoped_array,shared_ptr,shared_array,weak_ptr,instrusive_ptr,这一篇先重点介绍scoped_ptr.

scoped_ptr源代码如下:

#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
#define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED

//  (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
//  Copyright (c) 2001, 2002 Peter Dimov
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
//  http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
//

#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/detail/workaround.hpp>

#ifndef BOOST_NO_AUTO_PTR
# include <memory>          // for std::auto_ptr
#endif

namespace boost
{

// Debug hooks

#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)

void sp_scalar_constructor_hook(void * p);
void sp_scalar_destructor_hook(void * p);

#endif

//  scoped_ptr mimics a built-in pointer except that it guarantees deletion
//  of the object pointed to, either on destruction of the scoped_ptr or via
//  an explicit reset(). scoped_ptr is a simple solution for simple needs;
//  use shared_ptr or std::auto_ptr if your needs are more complex.

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
    {
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
        boost::sp_scalar_constructor_hook( px );
#endif
    }

#ifndef BOOST_NO_AUTO_PTR

    explicit scoped_ptr( std::auto_ptr<T> p ): px( p.release() ) // never throws
    {
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
        boost::sp_scalar_constructor_hook( px );
#endif
    }

#endif

    ~scoped_ptr() // never throws//析构
    {
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
        boost::sp_scalar_destructor_hook( px );
#endif
        boost::checked_delete( px );
    }

    void reset(T * p = 0) // never throws 调用了swap转移的指针的管理权
    {
        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 // never throws//返回原始指针
    {
        return px;
    }

// implicit conversion to "bool"
#include <boost/smart_ptr/detail/operator_bool.hpp>

    void swap(scoped_ptr & b) // never throws 用于实现上面的reset函数,交换两个指针
    {
        T * tmp = b.px;
        b.px = px;
        px = tmp;
    }
};

template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
{
    a.swap(b);
}

// get_pointer(p) is a generic way to say p.get()

template<class T> inline T * get_pointer(scoped_ptr<T> const & p)//与get类似,不过这个返回的是参数对象管理的指针
{
    return p.get();
}

} // namespace boost

#endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
       由上面的scoped_ptr的构造函数可知,scoped_ptr接受一个参数为T*的指针p,创建一个scoped_ptr对象,并在内存保存参数p,在scoped_ptr生命期结束以后,会自动调用~scoped_ptr()函数,保证资源及时回收。它实际上是封装了auto_ptr(见# include <memory>),但是与auto_ptr不同的是,他把拷贝构造函数和重载的赋值函数都声明为私有的,禁止对私有指针的赋值等操作,即保证了对当前指针的完全管理权。operator*()和operator->()是为了模仿原始指针的行为。get返回当前指针,值得注意的是,再用get取得这个指针以后,不能再进行delete操作,否则在scoped_ptr进行析构的时候,会对已经删除的指针在进行delete操作。其实只要理解了auto_ptr这个基本上和原来差不多。使用举例:

#include <boost\smart_ptr.hpp>
#include <string>
using namespace std;
using namespace boost;
int main()
{
	scoped_ptr<string> a(new string("abcde"));
	cout<<*a<<endl;    //输出abced
	cout<<a->append("fg");//输出abcdefg
	//a++; //错误,没有定义
	return 0;
}
      从这里也可以看出scoped_ptr的用法和auto_ptr几乎是一样的,唯一不同的是指针的管理权问题,scoped_ptr的指针管理权不能更换,auto_ptr可以,同时因为scoped_ptr不支持比较拷贝和赋值,所以不能用作容器的元素。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值