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. JAVA中的一些内置方法

    Math 函数: Math.E //自然常数e Math.abs(12.3); //返回该值的绝对值 Math.ceil(12.3); //向上取整 Math.floor(12.3); //向下取整 ...

  2. 三、Shiro授权开发

    Shiro 支持三种方式的授权: I. 编程式:通过写if/else 授权代码块完成: Subject subject =SecurityUtils.getSubject(); if(subject. ...

  3. Java Service Wrapper 发布Java程序为Windows服务

    下载Windows版本:https://www.krenger.ch/blog/java-service-wrapper-3-5-37-for-windows-x64/ 转自:F:\java\bhGe ...

  4. Web的基本工作原理、HTTP协议和URL说明

    Web工作原理 客户端和Web服务器通过HTTP协议进行通信.Web服务器有是也叫HTTP服务器或Web容器.HTTP协议采用的是请求/响应模式.即客户端发起HTTP请求,web服务器接收并解析处理H ...

  5. C++练习 | 递归创建二叉树并求叶子结点的数值和

    #include <iostream> using namespace std; struct Tree { int data; Tree *lchild; Tree *rchild; } ...

  6. MySQL慢日志查询实践

    慢日志查询作用 慢日志查询的主要功能就是,记录sql语句中超过设定的时间阈值的查询语句.例如,一条查询sql语句,我们设置的阈值为1s,当这条查询语句的执行时间超过了1s,则将被写入到慢查询配置的日志 ...

  7. jquery ajax 滚动加载数据

    jquery php 滚动加载数据(文件包 rollingpage) 效果如下: 页面加载时候($function(){ 自动加载第一页数据 }) 设置: var winH = $(window).h ...

  8. 树莓派3B+学习笔记:9、更改软件源

    树莓派系统安装完成后,由于默认软件源服务器访问速度慢,安装软件耗时会很长,可以通过更改软件源来加快软件的安装速度. 系统安装完成后默认软件源如下: 更改镜像源前需要自行查找镜像源,并记下网址: 1.阿 ...

  9. mysql5.6升级为mysql5.7部署jboss/wildfly应用项目

    一.部署mysql5.7二进制版 解压tar -xvf mv mysql-5.7  /usr/local/mysql5.7  或者其他文件夹 cd  /usr/local/mysql.57 usera ...

  10. flink 根据时间消费kafka

    经常遇到这样的场景,13点-14点的时候flink程序发生了故障,或者集群崩溃,导致实时程序挂掉1小时,程序恢复的时候想把程序倒回13点或者更前,重新消费kafka中的数据. 下面的代码就是根据指定时 ...