本文首发于个人博客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 properly delete[] .

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

5种智能指针指向数组的方法| 5 methods for c++ shared_ptr point to an array的更多相关文章

  1. c++ 中的8种智能指针[转]

    一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...

  2. 转载:STL四种智能指针

    转载至:https://blog.csdn.net/K346K346/article/details/81478223 STL一共给我们提供了四种智能指针: auto_ptr.unique_ptr.s ...

  3. Android智能指针SP WP使用方法介绍

    Android手机操作系统既然是开源的操作系统.那么在具体的文件夹中就会存放着各种相关功能的开源代码.我们在使用的时候可以根据这些源代码进行相应的修改就能轻松的完成我们所需的功能.在这里大家就一起来看 ...

  4. c++中的四种智能指针

    c++中的四种智能指针 写惯了python,golang再来写c++总觉得头大,很大一个原因就是他没有一个GC机制. 不过c++中提供了智能指针,也不是不能用,李姐万岁! auto_ptr, shar ...

  5. stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结

    stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...

  6. foreach() 中用指针指向数组元素,循环结束后最好销毁指针

    之前发过一次微博,今天又遇到这个问题,并且再次犯错,于是决定再加深一下. 就举php.net里的一个例子吧 $a = array('abe','ben','cam'); foreach ($a as ...

  7. 聊聊 C++ 中的几种智能指针 (下)

    一:背景 上一篇我们聊到了C++ 的 auto_ptr ,有朋友说已经在 C++ 17 中被弃用了,感谢朋友提醒,今天我们来聊一下 C++ 11 中引入的几个智能指针. unique_ptr shar ...

  8. 到底有多少种智能指针(smart pointer)

    最近Qt的blog总结了到底有多少种smart pointer, 下面是一个简要的介绍: 1.   QPointer :提供对指针的保护,当一个指针被删除以后,再使用不会造成野指针或者指针溢出.比如 ...

  9. 聊聊 C++ 中的几种智能指针 (上)

    一:背景 我们知道 C++ 是手工管理内存的分配和释放,对应的操作符就是 new/delete 和 new[] / delete[], 这给了程序员极大的自由度也给了我们极高的门槛,弄不好就得内存泄露 ...

随机推荐

  1. Unity各平台宏定义

    属性 方法 UNITY_EDITOR #define directive for calling Unity Editor scripts from your game code. UNITY_EDI ...

  2. Linux防火墙的相关资料

    1.查看防火墙状态 [root@localhost ~]# service iptables status 2.编辑/etc/sysconfig/iptables文件.我们实例中要打开8080端口和9 ...

  3. 2016/09/21 context.getConfiguration().get()

    查看api:http://hadoop.apache.org/docs/stable/api/ public String get(String name) Get the value of the ...

  4. 常见的 由于未调整服务器 ulimit 而引起的内存溢出问题

    原文内容来自于LZ(楼主)的印象笔记,如出现排版异常或图片丢失等问题,可查看当前链接:https://app.yinxiang.com/shard/s17/nl/19391737/e3bb62c9-9 ...

  5. 【AHOI 2013】差异

    Problem Description 给定一个长度为 \(n\) 的字符串 \(S\),令 \(T_i\) 表示它从第 \(i\) 个字符开始的后缀.求 \(\sum_{1\leqslant i&l ...

  6. Linux服务器部署.Net Core笔记:三、CentOS 7上安装.NetCore运行环境

    1.要开始安装 .NET,您需要注册 Microsoft 签名密钥并添加 Microsoft 产品提要.每台机器只需要做一次. 打开命令提示符并运行以下命令:sudo rpm -Uvh https:/ ...

  7. python获取淘宝登入cookies

    重点:去新浪微博登入接口登入 一.代码 # coding=utf-8 import requests from selenium.webdriver.common.by import By from ...

  8. AndroidStudio初识

    大家好,欢迎来到下码看花,伟大领袖毛爷爷曾经说过:“ ‘走马看花不如驻马看花,驻马看花不如下马看花.’我希望你们都要下马看花.”,比喻停下来,深入实际,认真调查研究,这就是咱们公众号名字的由来.与君初 ...

  9. 离线安装Mariadb

    CentOS7.4开发站系统和红旗Asianux-7.3离线安装Mariadb 安装 需要Root权限 # 解压离线rpm包 tar -xvf Mariadb5.5.56.tar cd Mariadb ...

  10. <计算机系统结构中的8个伟大思想>

    摘自<计算机组成与设计>戴维帕森 ——面向摩尔定律的设计 ——使用抽象简化设计 ——加速大概率事件 ——通过并行提高性能 ——通过流水线提高性能 ——存储器层次 ——通过冗余提高可靠性