5种智能指针指向数组的方法| 5 methods for c++ shared_ptr point to an array
本文首发于个人博客https://kezunlin.me/post/b82753fc/,欢迎阅读最新内容!
5 methods for c++ shared_ptr point to an array
Guide
shared_ptr
Prior to C++17, shared_ptr could not be used to manage dynamically allocated arrays. By default, shared_ptr will call delete on the managed object when no more references remain to it. However, when you allocate using new[] you need to call delete[], and not delete, to free the resource.
In order to correctly use shared_ptr with an array, you must supply a custom deleter.
code example
//OK, pointer to int 999
std::shared_ptr<int> sp(new int(999));
template< typename T >
struct array_deleter
{
void operator ()( T const * p)
{
delete[] p;
}
};
// pointer to int array,
// (1) provide array deleter
std::shared_ptr<int> sp(new int[10], array_deleter<int>());
// (2) or lambda expression
std::shared_ptr<int> sp(new int[10], [](int *p) { delete[] p; });
// (3) or use default_delete
std::shared_ptr<int> sp(new int[10], std::default_delete<int[]>());
// (4) or we can use unique_ptr
std::unique_ptr<int[]> up(new int[10]); // this will correctly call delete[]
// (5) or we use vector<int>, no need to provide deleter
typedef std::vector<int> int_array_t;
std::shared_ptr<int_array_t> sp(new int_array_t(10));
std::memcpy(sp.get()->data(), arr, size);
std::unique_ptr<int[]>has built-in support for arrays to properlydelete[].
image buffer
std::shared_ptr<uchar> pImage(new uchar[length], std::default_delete<uchar[]>());
memcpy(pImage.get(), (void*)(data.sync_image().data().c_str()), length);
cv::Mat image = cv::Mat(height, width, CV_8UC3, pImage.get());
Reference
History
- 20191012: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/b82753fc/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
5种智能指针指向数组的方法| 5 methods for c++ shared_ptr point to an array的更多相关文章
- c++ 中的8种智能指针[转]
一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...
- 转载:STL四种智能指针
转载至:https://blog.csdn.net/K346K346/article/details/81478223 STL一共给我们提供了四种智能指针: auto_ptr.unique_ptr.s ...
- Android智能指针SP WP使用方法介绍
Android手机操作系统既然是开源的操作系统.那么在具体的文件夹中就会存放着各种相关功能的开源代码.我们在使用的时候可以根据这些源代码进行相应的修改就能轻松的完成我们所需的功能.在这里大家就一起来看 ...
- c++中的四种智能指针
c++中的四种智能指针 写惯了python,golang再来写c++总觉得头大,很大一个原因就是他没有一个GC机制. 不过c++中提供了智能指针,也不是不能用,李姐万岁! auto_ptr, shar ...
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- foreach() 中用指针指向数组元素,循环结束后最好销毁指针
之前发过一次微博,今天又遇到这个问题,并且再次犯错,于是决定再加深一下. 就举php.net里的一个例子吧 $a = array('abe','ben','cam'); foreach ($a as ...
- 聊聊 C++ 中的几种智能指针 (下)
一:背景 上一篇我们聊到了C++ 的 auto_ptr ,有朋友说已经在 C++ 17 中被弃用了,感谢朋友提醒,今天我们来聊一下 C++ 11 中引入的几个智能指针. unique_ptr shar ...
- 到底有多少种智能指针(smart pointer)
最近Qt的blog总结了到底有多少种smart pointer, 下面是一个简要的介绍: 1. QPointer :提供对指针的保护,当一个指针被删除以后,再使用不会造成野指针或者指针溢出.比如 ...
- 聊聊 C++ 中的几种智能指针 (上)
一:背景 我们知道 C++ 是手工管理内存的分配和释放,对应的操作符就是 new/delete 和 new[] / delete[], 这给了程序员极大的自由度也给了我们极高的门槛,弄不好就得内存泄露 ...
随机推荐
- 深入理解Linux的I/O复用之epoll机制
0.概述 通过本篇文章将了解到以下内容: I/O复用的定义和产生背景 Linux系统的I/O复用工具演进 epoll设计的基本构成 epoll高性能的底层实现 epoll的ET模式和LT模式 epol ...
- python学习-def
# 函数# 实现了某一特定功能.# 可以重复使用. # len() 功能:获取长度.# input() 功能: 控制台输入# print() 功能:输出 # 语法 关键字def"" ...
- 在 VSCode 中 Angular 的字符串报错的问题
使用 Angular 时,报错 [tslint] " should be ' 报错原因是因为 ESLint 的严格模式,限制了使用 ' ,甚至多一个空格都会报错. 我们只需要在 settin ...
- java基础-控制流程语句
一 前言 周末睡觉好舒服,都不想动了,就想睡睡,晒晒太阳,作者劳碌命还是过来写文章了.基础系列文章已经已经出到控制流程,感觉也挺快的,我很自信全网没都多少系列文章能有我这基础系列写的这么好,易于初学者 ...
- 《Java知识应用》Java加密方式(Base64)详解
1. 说明 Base64加密方式:比较简单,加密快,对普通大众可以起到加密的作用.在程序员眼中和透明一样. Base64应用场景:图片转码(应用于邮件,img标签,http加密) 2. 案例 impo ...
- javascript对url进行编码和解码
这里总结下JavaScript对URL进行编码和解码的三个方法. 为什么要对URL进行编码和解码 只有[0-9[a-Z] $ - _ . + ! * ' ( ) ,]以及某些保留字,才能不经过编码直接 ...
- 重新精读《Java 编程思想》系列之类的访问权限
Java 中,我们用访问权限修饰词确定库中的哪些类对于使用者是可以使用的. 访问权限修饰词有 public,protected,private 和什么都不写. 那么对于类来说,我们只可以用 publi ...
- Winform中实现仿XP系统的任务栏菜单效果(附代码下载)
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个Fo ...
- 多个 .NET 框架
目录 应用程序编程接口 C# 和 .NET 版本控制 .NET Standard 目前存在多个 .NET 框架. Microsoft 的宗旨是在最大范围的操作系统和硬件平台上提供 .NET 实现. 下 ...
- 阿里云ECS部署Redis主备哨兵集群遇到的问题
一.部署 详细部署步骤:https://blog.csdn.net/lihongtai/article/details/82826809 Redis5.0版本需要注意的参数配置:https://www ...