auto_ptr & share_ptr & unique_ptr
Using auto_ptr, you don’t need think about the memory deallocation, but there are also many arguments about it because of auto_ptr ownership translation.
void AutoPtrTest(void)
{
std::auto_ptr<int> p1 (new int());
//pass ownership
std::auto_ptr<int> p2 = p1;
std::cout << "p1 will raise exception:" << *p1 << std::endl;
std::cout << "p2 is ok: " << *p2 << std::endl;
}
In C++11, the auto_ptr is deprecated. The shared_ptr and unique_ptr are designed which have more security and comprehensive.
void AutoPtrTest(void)
{
std::auto_ptr<int> p1 (new int());
//pass ownership
std::auto_ptr<int> p2 = p1; //here has a warning in GCC
//std::cout << "p1 will raise exception:" << *p1 << std::endl;
std::cout << "p2 is ok: " << *p2 << std::endl; std::shared_ptr<int> s1 (new int());
std::shared_ptr<int> s2 = s1; //share it. point to same address
std::cout << "s1 is ok:" << *s1 << std::endl;
std::cout << "s2 is ok: " << *s2 << std::endl; std::unique_ptr<int> u1 (new int());
// std::unique_ptr<int> u2 = u1; //cannot be compiled
std::cout << "u1 is ok:" << *u1 << std::endl;
// std::cout << "s2 is ok: " << *s2 << std::endl;
}
In conclusion, auto_ptr could be replaced by shared_ptr or unique_ptr depending upon situation.
auto_ptr & share_ptr & unique_ptr的更多相关文章
- C++智能指针: auto_ptr, shared_ptr, unique_ptr, weak_ptr
		
本文参考C++智能指针简单剖析 内存泄露 我们知道一个对象(变量)的生命周期结束的时候, 会自动释放掉其占用的内存(例如局部变量在包含它的第一个括号结束的时候自动释放掉内存) int main () ...
 - C++11智能指针  share_ptr,unique_ptr,weak_ptr用法
		
0x01 智能指针简介 所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...
 - stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
		
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
 - auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
		
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
 - c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)
		
一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...
 - 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
		
1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...
 - C++智能指针 unique_ptr
		
C++智能指针 unique_ptr unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现), 定义于 memory ...
 - C++11新特性总结 (二)
		
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
 - c++基础 使用智能指针
		
三个智能指针模板(auto_ptr.unique_ptr和shard_ptr)都定义了类似指针的对象(c++11已将auto_ptr摒弃),可以将new获得(直接或间接) 的地址赋给这种对象.当智能指 ...
 
随机推荐
- :迭代器模式1:Iterator
			
//今天一口气把这一章前半部分的iterator例子的所有代码写完,涉及到了不少指针的内容,竟然一次性编译通过.... //Iterator与Menu之间应该不是has a的关系,先这样着吧. #if ...
 - ACM-ICPC 2018 徐州赛区网络预赛   G. Trace
			
There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy ...
 - 8.Python爬虫实战一之爬取糗事百科段子
			
大家好,前面入门已经说了那么多基础知识了,下面我们做几个实战项目来挑战一下吧.那么这次为大家带来,Python爬取糗事百科的小段子的例子. 首先,糗事百科大家都听说过吧?糗友们发的搞笑的段子一抓一大把 ...
 - MVC扩展之HtmlHelper辅助方法
			
1.什么是HtmlHelper辅助方法?其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class F ...
 - arduino使用oled显示时间MQ_2温湿度
			
这代码一般都是复制过来,在小改下就行了 代码如下: double Fahrenheit(double celsius) { ; } //摄氏温度度转化为华氏温度 double Kelvin(doubl ...
 - Linux:磁盘配额
			
磁盘配额 一.简略步骤显示 第一步:关闭虚拟机 第二步:编辑虚拟机设置--硬盘--添加 第三步:查看磁盘分区情况 fdisk -l 第四步:选择磁盘分区 fdisk /dev/sda2 第五步:选择磁 ...
 - Bug02_MyBatis_org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
			
我出现问题的原因是: 映射xml文件名写错了. 查资料,可以有以下解决办法 按以下步骤一一执行: 1:检查xml文件所在的package名称是否和interface对应的package名称一一对应 2 ...
 - https://stackoverflow.com/questions/40949967/running-storm-from-intellij-nimbus-error
			
https://stackoverflow.com/questions/40949967/running-storm-from-intellij-nimbus-error 0down votefavo ...
 - awr脚本使用dump导出导入
			
实际工作中,存在这么一种场景.客户现场分析问题,无法立即得出结论,且无法远程服务器,因此对于服务器中的awr信息,如何提取是一个问题,oracle有脚本可以对服务器中以db为单位导出awr基表的dum ...
 - 【linux基础】linux远程登录
			
可以用ssh命令行方式登录.对方需要开启ssh服务. 1. https://blog.csdn.net/zilaike/article/details/78922524 2. https://blog ...