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

  1. (转) Dynamic memory

      In the programs seen in previous chapters, all memory needs were determined before program executi ...

  2. 论文笔记:Learning Dynamic Memory Networks for Object Tracking

    Learning Dynamic Memory Networks for Object Tracking  ECCV 2018Updated on 2018-08-05 16:36:30 Paper: ...

  3. c++ Dynamic Memory (part 1)

    1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args ...

  4. 动态内存分配(Dynamic memory allocation)

    下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题.    尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...

  5. 从五大结构体,带你掌握鸿蒙轻内核动态内存Dynamic Memory

    摘要:本文带领大家一起剖析了鸿蒙轻内核的动态内存模块的源代码,包含动态内存的结构体.动态内存池初始化.动态内存申请.释放等. 本文分享自华为云社区<鸿蒙轻内核M核源码分析系列九 动态内存Dyna ...

  6. [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 ...

  7. C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)

    1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过. 2.初始化 1 struct X { int i ; floa ...

  8. 基于神经网络的混合计算(DNC)-Hybrid computing using a NN with dynamic external memory

    前言: DNC可以称为NTM的进一步发展,希望先看看这篇译文,关于NTM的译文:人工机器-NTM-Neutral Turing Machine 基于神经网络的混合计算 Hybrid computing ...

  9. (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 ...

随机推荐

  1. debian系统,启动Wireshark,出现Couldn't run /usr/bin/dumpcap in child process:权限不够

    这是由于当前用户没有权限运行/usr/bin/dumpcap造成的./usr/bin/dumpcap是Wireshark的包捕获引擎. 先用ls命令看一下dumpcap的权限情况:xy@debian- ...

  2. 全局变量和局部变量(global关键字)

    1.定义在函数外部的就是全局变量,它的作用域从定义处一直到文件结尾. 2.函数内定义的变量就是局部变量,它的作用域为函数定义范围内. 3.函数之间存在作用域互不影响. 4.函数内访问全局变量需要 gl ...

  3. u-boot-1.1.6第1阶段分析之start.S、lowlevel_init.S文件

    学习目标: 对start.S中每一行代码,都有基本了解 通过对start.S文件分析,对ARM920T架构的CPU的启动过程,有更清楚理解 U-boot属于两个阶段的Bootloader,第一阶段的文 ...

  4. Linux 字符设备驱动—— ioremap() 函数解析

    一. ioremap() 函数基础概念 几乎每一种外设都是通过读写设备上的相关寄存器来进行的,通常包括控制寄存器.状态寄存器和数据寄存器三大类,外设的寄存器通常被连续地编址.根据CPU体系结构的不同, ...

  5. python自学之第一章——列表(一)

    1.列表是什么? 列表[list]:列表是由一系列特定顺序排列的元素组成. 列表由[]表示 eg: a = ['jackal','jax';'jack','jeef','jacky'] print(a ...

  6. 《Linux命令学习手册》系列分享专栏

    <Linux命令学习手册>系列分享专栏 <Linux命令学习手册>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/207 ...

  7. PHP中使用Redis接管文件存储Session详解

    前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用redis替换文件来存储session. 最近就遇到了这个问题,之前找了网上的一套直播 ...

  8. MFC 中的 Value 和 Control

    一.變量類型不同:control 型变量是这个控件所属类的一个实例(对象),控制對象的變量.即變量代表對象本身.代表這個人!value 是用来传递数据,不能对控件进行其它的操作.向變量傳遞數據.代表這 ...

  9. 常见的Content-Type类型

    Content-Type说明 MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型, 在Http协议消息头中,使用Content-Type来表示具体请求中 ...

  10. 机器人操作系统ROS-工作空间的建立

    运行例子为重德智能的github中的robot_sim_demo 创建一个robot_ws工作空间 1. mkdir -p robot_ws/src   #创建catkin 工作空间 2. cd ro ...