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[], 这给了程序员极大的自由度也给了我们极高的门槛,弄不好就得内存泄露 ...
随机推荐
- hexo + next 搭建博客时Cannot GET /tags/问题处理
原来是要修改新建的index.md文件,不仔细. 此外,愈发觉得百度和谷歌搜索同一问题的差距,谷歌更适合程序员! https://www.zhihu.com/question/29017171 这个可 ...
- 清晰明了的javascript版动态规划
算法是一种艺术,给人感觉很不好接近,但是一旦你和ta熟络了,你就能发现这门艺术的内在是多么美妙且多变. 对于前端来说,算法也许不是最重要的,在日常工作中,几乎很少用到.所以很多人也不是很感冒. 不过呢 ...
- Elasticsearch 监控指标解析
1.集群监控 集群监控主要包括两个方面的内容,分别是集群健康情况和集群的运行状态. 集群健康状态可以通过以下api获取: http://ip:9200/_cluster/health?pretty 关 ...
- ubuntu14.04编译gnu global 6.6.3
打算重新折腾下环境,看中了gtags ,可参考 Vim 8 中 C/C++ 符号索引:GTags 篇 ,先记录下编译过程 源码 下载并解压源码 最新的代码到官方下载页面获取 https://www.g ...
- DevExpress的GridControl的使用以及怎样添加列和绑定数据源
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- 计算属性computed
computed 在Vue中有多种方法为视图设置值: 1.使用指令直接将数据值绑定到视图 2.使用简单的表达式对内容进行简单的转换 3.使用过滤器对内容进行简单的转换 除此之外,我们还可以使用计算属性 ...
- 14集超详细视频教程,手把手教你用数据神器Hawk!
沙漠君在闭关4个月后,终于把开源数据神器Hawk(详细介绍在这里)的文档和教学视频基本录制完毕, 并同步更新在有爱无广告二次元的B站! 教程总时间超过3个小时,覆盖了网页采集器,数据清洗,文件读写等方 ...
- LinqDB 查询数据库
LinqDB数据库查询数据,还是很方便的. 1. 添加Entity数据实体类 方便之后映射操作 /// <summary> /// 课件 /// </summary> [Dat ...
- Docker Compose 基本使用
Dockercompose v3官网文档: https://docs.docker.com/compose/compose-file/ Dockercompose中文: http://www.d ...
- CentOS 安装Asp.net Core & FTP服务
网络设置 确认是否成功连网: ping baidu.com 如果无法上网请检查以下设置 ip link show vim /etc/sysconfig/network-scripts/ipcfg-(看 ...