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基础需要掌握的内容
一.Java的基本程序设计结构 二.对象与类 三.继承 四.接口.lambda表达式与内部类 五.异常,断言与日志 六.泛型程序设计 七.集合 八.并发(线程) 九.输入与输出(IO流) 十.网络 十 ...
- 复习宝典之Mysql数据库
查看更多宝典,请点击<金三银四,你的专属面试宝典> 第一章:mysql数据库 1)mysql与mariaDb MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用 ...
- python正则中如何匹配汉字以及encode(‘utf-8’)和decode(‘utf-8’)的互转
正则表达式: [\u2E80-\u9FFF]+$ 匹配所有东亚区的语言 [\u4E00-\u9FFF]+$ 匹配简体和繁体 [\u4E00-\u9FA5]+$ 匹配简体 <input ty ...
- Docker学习系列(一)-CentOS7下安装Docker
CentOS7下Docker的安装 一.操作系统要求 CentOS 7 64位 Kernel 3.10+ 本机系统信息 二.卸载旧版本 如果之前安排过旧版本的Docker,先卸载掉旧版Docker以及 ...
- PhpStorm中实现代码自动换行
方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...
- php 计算两个日期相差天数
<?php $startdate=strtotime("2013-3-09"); $enddate=strtotime("2013-4-05"); $da ...
- HBase操作一
package Hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.a ...
- 每天一个Linux命令之less
之前一下子看过好多Linux命令,当初记得但是一直没有使用就忘了,现在仿这别人写一下争取能记得时间久一点233333 我使用的是ubuntu Less 这是一个查看文件的命令 进行翻页的命令有一下几个 ...
- python教程(四)·序列
距离上次的小项目已经休息了很长一段时间,是时候来继续本系列教程了.这一节开始我们将深入python中的数据结构. 序列的概念 在python中,最基本的数据结构是序列,序列包含一个或多个元素,每个元素 ...
- HDFS要点
namenode存储的数据: 主控服务器主要有三类数据:文件系统的目录结构数据,各个文件的分块信息,数据块的位置信息(就数据块放置在哪些数据服务器上...).在GFS和HDFS的架构中,只有文件的目录 ...