• C/C++ C++11智能指针


    在使用基本指针类型时,因为要手动释放指针指向的内存,常常容易造成内存泄漏,特别是异常分支很多的情况下。而智能指针类型就是将基本指针类型封装成模板类,以便更好地管理内存。

    智能指针都包含一个explicit构造函数,因此基本指针类型不能隐式转换成智能指针,需要显式调用。

    shared_ptr<double> sp_d; 
    double *p_d = new double;
    sp_d = p_d; // 错误,隐式转换。
    sp_d = shared_ptr<double>(p_d); // 正确,显式转换。
    shared_ptr<double> sp_d = p_d; // 错误,隐式转换。
    shared_ptr<double> sp_d(p_d); // 正确,显式转换。

    1、auto_ptr

    1)由C++98提出,C++11中已经弃用。

    std::auto_ptr<int> sp_i1(new int);
    *sp_i1 = 10;
    std::auto_ptr<int> sp_i2 = sp_i1; // 所有权移交给sp_i2,sp_i1变成空指针
    std::cout << *sp_i1 << std::endl; // 运行崩溃

    2、unique_ptr

    std::unique_ptr<int> sp_i1(new int);
    *sp_i1 = 10;
    // 编译报错:Call to implicitly-deleted copy constructor of 'std::unique_ptr<int>'
    // 即unique_ptr不提供拷贝构造函数unique_ptr(const unique_ptr &) = delete;
    // 以及赋值运算符unique_ptr& operator=(const unique_ptr &) = delete;
    std::unique_ptr<int> sp_i2 = sp_i1; 
    std::cout << *sp_i1 << std::endl;
    // unique_ptr提供移动构造函数unique_ptr(unique_ptr &&) noexcept;
    // 以及移动赋值运算符unique_ptr& operator=(unique_ptr &&) noexcept; 因为右值之后不会再用到
    // 和auto_ptr都是所有权移交,但是unique_ptr更优。 std::unique_ptr<int> sp_i2 = std::unique_ptr<int>(new int);
    std::unique_ptr<int> sp_i1(new int);
    *sp_i1 = 10;
    std::unique_ptr<int> sp_i2 = std::move(sp_i1);
    std::cout << *sp_i2 << std::endl;
    std::cout << *sp_i1 << std::endl; // 运行崩溃,所有权已经移交给sp_i2,sp_i1变成空指针了,可以重新赋值

    3、shared_ptr

    shared_ptr使用引用计数技术来管理内存(正如Objective-C中的内存管理),引用计数为0,对象被析构。

    std::shared_ptr<int> sp_i1(new int(100)); // sp_i1指向int(100),int(100)的引用计数为1。
    std::shared_ptr<int> sp_i2 = sp_i1; // sp_i2和sp_i1同时指向int(100),int(100)的引用计数变为2。
    sp_i1.reset(new int(200)); // sp_i1指向int(200),int(100)的引用计数变为1。
    sp_i2.reset(); // int(100)的引用计数变为0,被析构。

    4、weak_ptr

    正如Objective-C会出现循环引用,导致内存无法释放,shared_ptr也有这个问题。weak_ptr可以解决这个问题,只会指向对象,对象的计数不会加1。

    参考链接:

    http://www.cplusplus.com/reference/memory/shared_ptr/

  • 相关阅读:
    关于串通京东接口的demo
    http链接中请求进行编码,Http请求api
    RabbitMQ 创建用户和创建Virtual host
    dedecms调用当前栏目的子栏目及子栏目文章
    dede调出所有栏目以及栏目下的二级栏目
    JS判断移动端访问设备并加载对应CSS样式
    border-style
    网页色阶表
    破解centos7 密码
    DEDECMS打开网站后台系统首页卡解决方法
  • 原文地址:https://www.cnblogs.com/yangwenhuan/p/10635102.html
Copyright © 2020-2023  润新知