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[], 这给了程序员极大的自由度也给了我们极高的门槛,弄不好就得内存泄露 ...
随机推荐
- JVM垃圾收集器CMS和G1
CMS(Concurrent Mark Sweep)收集器是一种以获取 最短回收停顿时间 为目标的收集器.采用的是"标记-清除算法",整个过程分为4步 由于整个过程中,并发标记和并 ...
- Java获取配置文件中的属性
获取配置文件的属性值 example 目标配置文件jdbc.properties,现想要在java类里面调用opcl的url jdbc.url=jdbc:mysql://localhost:3306/ ...
- Spring Cloud Alibaba 新一代微服务解决方案
本篇是「跟我学 Spring Cloud Alibaba」系列的第一篇, 每期文章会在公众号「架构进化论」进行首发更新,欢迎关注. 1.Spring Cloud Alibaba 是什么 Spring ...
- 跟着文档学习gulp1.2创建任务(task)
导出任务 任务(task)可以分为公开(public)或私有(private)类型 公开任务从gulpfile中被导出(export),可以通过gulp命令直接调用 私有任务被设计为在内部使用,通常作 ...
- C#中使用Path、Directory、Split、Substring实现对文件路径和文件名的常用操作实例
场景 现在有一个文件路径 E:\\BTSData\\2019-11\\admin_20180918_1_1_2 需要获取最后的文件名admin_20180918_1_1_2 需要获取文件的上层目录20 ...
- 技术分享预告丨k3s在边缘计算中的应用实践
技术分享是在[Rancher官方微信技术交流群]里以图文直播+QA实时互动的方式,邀请国内已落地经验的公司或团队负责人分享生产落地的最佳实践.记得添加微信小助手(微信号:rancher2)入群,实时参 ...
- js 淡入淡出的tab选项卡
代码如下 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- Flask 教程 第十四章:Ajax
本文翻译自The Flask Mega-Tutorial Part XIV: Ajax 这是Flask Mega-Tutorial系列的第十四部分,我将使用Microsoft翻译服务和少许JavaSc ...
- Entity Framework Core Code First 项目实践
Entity Framework Core Code First 实践 任何一种技术的出现都是为了解决一系列特定的问题,只有了解了技术所要解决的关键问题,才能理解它的真正用途,之后,才能在实践中用好它 ...
- Ligg.WinOa-000: Windows运维自动化编程实战--前言
本开源项目Ligg.WinOa是一个基于Ligg.EasyWinApp的Windows运维自动化应用.通过Ligg.EasyWinForm生成2个功能界面:管理员工具箱和用户工具箱:通过Lig ...