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 ...
随机推荐
- jquery toggleclass方法
给元素更改样式,一般使用 addClass() 和removeClass() jquery官方文档 对 addClass的介绍: Adds the specified class(es) to eac ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 021-024
忙疯警告,这两天可能进度很慢,下午打了一下午训练赛,训练赛的题我就不拿过来的,pta就做了一点点,明天又是满课的一天,所以进度很慢啦~ -------------------------------- ...
- $.trim() 去除空格方法 (验证使用)
- react 使用antd 按需加载
使用 react-app-rewired 1. 安装react-app-rewired: 由于新的 react-app-rewired@2.x 版本的关系,你还需要安装 customize-cra. ...
- redis常用数据类型操作命令集锦
redis操作命令集锦 redis中五种数据类型 1) 字符串 String 特点: 存储所有的字符和字符串 应用场景: 做缓存使用 2) 哈希 hash 特点: 相当于java中hashMap集合 ...
- 我们比较常见的PHP实现openSug.js参数调试
这是一款利PHP对百度搜索下拉框提示免费代码实现参数配置调试的程序源代码. 由想要对网站进行搜索下拉调试的站长朋友们进行方便.快速的效果演示,具体参考下面的PHP代码. 如何使用? 请新建一份PHP文 ...
- ubuntu18 安装apache2.4 php5.6 mysql5.6
源码包下载: 链接:https://pan.baidu.com/s/1uoVMy_QNyR_zqVi06QPqKg 提取码:ll7s 注意: ubuntu安装:sudo apt-get inst ...
- 快速理解python2中的编码问题
# -*- coding:utf-8 -*- ''' python2 中的字符编码有str和unicode(字符串类型的名字) str类型字符串类型在内存中存储的是bytes数据 Unicode类型字 ...
- Flex copy and paste
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx ...
- 20155210潘滢昊 Java第二次试验
20155210潘滢昊 Java第二次试验 实验内容 学会JunitTest的使用 实验代码 MyUtilTest代码: import org.junit.*; import junit.framew ...