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 ...
随机推荐
- Java并发编程(九)线程间协作(下)
上篇我们讲了使用wait()和notify()使线程间实现合作,这种方式很直接也很灵活,但是使用之前需要获取对象的锁,notify()调用的次数如果小于等待线程的数量就会导致有的线程会一直等待下去.这 ...
- js基础(闭包实例)
1,常用发送短信的闭包实现: function sms() { var count = 60; return { start: function() { if(count == 0) { count ...
- Tarjan算法初探(2):缩点
接上一节 Tarjan算法初探(1):Tarjan如何求有向图的强连通分量 Tarjan算法一个非常重要的应用就是 在一张题目性质在点上性质能够合并的普通有向图中将整个强连通分量视作一个点来把整张图变 ...
- ORACLE->SQL*Loader[20180712]
https://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_concepts.htm#g1013706 SQL*Loader将外部 ...
- Linux中的阻塞机制
我们知道在字符设备驱动中,应用层调用read.write等系统调用终会调到驱动中对应的接口. 可以当应用层调用read要去读硬件的数据时,硬件的数据未准备好,那我们该怎么做? 一种办法是直接返回并报错 ...
- [Golang学习笔记] 08 链表
链表(Linked list)是一种常见数据结构,但并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针. 由于不必须按顺序存储,链表在插入的时候可以达到O(1),比顺序表快得多,但是查 ...
- BFC与浮动
一.BFC的含义 BFC(block formatting contexts) 块级元素格式化上下文,它决定了块级元素如何对它的内容进行布局,以及与其它元素的关系和相互作用. 块级元素:父级(是一个块 ...
- node从搭建运行项目整体流程
1. 初始化配置基本信息: npm init (自定义配置) npm init -y (一切配置采用默认值) 在当前目录产生package.json文件,有一个dependencies用来记录该项目所 ...
- java基础之while循环练习(2)
实现猜数游戏,如果没有猜对随机数,则程序继续,猜对后停止程序. 方法思路: 1:要产生一个随机数,所以需要创建一个随机数对象 Random random=new Random(): 2: 调用随机数对 ...
- 20155206 《Java程序设计》实验三实验报告
20155206 <Java程序设计>实验三实验报告 实验内容 Java敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 提交一: 提交二: 提交三: 提交四: