auto_ptr, which can release the space automatically

C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理。

使用std::auto_ptr,要#include <memory>。[1]

中文名

自动指针

外文名

auto_ptr

在C++中, auto_ptr是一个类,它用来实现对动态分配对象的自动释放。

它的源代码:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

template<class T>

class auto_ptr

{

private:

    T*ap;

public:

    //constructor & destructor-----------------------------------(1)

    explicit auto_ptr(T*ptr=0)throw():ap(ptr)

    {

    }

    ~auto_ptr()throw()

    {

        delete ap;

    }

    //Copy & assignment--------------------------------------------(2)

    auto_ptr(auto_ptr& rhs)throw():ap(rhs.release())

    {

    }

    template<class Y>

    auto_ptr(auto_ptr<Y>&rhs)throw():ap(rhs.release())

    {

    }

    auto_ptr& operator=(auto_ptr&rhs)throw()

    {

        reset(rhs.release());

        return*this;

    }

    template<class Y>

    auto_ptr& operator=(auto_ptr<Y>&rhs)throw()

    {

        reset(rhs.release());

        return*this;

    }

    //Dereference----------------------------------------------------(3)

    T& operator*()const throw()

    {

        return*ap;

    }

    T* operator->()const throw()

    {

        returnap;

    }

    //Helper functions------------------------------------------------(4)

    //value access

    T* get()const throw()

    {

        returnap;

    }

    //release owner ship

    T* release()throw()

    {

        T*tmp(ap);

        ap=0;

        return tmp;

    }

    //reset value

    void reset(T*ptr=0)throw()

    {

        if(ap!=ptr)

        {

            deleteap;

            ap=ptr;

        }

    }

    //Special conversions-----------------------------------------------(5)

    template<class Y>

    struct auto_ptr_ref

    {

        Y*yp;

        auto_ptr_ref(Y*rhs):yp(rhs){}

    };

    auto_ptr(auto_ptr_ref<T>rhs)throw():ap(rhs.yp)

    {

    }

    

    auto_ptr& operator=(auto_ptr_ref<T>rhs)throw()

    {

        reset(rhs.yp);

        return*this;

    }

    

    template<class Y>

    operator auto_ptr_ref<Y>()throw()

    {

        returnauto_ptr_ref<Y>(release());

    }

    

    template<class Y>

    operator auto_ptr<Y>()throw()

    {

        returnauto_ptr<Y>(release());

    }

};

1 构造函数析构函数

auto_ptr在构造时获取对某个对象的所有权(ownership),在析构时释放该对象。我们可以这样使用auto_ptr来提高代码安全性:

?


1

2

int*p=new int(0);

auto_ptr<int> ap(p);

从此我们不必关心应该何时释放p,也不用担心发生异常会有内存泄漏。

这里我们有几点要注意:

1) 因为auto_ptr析构的时候肯定会删除他所拥有的那个对象,所以我们就要注意了,一个萝卜一个坑,两个auto_ptr不能同时拥有同一个对象。像这样:

?


1

2

3

int*p=new int(0);

auto_ptr<int>ap1(p);

auto_ptr<int>ap2(p);

因为ap1与ap2都认为指针p是归它管的,在析构时都试图删除p, 两次删除同一个对象的行为在C++标准中是未定义的。所以我们必须防止这样使用auto_ptr.

2) 考虑下面这种用法:

?


1

2

int*pa=new int[10];

auto_ptr<int>ap(pa);

因为auto_ptr的析构函数中删除指针用的是delete,而不是delete [],所以我们不应该用auto_ptr来管理一个数组指针。

3) 构造函数的explicit关键词有效阻止从一个“裸”指针隐式转换成auto_ptr类型。

4) 因为C++保证删除一个空指针是安全的, 所以我们没有必要把析构函数写成:

?


1

2

3

4

~auto_ptr()throw()

{

    if(ap)delete ap;

}

2 拷贝构造与赋值

引用计数智能指针不同的,auto_ptr要求其对“裸”指针的完全占有性。也就是说一个“裸”指针不能同时被两个以上的auto_ptr所拥有。那么,在拷贝构造或赋值操作时,我们必须作特殊的处理来保证这个特性。auto_ptr的做法是“所有权转移”,即拷贝或赋值的源对象将失去对“裸”指针的所有权,所以,与一般拷贝构造函数赋值函数不同, auto_ptr的拷贝构造函数,赋值函数的参数为引用而不是常引用(const reference).当然,一个auto_ptr也不能同时拥有两个以上的“裸”指针,所以,拷贝或赋值的目标对象将先释放其原来所拥有的对象。

这里的注意点是:

1) 因为一个auto_ptr被拷贝或被赋值后, 其已经失去对原对象的所有权,这个时候,对这个auto_ptr的提领(dereference)操作是不安全的。如下:

?


1

2

3

4

int*p=new int(0);

auto_ptr<int>ap1(p);

auto_ptr<int>ap2=ap1;

cout<<*ap1;//错误,此时ap1只剩一个null指针在手了

这种情况较为隐蔽的情形出现在将auto_ptr作为函数参数按值传递,因为在函数调用过程中在函数的作用域中会产生一个局部对象来接收传入的auto_ptr(拷贝构造),这样,传入的实参auto_ptr就失去了其对原对象的所有权,而该对象会在函数退出时被局部auto_ptr删除。如下:

?


1

2

3

4

5

6

7

8

void f(auto_ptr<int>ap)

{

    cout<<*ap;

}

auto_ptr<int>ap1(new int(0));

f(ap1);

cout<<*ap1;//错误,经过f(ap1)函数调用,ap1已经不再拥有任何对象了。

因为这种情况太隐蔽,太容易出错了, 所以auto_ptr作为函数参数按值传递是一定要避免的。或许大家会想到用auto_ptr的指针或引用作为函数参数或许可以,但是仔细想想,我们并不知道在函数中对传入的auto_ptr做了什么, 如果当中某些操作使其失去了对对象的所有权, 那么这还是可能会导致致命的执行期错误。 也许,用const reference的形式来传递auto_ptr会是一个不错的选择。

2)我们可以看到拷贝构造函数与赋值函数都提供了一个成员模板在不覆盖“正统”版本的情况下实现auto_ptr的隐式转换。如我们有以下两个类

?


1

2

class base{};

class derived:public base{};

那么下列代码就可以通过,实现从auto_ptr<derived>到auto_ptr<base>的隐式转换,因为derived*可以转换成base*类型

?


1

auto_ptr<base>apbase=auto_ptr<derived>(new derived);

3) 因为auto_ptr不具有值语义(value semantic), 所以auto_ptr不能被用在stl标准容器中。

所谓值语义,是指符合以下条件的类型(假设有类A):

?


1

2

3

4

5

6

A a1;

A a2(a1);

A a3;

a3=a1;

//那么

a2==a1,a3==a1

很明显,auto_ptr不符合上述条件,而我们知道stl标准容器要用到大量的拷贝赋值操作,并且假设其操作的类型必须符合以上条件。

3 提领操作(dereference)

提领操作有两个操作, 一个是返回其所拥有的对象的引用, 另一个则是实现了通过auto_ptr调用其所拥有的对象的成员。如:

?


1

2

3

4

5

6

7

8

struct A

{

    void f();

}

auto_ptr<A>apa(new A);

(*apa).f();

apa->f();

当然, 我们首先要确保这个智能指针确实拥有某个对象,否则,这个操作的行为即对空指针的提领是未定义的。

4 辅助函数

1) get用来显式的返回auto_ptr所拥有的对象指针。我们可以发现,标准库提供的auto_ptr既不提供从“裸”指针到auto_ptr的隐式转换(构造函数为explicit),也不提供从auto_ptr到“裸”指针的隐式转换,从使用上来讲可能不那么的灵活, 考虑到其所带来的安全性还是值得的。

2) release,用来转移所有权

3) reset,用来接收所有权,如果接收所有权的auto_ptr如果已经拥有某对象,必须先释放该对象。

5 特殊转换

这里提供一个辅助类auto_ptr_ref来做特殊的转换,按照标准的解释, 这个类及下面4个函数的作用是:使我们得以拷贝和赋值non-const auto_ptrs, 却不能拷贝和赋值const auto_ptrs. 我无法非常准确的理解这两句话的意义,但根据我们观察与试验,应该可以这样去理解:没有这些代码,我们本来就可以拷贝和赋值non-const的auto_ptr和禁止拷贝和赋值const的auto_ptr的功能, 只是无法拷贝和赋值临时的auto_ptr(右值), 而这些辅助代码提供某些转换,使我们可以拷贝和赋值临时的auto_ptr,但并没有使const的auto_ptr也能被拷贝和赋值。如下:

?


1

auto_ptr<int>ap1=auto_ptr<int>(new int(0));

auto_ptr<int>(new int(0))是一个临时对象,同时是一个右值,一般的拷贝构造函数当然能拷贝右值,因为其参数类别必须为一个const reference, 但是我们知道,auto_ptr的拷贝函数其参数类型为reference,所以,为了使这行代码能通过,我们引入auto_ptr_ref来实现从右值向左值的转换。其过程为:

1) ap1要通过拷贝 auto_ptr<int>(new int(0))来构造自己

2) auto_ptr<int>(new int(0))作为右值与现有的两个拷贝构造函数参数类型都无法匹配,也无法转换成该种参数类型

3) 发现辅助的拷贝构造函数auto_ptr(auto_ptr_ref<T> rhs) throw()

4) 试图将auto_ptr<int>(new int(0))转换成auto_ptr_ref<T>

5) 发现类型转换函数operator auto_ptr_ref<Y>() throw(),转换成功。

6)调用auto_ptr(auto_ptr_ref<T>rhs)throw()完成auto_ptr_ref向auto_ptr的构造。

从而通过一个间接类成功的实现了拷贝构造右值(临时对象)

同时,这个辅助方法不会使const auto_ptr被拷贝, 原因是在第5步, 此类型转换函数为non-const的,我们知道,const对象是无法调用non-const成员的, 所以转换失败。当然, 这里有一个问题要注意, 假设你把这些辅助转换的代码注释掉,该行代码还是可能成功编译,这是为什么呢?debug一下, 我们可以发现只调用了一次构造函数,而拷贝构造函数并没有被调用,原因在于编译器代码优化掉了。这种类型优化叫做returned value optimization,它可以有效防止一些无意义的临时对象的构造。当然,前提是你的编译器要支持returned value optimization。

时间: 2024-08-24 09:09:24

auto_ptr, which can release the space automatically的相关文章

My understanding about smart pointer

Here are two simple questions. Problem A #include <string> include <iostream> using namespace std; class vehicle { public: vehicle(const string& name); virtual ~vehicle(){} void PrintOwnerInfo(); private: string driver_name_; }; vehicle::v

ORA-15028: ASM file &#39;..&#39; not dropped; currently being accessed --转载

Couple of weeks ago we had a problem with one of our busiest databases. The FRA was filling quite rapidly and we just could not free enough space. What made this problem interesting was the fact that Oracle was telling us more than 40% of the space w

iPhone:4.7 5.5 4 3.5 对应的各个设备屏幕尺寸对应的像素及App上线信息

Shared App Information You can access these properties from the App Details page in the App Information section. This information is shared across any platform added to the app. Property Description Editable Primary Language The language you use to e

Haproxy Configure File

---------------------- HAProxy Configuration Manual ---------------------- version 1.5.11 willy tarreau 2015/02/01 This document covers the configuration language as implemented in the versionspecified above. It does not provide any hint, example or

EBS Archiving and Purging: You Know you need to

A number of trends in the IT industry have contributed to the increasing size of ERP application databases and show no signs of abating.  These include dramatic reductions over recent years in the cost of disk, such that it has often been easier to b

Rollback Segment Configuration &amp; Tips (Doc ID 69464.1)

Rollback Segment Configuration & Tips (Doc ID 69464.1) To Bottom ROLLBACK SEGMENT CONFIGURATION & TIPS ====================================== Good rollback segment configuration is crucial to a well tuned Oracle database. The following should help

智能指针_auto_ptr2_学习笔记

//1,release函数只是简单的转移对内存的拥有权,自己变成null,不会delete,一般在将所有权转移给别的智能指针的时候使用.具体可参加源码. 例: #include <memory> #include <iostream> using namespace std; class A { public:  A(int a):m_na(a)  {      cout<<"A cont" <<endl;  }  virtual ~A(

数据库的硬盘空间使用

SQL Server占用的存储空间,包含数据库file占用的存储空间,数据库对象占用的存储空间. 一,数据库file占用的存储空间 1,使用 sys.master_files 查看数据库中各个file占用的存储空间 select db.name as database_name,     db.is_auto_shrink_on,     db.recovery_model_desc,     mf.file_id,     mf.type_desc,     mf.name as logic_

allocation Unit 理解

Hobt_id和Partition_id这两个字段的值是相同的,每一个分区都有一个ID值,这个值是Partition_id.在每一个partition中,最多有三个allocation unit,type分别是 in_row, Lob, row_overflow.每一个allocation unit 都含有很多data page,存储data和index. Allocation Unit 可以理解为partition的单一data pages的集合. Each parition can have