c++--c++11标准下的三种智能指针

本文深入探讨C++中的智能指针概念,包括unique_ptr、shared_ptr和weak_ptr的使用方法及实例。智能指针能够自动管理堆上的内存,避免内存泄漏,特别适合于现代C++开发。

智能指针介绍

  • 在c++中有unique_ptr、shared_ptr、weak_ptr、auto_ptr四种智能指针,c++11标准中废除了auto_ptr(存在潜在的内存崩溃问题)。
  • 智能指针是一个在栈中的类,用于管理堆上分配的内存。
  • 传统c/c++对于堆上内存的开辟释放,需要程序手动管理,而智能指针是一个类,有构造函数和析构函数,在超出作用范围后,程序会自动调用析构函数释放其管理的指针指向的内存,不需要手动释放。

三种智能指针

  • unique_ptr是一种独占式指针,保证同一时间只有一个智能指针可以指向该对象,如果要安全重用该指针,标准库函数std::move()可以将unique_ptr赋值给另一个unique_ptr;
  • shared_ptr是一种共享式指针,多个智能指针可以指向同一个对象,对象的资源在最后一个指针销毁时释放。shared_ptr用计数机制表明对象有几个指针共享,用方法use_count()可以查看到所有者的个数,调用方法release()释放所有权,计数减1(计数为0时释放资源)。
  • weak_ptr是为了配合shared_ptr进行对象管理而产生的,只提供对对象的访问,它不会影响shared_ptr的计数发生变化,可以避免两个shared_ptr相互引用的死锁问题。weak_ptr只能用shared_ptr或另一个weak_ptr构造,通过lock()方法weak_ptr可以转化成shared_ptr。

实例练习

使用智能指针,先包含<memory>头文件

unique_ptr:
#include <iostream>
#include<memory>
using namespace std;

class Test{
public:
    string str;
    ~Test(){
        cout << "Test::~Test()"<<endl;//析构函数,测试是否释放资源
    }
};

int main()
{
    unique_ptr<Test> p1(new Test);
    p1->str = "test string";
    cout <<"p1->str:" << p1->str << endl;
    unique_ptr<Test> p2;
    //p2 = p1;//不可以这样使用
    p2 = move(p1);
    //cout << "p1->str:" << p1->str <<endl;//所有权移交给p2,此时访问不到资源
    cout << "p2->str:" << p2->str <<endl;
    return 0;
}
shared_ptr
#include <iostream>
#include<memory>
using namespace std;

int main()
{
    shared_ptr<string> p1(new string("hello world"));
    shared_ptr<string> p2;
    p2 = p1; //shared_ptr可以直接赋值,此时p1、p2都指向p1 new出来的内存
    cout << "*p2:" << *p2 << endl;//p2可以访问p1指向的内存
    cout << "use_count():" << p2.use_count() << endl;//获取到同时指向同一内存的指针指针数量
    p1.reset();//p1放弃所有权,此时p1不能访问那块内存
    cout << "use_count():" << p2.use_count() << endl;
    return 0;
}

weak_ptr

引入weak_ptr的原因:相互引用造成的死锁

#include <iostream>
#include<memory>
using namespace std;

class B;
class A{
public:
    shared_ptr<B> a;
    ~A(){
        cout << "A::~A()" << endl;
    }
};


class B{
public:
    shared_ptr<A> b;
    ~B(){
        cout << "B::~B()" << endl;
    }
};
/*******前边两个类中相互引用,此时引入weak_ptr解决死锁问题*/

int main()
{
    shared_ptr<A> p1(new A());
    shared_ptr<B> p2(new B());
    p1->a = p2;
    p2->b = p1;//这种情况发生死锁,两个shared_ptr计数都不会为0,资源不会被释放
    cout << "use_count():" << p1.use_count() << endl;
    cout << "use_count():" << p2.use_count() << endl;
    return 0;
}

此时两个资源都得不到释放。

通过weak_ptr解决死锁问题:

#include <iostream>
#include<memory>
using namespace std;

class B;
class A{
public:
    shared_ptr<B> a;
    ~A(){
        cout << "A::~A()" << endl;
    }
};


class B{
public:
    string str = "hello";
    shared_ptr<A> b;
    ~B(){
        cout << "B::~B()" << endl;
    }
};
/*******前边两个类中相互引用,此时引入weak_ptr解决死锁问题*/

int main()
{
    shared_ptr<A> p1(new A());
    shared_ptr<B> p2(new B());
    weak_ptr<B> p3;
    p3 = p2;
    p1->a = p2;
    cout << "use_count():" << p1.use_count() << endl;
    cout << "use_count():" << p2.use_count() << endl;
    shared_ptr<B> p4 = p3.lock();//weak_ptr不能直接访问,lock()方法可以将weak_ptr转化成shared_ptr
    cout << p4->str << endl;
    return 0;
}

此时可以正常释放资源,解决了死锁问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值