zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    软件工程概论课堂测试一
    每日站立会议个人博客五
    每日站立会议个人博客四
    典型用户以及场景分析
    每日站立会议个人博客三
    每日站立会议个人博客二
    每日站立会议个人博客一
    软件工程概论第三周学习进度条
    软件工程概论第二周学习进度条
    四则运算1
  • 原文地址:https://www.cnblogs.com/yangwenhuan/p/10635102.html
Copyright © 2011-2022 走看看