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的更多相关文章

  1. C++智能指针: auto_ptr, shared_ptr, unique_ptr, weak_ptr

    本文参考C++智能指针简单剖析 内存泄露 我们知道一个对象(变量)的生命周期结束的时候, 会自动释放掉其占用的内存(例如局部变量在包含它的第一个括号结束的时候自动释放掉内存) int main () ...

  2. C++11智能指针 share_ptr,unique_ptr,weak_ptr用法

    0x01  智能指针简介  所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...

  3. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  4. auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  5. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  6. 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr

    1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...

  7. C++智能指针 unique_ptr

    C++智能指针 unique_ptr unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现), 定义于 memory ...

  8. C++11新特性总结 (二)

    1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...

  9. c++基础 使用智能指针

    三个智能指针模板(auto_ptr.unique_ptr和shard_ptr)都定义了类似指针的对象(c++11已将auto_ptr摒弃),可以将new获得(直接或间接) 的地址赋给这种对象.当智能指 ...

随机推荐

  1. java泛型讲解

    原文: https://blog.csdn.net/briblue/article/details/76736356 泛型,一个孤独的守门者. 大家可能会有疑问,我为什么叫做泛型是一个守门者.这其实是 ...

  2. 2.5 C++类class和结构体struct区别

    参考:http://www.weixueyuan.net/view/6337.html 总结: 在C++中,struct类似于class,在其中既可以定义数据成员,又可以定义成员函数. 在C++中,s ...

  3. [PyImageSearch] Ubuntu16.04下针对OCR安装Tesseract

    今天的博文是安装和使用光学字符识别(OCR)的Tesseract库的两部分系列的第一部分. 本系列的第一部分将着重于在您的机器上安装和配置Tesseract,然后使用tesseract命令将OCR应用 ...

  4. android lombok 使用

    把get /set / toString/hash/equal等方法从源文件中简化掉直接编译到二进制文件中 地址https://github.com/rzwitserloot/lombok 一 安装l ...

  5. <Spark><Tuning and Debugging>

    Overview 这一部分我们主要讨论如果配置一个Spark application,如何tune and debug Spark workloads 配置对Spark应用性能调优很重要.我们有必要理 ...

  6. day 30 客户端获取cmd 命令的步骤

    import subprocessimport structimport jsonfrom socket import *server=socket(AF_INET,SOCK_STREAM)serve ...

  7. 虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件

    虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件 Win 10 vmware12 无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件. ...

  8. css , dl , dt , dd 的使用. calc

    dl .dt, dd 虽然很少使用, 但是 有时使用会有 更好的效果: 一: 展示图片: ------------------------- 代码: <!DOCTYPE html> < ...

  9. nodejs的express框架(request,response方法汇总)

    Response 对象 - response 对象表示 HTTP 响应,即在接收到请求时向客户端发送的 HTTP 响应数据.常见属性有: res.app:同req.app一样 res.append() ...

  10. Python之路,第七篇:Python入门与基础7

    python3 元组 (tuple) 元组是不可改变的序列, 同list 一样, 元组可以存放任意的值: 表示方法: 用小括号()括起来:  单个元素括起来后加逗号(,)区分单个对象还是元组: 创建空 ...