【c++ primer plus 笔记】01-智能指针

动机

使用堆区内存时,往往需要new分配指针,使用结束后要用delete回收内存,如果忘记delete就会造成内存泄漏。这样的手动管理是一种负担,智能指针会自动回收内存。

c++ 11 中常用的3种智能指针:C++11 引入了 3 个智能指针类型:

  1. std::unique_ptr<T> :独占资源所有权的指针。
  2. std::shared_ptr<T> :共享资源所有权的指针。
  3. std::weak_ptr<T> :共享资源的观察者,需要和 std::shared_ptr 一起使用,不影响资源的生命周期。

可以看作指针的封装类,创建时将new得到的地址赋给智能指针,过期时(对象销毁)在析构函数中释放堆区内存。

示例

头文件:#include <memory>

  
#include <iostream>
#include <string>
#include <memory> // auto_ptr,unique_ptr,shared_ptr
#include <vector>
#include <algorithm> //for_each
#include <assert.h>
using namespace std;

unique_ptr<string> demo(const char*s);
unique_ptr<int> make_int(int n);
void show(unique_ptr<int> &p);//pass by reference

int main(){

    // auto_ptr

    {
        auto_ptr<double> p1(new double(2.0));
        /*
            auto_ptr<double> p2;
            p2 = p1; // pass compiler, run-time error

            // delete twice
            cout << " *p2=" << *p2 << " ,*p1=" << *p1 << "\n";
        */
    }

    // unique_ptr
    {  
        //assign by std::move
        unique_ptr<string> p1 = std::make_unique<string>("hello");
        unique_ptr<string> p2;
        p2 = std::move(p1);
        assert(p1 == nullptr);//p1 is null
        p1 = std::make_unique<string>("world");
        cout << *p1 << " , " << *p2 << "\n";

        //assign with temp unique_ptr
        unique_ptr<string> p3;
        p3 = demo("tmp ptr"); // tmp unique_ptr deconstruct
        cout<<*p3<<"\n";

        //point to array : equal to new[]
        unique_ptr<int[]> p2arr = std::make_unique<int[]>(20);
        for(int i=0;i<20;i++) p2arr[i] = i*i;
        for (int i = 0; i < 20; i++)
            cout<<p2arr[i]<<" ";
        cout << "\n";

        // used in std::vector
        int len = 10;
        vector<unique_ptr<int>> vp(len);
        for (int i = 0; i < len; i++)
            vp[i] = make_int(i);              // copy temporary unique_ptr
        vp.push_back(make_int(20));           // ok, temporary unique_ptr
        for_each(vp.begin(), vp.end(), show); // print
        cout << "\n";
    }

    // shared_ptr
    {

        shared_ptr<int> p1(new int(5));
        shared_ptr<int> p2;
        p2 = p1; //ok, reference count
        cout << *p1 << " , " << *p2 << "\n";

  
        shared_ptr<int> p3;
        p3=make_int(10);//ok, unique_ptr is a rvalue
        cout << *p3 << "\n";
        p1=make_int(20);
        cout << *p1 << " , " << *p2 << "\n";
    }

  

}

  

unique_ptr<string> demo(const char* s){
    unique_ptr<string> tmp(new string(s));
    return tmp;
}

unique_ptr<int> make_int(int n){
    return unique_ptr<int>(new int(n));
}

void show(unique_ptr<int> &p){
    cout<<*p<<" ";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值