C++中的auto_ptr智能指针的作用及使用方法详解
作者:2010120422 发布时间:2022-04-07 03:01:10
智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势,但也有其局限。本文总结的8个问题足以涵盖auto_ptr的大部分内容。
auto_ptr是什么?
auto_ptr 是C++标准库提供的类模板,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同时被分给两个拥有者。当auto_ptr对象生命周期结束时,其析构函数会将auto_ptr对象拥有的动态内存自动释放。即使发生异常,通过异常的栈展开过程也能将动态内存释放。auto_ptr不支持new 数组。
该类型在头文件memory中,在程序的开通通过 #include<memory> 导入,接下来讲解该智能指针的作用和使用。
使用方法:
auto_ptr<type> ptr(new type()); 这是该指针的定义形式,其中 type 是指针指向的类型,ptr 是该指针的名称。
比如该type 是int,具体定义如下:
auto_ptr<int> ptr(new int(4));
比如该type 是map<int,vector<int> >,具体定义如下:
auto_ptr<map<int,vector<int> > > ptr(new map<int,vector<int> > ());
当然可以先定义,后赋值,如下所示:
auto_ptr<map<int,int> > ptr;
ptr = auto_ptr<map<int,int> >(new map<int,int> ());
作用1:保证一个对象在某个时间只能被一个该种类型的智能指针所指向,就是通常所说的对象所有权。
作用2:对指向的对象自动释放的作用,详情看如下代码。
代码片段一:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; // 输出 创建次数
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}
在某些情况下,可能我们就会写出上面的代码来,通过运行会发现存在内存溢出。对于一些经验老道的程序员可能会作如下改写:
代码片段二:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl;
test_ptr * tmp = new test_ptr();
}
system("pause");
return 0;
}
在test_ptr 类中的析构函数中添加内存释放代码,但是在main函数中,定义的局部指针,当局部指针失效时并不会自动调用析构函数,在这种情况下也会导致内存泄漏问题。当然,如果细心的程序员可以在 test_ptr * tmp = new test_ptr() 后面加上一句 delete tmp ,这样也能够释放内存,不会出现内存泄漏问题。但是在某些情况下,很容易漏写,为了解决此问题,auto_ptr 就能发挥作用了。
代码片段三:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
map<int,int> *p;
test_ptr()
{
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
~test_ptr()
{
delete p;
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
在main函数中,创建test_ptr类型指针时,该指针是auto_ptr 类型的智能指针,当智能指针失效时,会自动调用该类的析构函数。所以这种写法可以不再显示调用delete 语句了。但是该智能指针也只是保证调用类的析构函数,如果析构函数并没有释放类中声明的变量,那该怎么办。
代码片段四:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
//auto_ptr<map<int,int> > p;
map<int,int> *p;
test_ptr()
{
//p = auto_ptr<map<int,int> > (new map<int,int>());
p = new map<int,int>();
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
/*
~test_ptr()
{
delete p;
}
*/
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
在这种情况下,还是会出现内存泄漏问题,为了解决该问题,对类中声明的指针也是需要声明为auto_ptr类型。
代码片段五:
#include <iostream>
#include <string.h>
#include <memory>
#include <string>
#include <Windows.h>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
#define MAXN 20000000
class test_ptr
{
public:
auto_ptr<map<int,int> > p;
test_ptr()
{
p = auto_ptr<map<int,int> > (new map<int,int>());
for(int i = 0;i<MAXN;i++)
p->insert(make_pair(i,i));
}
};
int main(int argc,char *argv[])
{
for(int i = 0;i<100;i++)
{
Sleep(1000);
cout << i << endl; //输出创建次数
auto_ptr<test_ptr> tmp = auto_ptr<test_ptr> (new test_ptr());
}
system("pause");
return 0;
}
这样就不用显示定义类的析构函数,不用在外部显示调用delete函数,当然如果尽早调用delete函数也是可以的,尽早释放内存也比该指针失效再释放好一些,这些就是为了防止忘记调用。
通过如上分析:可以得出如下结论。
1 定义了智能指针,当智能指针失效时会自动调用类的析构函数。
2 在 类中定义的智能指针,不必在析构函数中显示的delete,当外部调用该类的析构函数时,会自动释放该智能指针指向的对象,释放内存。
3 如果类中定义的是智能指针,但是外部没有触发类中的析构函数调用,该智能指针指向的对象还是不能释放。
auto_ptr 智能指针的bug
auto_ptr 智能指针在c++ 11 标准中已经被抛弃,被抛弃的原因就是因为该bug。前面也提到过,一个对象只能被一个智能指针所引用,这样就会导致一个赋值问题。看如下代码
代码片段六:
#include <iostream>
#include <string.h>
#include <memory>
#include <set>
using namespace std;
#define MAXN 20000000
void pri(auto_ptr<set<int> > p)
{
set<int>::iterator ite = p->begin();
for(;ite!=p->end();ite++)
{
cout << *ite << endl;
}
}
int main(int argc,char *argv[])
{
auto_ptr<set<int> > ptr(new set<int> ());
for(int i = 0;i< 3;i++)
{
int a;
cin >> a;
ptr->insert(a);
}
pri(ptr);
pri(ptr);
system("pause");
return 0;
}
初看这代码没什么问题,不过运行程序会崩溃。这就是该智能指针最大的bug, 在程序32行 调用pri(ptr) ,程序到这并没什么问题,但是第二次调用pri(ptr) 时程序就会崩溃。原因就是前面讲过,一个对象智能被一个智能指针所指向,在第一次调用pri()函数时,为了保证这一原则,当把ptr指针传入pri函数时,程序内部就把ptr置为空,所以到第二次调用时,就会出现崩溃的情况。对于这种情况的解决之道就是使用shared_ptr 指针(该指针的原理是通过引用计数器来实现的)。
如果要使用shared_ptr 智能指针,需要安 * oost库,该库还包括许多其他功能。有兴趣的可以尝试以下,该类中的智能指针还是比较好用。也不存在很多其他bug。
以上所述是小编给大家介绍的C++中的auto_ptr智能指针实例详解网站的支持!
来源:http://www.cnblogs.com/ChenAlong/archive/2016/07/12/5662851.html


猜你喜欢
- 在Android Studio中,你可以很快速的使用Parcelable插件进行实体类的序列化的实现,使用该插件后,你的实体类可以快速的实现
- /// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static
- 刚开始学习Service的时候以为它是一个线程的封装,也可以执行耗时操作。其实不然,Service是运行在主线程的。直接执行耗时操作是会阻塞
- 1. 自动化装配介绍Spring Boot针对mvc做了大量封装,简化开发者的使用,内部是如何管理资源配置,Bean配置,环境变量配置以及启
- 数据库事务是后端开发中不可缺少的一块知识点。Spring为了更好的支撑我们进行数据库操作,在框架中支持了两种事务管理的方式: 编程式事务声明
- Java float和double精度范围大小要想理解float和double的取值范围和计算精度,必须先了解小数是如何在计算机中存储的:举
- 背景产品想对多次快速点击做一下优化,想要的效果就是双击不会打开多次但是从开发角度来说,我可以用kotlin的拓展方法来调整这个,但是之前的历
- 一、垃圾回收机制创建对象就会占据内存,如果程序在执行过程中不能再使用某个对象,这个对象是徒耗内存的垃圾。作为程序员不用关心回收垃圾对象问题,
- 多个线程访问共享对象和数据的方式有两种情况:1、每个线程执行的代码相同,例如,卖票:多个窗口同时卖这100张票,这100张票需要多个线程共享
- 目录前言Maven依赖代码前言在工作中是否会遇到实用websocket客户端连接服务端的时候,网络波动,服务端断连的情况。会导致客户端被动断
- Scala小程序详解1. 交互式模式在命令行窗口中,输入Scala命令:xiaosi@Qunar:~$ scalaWelcome to S
- Java8被称作Java史上变化最大的一个版本。其中包含很多重要的新特性,最核心的就是增加了Lambda表达式和StreamAPI。这两者也
- 访问静态资源的配置及顺序今天在玩SpringBoot的demo的时候,放了张图片在resources目录下,启动区访问的时候,突然好奇是识别
- 经过几天的折腾,终于到了学习一个重量级的查询方式上,使用@Query注解,使用注解有两种方式,一种是JPQL的SQL语言方式,一种是原生SQ
- @Aspect@Order各个通知的执行顺序两个切面类:【记录日志】和【判断参数】,分别对应顺序 @Order(0) 和@Order(1)
- 前言我们大家平时长时间打代码的时候肯定会感到疲惫和乏味,这个时候一边播放自己喜欢的音乐,一边继续打代码,心情自然也愉快很多。音乐带给人的听觉
- 前言taptap-developer是一个spring boot框架驱动的纯Grpc服务,所以,只用了四步,移除了web和spring cl
- 1、检测权限因为dump系统lsass内存和sam注册表需要管理员权限,所以首先需要对当前进程上下文权限做判断。public static
- 本文实例讲述了Java+Ajax实现的用户名重复检验功能。分享给大家供大家参考,具体如下:今天,我来教大家怎么实现Java+Ajax实现用户
- 注:如果没有 root 权限也是可以试试,一般情况下,都需要 root 权限,才能连接成功。1.需要确保你的开发 PC 和 Android