首页 技术 正文
技术 2022年11月19日
0 收藏 520 点赞 2,842 浏览 2034 个字

1. auto_ptr 的设计动机:

函数操作经常依照下列模式进行:

  • 获取一些资源
  • 执行一些动作
  • 释放所获取的资源

那么面对这些资源的释放问题就会出现下面的两种情况:

  • 一开始获得的资源被绑定于局部对象,那么当函数退出的时候,这些局部对象的析构函数被自动的调用,从而自动释放掉这些资源;
  • 一开始获得的资源是通过某种显示手段获取,而且并没有绑定在任何对象身上,那么必须以显式的方式释放。这种情况常常发生在指针身上;

例子:

 void f()
{
ClassA* ptr = new ClassA();
...
delete ptr;
}

上述代码的真正的问题在于: 一旦在…中的某处发生异常,那么f()函数将立即退出,根本不会去调用函数尾端的delete语句。结果可能会造成内存的遗失,或者称为资源的遗失。

上述问题的一个解决方案是:

 void f()
{
ClassA* ptr = new ClassA(); try
{
...
}
catch(...)
{
delete ptr;
throw;
} delete ptr;
}

为了在异常发生时处理对象的删除工作,程序代码就会变得非常复杂和累赘!

为此,引入了智能指针的概念:

智能指针应该保证:无论在何种情形下,只要自己被销毁,就一定要连带释放其所指向的资源。由于智能指针本身就是区域变量,所以无论是正常推出,还是异常推出,它都一定被销毁,auto_ptr正是这种指针。

auto_ptr是这样的一种指针: 它是”其所指向的对象”的拥有者。所以,当身为对象拥有者的auto_ptr被销毁时,该对象也将被销毁。auto_ptr要求一个对象只能有一个拥有者,严禁一物二主。(天地之间物各有主,苟非吾之所有,虽一毫而莫取)。

下面我们通过一个实例来认识auto_ptr的使用:

 #include <iostream>
#include <string>
#include <memory> using namespace std; class Test
{
public:
Test(const string& psg);
string getMsg();
private:
string msg;
}; Test::Test(const string& psg)
{
msg = psg;
} string Test::getMsg()
{
return msg;
} int main(void)
{
std::auto_ptr<Test> ptr(new Test("This is the end of the world!"));
cout<<ptr->getMsg()<<endl;
return ;
}

初始化一个auto_ptr时,将一个指针作为参数传递给auto_ptr的构造函数。而不能使用赋值操作符。

2. auto_ptr拥有权的转移:
auto_ptr所界定的是一种严格的拥有权观念。也就是说,由于一个auto_ptr会删除其所指向的对象,所以这个对象绝对不能被其他对象同时”拥有”。绝对不可以出现多个

auto_ptrs拥有同一个对象。

那么auto_ptr的copy构造函数和assignment操作符该如何运作呢?

另auto_ptr的copy构造函数和assignment操作符将对象的拥有权交出去!

     // initialize an auto_ptr with a new object
std::auto_ptr<Test> ptr1(new Test("This is the end of the world!")); // copy the auto_ptr
// ~ transfers ownership from ptr1 to ptr2
std::auto_ptr<Test> ptr2(ptr1); cout<<ptr1->getMsg()<<endl;

上面对象的拥有权从ptr1交到ptr2的时候,再使用ptr1去getMsg的时候,会出现”段错误”提示。

 #include <iostream>
#include <string>
#include <memory> using namespace std; class Test
{
public:
Test(const string& psg,const int& id);
~Test();
string getMsg();
private:
string msg;
int id;
}; Test::Test(const string& psg,const int& pid)
{
msg = psg;
id = pid;
} Test::~Test()
{
cout<<"Delete "<<id<<" "<<endl;
} string Test::getMsg()
{
return msg;
} int main(void)
{
std::auto_ptr<Test> ptr1(new Test("This is ptr1",));
std::auto_ptr<Test> ptr2(new Test("This is ptr2",));
ptr2 = ptr1;
return ;
}

上面代码的结果是:

 Delete
Delete

表明: 当ptr2被赋值前正拥有另一个对象,赋值动作发生时会发生delete,将该对象删除。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920