当前位置:   article > 正文

C++ 使用智能指针shared_ptr/unique_ptr管理数组_智能指针数组 c++

智能指针数组 c++


关于shared_ptr/unique_ptr的基础,我不在本篇博客中赘述。本篇博客主要关注如何安全地使用智能指针来管数组。

零、要管理的类

Connection是一个管理连接的类。

#include <iostream>
#include <memory>
#include <string>
#include <vector>

class Connection{
public:
    Connection():_name(""){}
    Connection(string name):_name(name){
    }
    string get_name() const {
        return _name;
    }
    ~Connection(){
        cout << string("关闭")+get_name()+"管理的连接中..." << endl;
        //关闭连接的代码
        // .....
        cout << "关闭完成。" << endl;
    }
private:
    string _name;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

一、使用shared_ptr管理数组

给出一个例子:

int main(){
    Connection* c1 = new Connection[2]{string("s1"), string("s2")};
    // 新建管理连接Connection的智能指针
    shared_ptr<Connection> sp(c1);
}
  • 1
  • 2
  • 3
  • 4
  • 5

运行输出:

关闭s1管理的连接中...
munmap_chunk(): invalid pointer
关闭完成。

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
  • 1
  • 2
  • 3
  • 4
  • 5

可以发现,只有s1被正确释放了,s2没有被成功释放。因为shared_ptr默认是使用delete来释放管理的资源,delete只会调用第一个元素的析构函数,所以只有s1被正确析构了(delete[ ] 会依次调用数组元素的析构函数)。要使用shared_ptr来管理数组,就需要需要自定义删除器(参考链接)。

int main(){
    auto Deleter=[](Connection *connection){
        delete[] connection;
    };
    Connection* c1 = new Connection[2]{string("s1"), string("s2")};
    // 新建管理连接Connection的智能指针
    shared_ptr<Connection> sp(c1, Deleter);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

二、使用unique_ptr管理数组

1、第一种方式

unique_ptr可以像shared_ptr一样使用删除器来释放管理的数组。

int main(){
    auto Deleter=[](Connection *connection){
        delete[] connection;
    };
    Connection* c1 = new Connection[2]{string("c1"), string("c2")};
    // 新建管理连接Connection的智能指针
    unique_ptr<Connection, decltype(Deleter)> up(c1, Deleter);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、第二种方式

unique_ptr重载了管理数组的版本,使用方法如下:

int main(){
    Connection* c1 = new Connection[2]{string("c1"), string("c2")};
    // 新建管理连接Connection的智能指针
    unique_ptr<Connection[]> up(c1);
}
  • 1
  • 2
  • 3
  • 4
  • 5

模板参数“Connection”后面还有“[]”。 unique_ptr就会自动使用delete[]来释放c1。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/781603
推荐阅读
相关标签
  

闽ICP备14008679号