c++ Dynamic Memory (part 2)
Don't use get to initialize or assign another smart pointer.
The code that use the return from get can not delete the pointer
Although the compiler will not complain, it is an error to build another smart pointer to the pointer returned by get
- shared_ptr<int> p(new int()); // reference count is 1
- int *q = p.get(); // ok; but do not delete its pointer
- {
- shared_ptr<int> (q);
- } // block ends. q is destroyed, and the memory to which q points is freed
- int f = *p; // undefined. The memory to which p points is freed.
Using Our Own Deletion Code
- void end_connection(connection *p)
- {
- disconnection(*p);
- }
- void f(destination &d)
- {
- connection c = conn(&d);
- shared_ptr<connection p(&c, end_connection);
- // use the connection
- // when f exists, even if by an exception, the connection resource will be properly closed
- }
For unique_ptr
Call release() breaks the connection between a unique_ptr and the object it had been managing. Ofter the pointer returned by release() is used to initialized or assign another pointer
- unique_ptr<int> p2(new int());
- p2.release(); // WRONG! P2 will not free the memory, and we have lose the pointer
- auto p = p2.release(); // OK, but we must remember to delete p
Backward compatibilities auto_ptr
Although auto_ptr is still part of the standard library, programs should use unique_ptr instead.
c++ Dynamic Memory (part 2)的更多相关文章
- (转) Dynamic memory
In the programs seen in previous chapters, all memory needs were determined before program executi ...
- 论文笔记:Learning Dynamic Memory Networks for Object Tracking
Learning Dynamic Memory Networks for Object Tracking ECCV 2018Updated on 2018-08-05 16:36:30 Paper: ...
- c++ Dynamic Memory (part 1)
1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ...
- 动态内存分配(Dynamic memory allocation)
下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题. 尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...
- 从五大结构体,带你掌握鸿蒙轻内核动态内存Dynamic Memory
摘要:本文带领大家一起剖析了鸿蒙轻内核的动态内存模块的源代码,包含动态内存的结构体.动态内存池初始化.动态内存申请.释放等. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列九 动态内存Dyna ...
- [Paper翻译]Scalable Lock-Free Dynamic Memory Allocation
原文: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.3870&rep=rep1&type=pdf Abstr ...
- C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)
1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过. 2.初始化 1 struct X { int i ; floa ...
- 基于神经网络的混合计算(DNC)-Hybrid computing using a NN with dynamic external memory
前言: DNC可以称为NTM的进一步发展,希望先看看这篇译文,关于NTM的译文:人工机器-NTM-Neutral Turing Machine 基于神经网络的混合计算 Hybrid computing ...
- (C/C++) Interview in English. - Memory Allocation/Deallocation.
Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...
随机推荐
- Oracle 执行计划的查看方式
访问数据的方法:一.访问表的方法:1.全表扫描,2.ROWID扫描 二.访问索引的方法:1.索引唯一性扫描,2.索引范围扫描,3.索引全扫 ...
- 基于 HTML5 Canvas 的 3D 渲染引擎构建机架式服务器
前言 今天找到了 HT 的官网里的 Demo 网站( http://www.hightopo.com/demos/index.html ),看的我眼花缭乱,目不暇接. 而且 HT 的用户手册,将例子和 ...
- JavaScript的迭代函数与迭代函数的实现
前言 如果对技术很自信,请直接看 实现的源码 如果想回顾一下基础,请按文章顺序阅读 说到迭代方法,最先想到的是什么?forEach还是map,迭代的方法ES5提供了5种方法 以下定义来自 Ja ...
- [转]Python爬虫html解析工具beautifulSoup在pycharm中安装及失败的解决办法
原文地址:https://www.cnblogs.com/yysbolg/p/9040649.html 刚开始学习一门技术最麻烦的问题就是搞定IDE环境,直接在PyCharm里安装BeautifulS ...
- C语言中const有什么用途
自己上网查的资料. 可以定义const常量,具有不可变性.例如:const int Max=100; Max++会产生错误; 便于进行类型检查,使编译器对处理内容有更多了解,消除了一些隐患.例如: v ...
- PTA基础编程题目集6-4求自定类型元素的平均 (函数题)
6-4 求自定类型元素的平均 (10 分) 本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType. 函数接口定义: ElementType Aver ...
- STM32F103C8T6、STM32F103ZET6工程模板
STM32F103C8T6工程模板,推荐使用以下最新版本 最终版 2018 7 16 https://pan.baidu.com/s/1lIdZ2awus_quVu332RvJ6Q https:// ...
- android studio 插件开发(自动生成框架代码插件)
android studio 插件开发 起因 去年公司开始上新项目,正好android在架构这方面的讨论也开始多了起来,于是mvp架构模型就进入我们技术选择方案里面,mvp有很多好处,但是有一个非常麻 ...
- 批量复制windows文件夹下所有文件名
第一步,打开文件夹 第二步,在该文件夹下新建一个txt文件,然后将“.txt”后缀名修改为“.bat” txt文件内容“DIR *.* /B >LIST.TXT” 第三步,双击“.bat”,直接 ...
- Titanic幸存预测分析(Kaggle)
分享一篇kaggle入门级案例,泰坦尼克号幸存遇难分析. 参考文章: 技术世界,原文链接 http://www.jasongj.com/ml/classification/ 案例分析内容: 通过训练集 ...