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. DBA手记(学习) - LOGMNR 简单而强大的工具

    LOGMNR首先执行一个ddl(或dml)操作,以记录重做信息: SYS@ ora11g>conn scott/tigerConnected.SCOTT@ ora11g>alter sys ...

  2. 处理Model

    将Model对象归档解档 - CSDN博客 iOS archive(归档)的总结 - 苹果吧 - 博客园 IOS数据持久化之归档NSKeyedArchiver,iosnskeyedarchiver_I ...

  3. $(document).ready(function(){})与window.load

    $(document).ready(function(){ //to do something}) 是当文档全部加载完全的时候触发,包括img也加载完成但是相关的文件没有下载下来,能同时编写多个 wi ...

  4. dfs板子题-Hdu1283Vegetables

    题目描述毕业后,Vegetable在一家建筑公司找到了工作.他所在的城市将要进行整修,要求把所有空地修成公园. 市区是一个N*M的矩形,Vegetable拿到了该市的地图,现在判断共要修几处公园? 注 ...

  5. luogu11月月赛T3咕咕咕(组合数学)

    题目描述 小 F 是一个能鸽善鹉的同学,他经常把事情拖到最后一天才去做,导致他的某些日子总是非常匆忙. 比如,时间回溯到了 2018 年 11 月 3 日.小 F 望着自己的任务清单: 看 iG 夺冠 ...

  6. 实施erp的建议

    纺织行业实施ERP建议 (一)企业各层面应提高对ERP的认识 ERP项目的实施范围横跨企业的每一个部门,在实施过程中需要调动各个部门的资源,这首先需要企业领导者高度重视,从实施的各个环节给予支持:其次 ...

  7. Mysql only_full_group_by 引起的错误

    SQLSTATE[]: Syntax error or access violation: Expression # of SELECT list is not in GROUP BY clause ...

  8. django数据库迁移-15

    目录 1.迁移 1.生成迁移文件 2.执行迁移命令 添加测试数据 1.迁移 创建完模型类后,并没有真正的在数据库中创建了数据表,需要执行迁移命令,在数据表中创建数据表. 1.生成迁移文件 manage ...

  9. Python学习:20.Python网络编程(Socket)

    一.Socket介绍 我们知道两个进程如果需要进行通讯,最基本的一个前提是能够唯一标示一个进程.在本地进程通讯中可以使用PID来唯一标示一个进程,但PID只在本地唯一,网络中的两个进程PID冲突几率很 ...

  10. Eclipse怎么恢复默认界面

    Eclipse里面将界面恢复到默认状态: 1.选择Eclipse的工具栏里面的“窗口(Window)”,找到“复位透视图(Reset Perspective)”选项: 2.单击“复位透视图(Reset ...