c++智能指针之shared_ptr

1.简介

​ shared_ptr实现共享式拥有概念。多个智能指针可以指向相同对象,该对象和其相关资源会在“最后一个引用被销毁”时候释放。从名字share就可以看出了资源可以被多个指针共享,它使用计数机制来表明资源被几个指针共享。可以通过成员函数use_count()来查看资源的所有者个数。除了可以通过new来构造,还可以通过传入auto_ptr, unique_ptr,weak_ptr来构造。当我们调用release()时,当前指针会释放资源所有权,计数减一。当计数等于0时,资源会被释放。

​ shared_ptr 是为了解决 auto_ptr 在对象所有权上的局限性(auto_ptr 是独占的), 在使用引用计数的机制上提供了可以共享所有权的智能指针。

2. 初始化shared_ptr对象的方法?

  1. 构造函数

1] 将已存在的指向动态内存的普通指针作为参数来构造

int* p = new int(33);
shared_ptr<int> api(p);

2] 直接构造智能指针

shared_ptr< int > api( new int( 33 ) );
  1. 拷贝构造

​ 利用已经存在的智能指针来构造新的智能指针

shared_ptr< string > pstr_auto( new string( "Brontosaurus" ) );
shared_ptr< string > pstr_auto2( pstr_auto ); //利用pstr_auto来构造pstr_auto2

​ 使用一个智能指针初始化另外一个只能指针会使计数器+1,只有当计数器为0的时候才会调用释放函数

  1. 构造函数

1] 将已存在的指向动态内存的普通指针作为参数来构造

int* p = new int(33);
shared_ptr<int> api(p);

2] 直接构造智能指针

shared_ptr< int > api( new int( 33 ) );

3. 两个shared_ptr对象拥有同一个对象(一块内存)

int* p = new int(0);
shared_ptr<int> ap1(p);
shared_ptr<int> ap2(p);

4. shared_ptr常用的成员函数

  1. get()

返回shared_ptr指向的那个对象的内存地址。如下例:

int* p = new int(33);
cout << "the adress of p: "<< p << endl;
shared_ptr<int> ap1(p);
cout << "the adress of ap1: " << &ap1 << endl;
cout << "the adress of the object which ap1 point to: " << ap1.get() << endl;

输出如下:

the adress of p: 00481E00

the adress of ap1: 0012FF68

the adress of the object which ap1 point to: 00481E00

第一行与第三行相同,都是int所在的那块内存的地址。第二行是ap1这个类对象本身所在内存的地址。

  1. reset()

放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少

shared_ptr< string > pstr_auto( new string( "Brontosaurus" ) );
pstr_auto.reset( new string( "Long -neck" ) );

注:reset(0)可以释放对象,销毁内存。

  1. release()

返回shared_ptr指向的那个对象的内存地址,并释放对这个对象的所有权。

用此函数初始化shared_ptr时可以避免两个shared_ptr对象拥有同一个对象的情况(与get函数相比)。

例子如下:

shared_ptr< string > pstr_auto( new string( "Brontosaurus" ) );
shared_ptr< string > pstr_auto2( pstr_auto.get() ); //这是两个shared_ptr拥有同一个对象
shared_ptr< string > pstr_auto2( pstr_auto.release() ); //release可以首先释放所有权

4)move()

​ 如果确实想执行类似赋值的操作,要安全的重用这种指针,可给它赋新值。C++有一个标准库函数std::move(),让你能够将一个shared_ptr赋给另一个。尽管转移所有权后 还是有可能出现原有指针调用(调用就崩溃)的情况。但是这个语法能强调你是在转移所有权,让你清晰的知道自己在做什么,从而不乱调用原有指针。

shared_ptr<string> ps1, ps2;
ps1 = shared_ptr<string>(new string("hello"));
ps2 = move(ps1);
ps1 = shared_ptr<string>(new string("alexia"));
cout << *ps2 << *ps1 << endl;

5)use_count

​ 返回引用计数的个数。

string *s1 = new string("s1");	
shared_ptr<string> ps1(s1);
shared_ptr<string> ps2;
ps2 = ps1;
cout << ps1.use_count()<<endl;	//2
cout<<ps2.use_count()<<endl;	//2

6)unique

​ 返回是否是独占所有权(use_count为1)

string *s1 = new string("s1");
shared_ptr<string> ps1(s1);
shared_ptr<string> ps2;
ps2 = ps1;
cout << ps1.use_count()<<endl;	//2
cout<<ps2.use_count()<<endl;	//2
cout << ps1.unique()<<endl;	//0

7)swap

​ 交换两个 shared_ptr 对象(即交换所拥有的对象)

string *s1 = new string("s1");
shared_ptr<string> ps1(s1);	
string *s3 = new string("s3");
shared_ptr<string> ps3(s3);
cout << (ps1.get()) << endl;	//033AEB48
cout << ps3.get() << endl;	//033B2C50
swap(ps1, ps3);	//交换所拥有的对象
cout << (ps1.get())<<endl;	//033B2C50
cout << ps3.get() << endl;	//033AEB48

5.shared_ptr的缺陷

​ share_ptr虽然已经很好用了,但是有一点share_ptr智能指针还是有内存泄露的情况,当两个对象相互使用一个shared_ptr成员变量指向对方,会造成循环引用,使引用计数失效,从而导致内存泄漏。

class B;	//声明
class A
{
public:
	shared_ptr<B> pb_;
	~A()
	{
		cout << "A delete\n";
	}
};

class B
{
public:
	shared_ptr<A> pa_;
	~B()
	{
		cout << "B delete\n";
	}
};

void fun()
{
	shared_ptr<B> pb(new B());
	shared_ptr<A> pa(new A());
	cout << pb.use_count() << endl;	//1
	cout << pa.use_count() << endl;	//1
	pb->pa_ = pa;
	pa->pb_ = pb;
	cout << pb.use_count() << endl;	//2
	cout << pa.use_count() << endl;	//2
}

int main()
{
	fun();
	return 0;
}

​ 可以看到fun函数中pa ,pb之间互相引用,两个资源的引用计数为2,当要跳出函数时,智能指针pa,pb析构时两个资源引用计数会减1,但是两者引用计数还是为1,导致跳出函数时资源没有被释放(A、B的析构函数没有被调用)运行结果没有输出析构函数的内容,造成内存泄露

6.完整测试代码

#include <iostream>
#include <memory> //auto_ptr 的头文件
#include <string>
using namespace std;

int main()
{

    {
        shared_ptr< string > pstr_auto(new string("Brontosaurus"));
        shared_ptr< string > pstr_auto2(pstr_auto); //利用pstr_auto来构造pstr_auto2
        cout << "pstr_auto" << pstr_auto.use_count()<<endl;
        cout << "pstr_auto2" << pstr_auto2.use_count() << endl;
    }

    string *s1 = new string("s1");

    shared_ptr<string> ps1(s1);
    shared_ptr<string> ps2;
    ps2 = ps1;

    cout << ps1.use_count() << endl;	//2
    cout << ps2.use_count() << endl;	//2
    cout << ps1.unique() << endl;	//0

    string *s3 = new string("s3");
    shared_ptr<string> ps3(s3);

    cout << (ps1.get()) << endl;	//033AEB48
    cout << ps3.get() << endl;	//033B2C50
    swap(ps1, ps3);	//交换所拥有的对象
    cout << (ps1.get()) << endl;	//033B2C50
    cout << ps3.get() << endl;	//033AEB48

    cout << ps1.use_count() << endl;	//1
    cout << ps2.use_count() << endl;	//2
    ps2 = ps1;
    cout << ps1.use_count() << endl;	//2
    cout << ps2.use_count() << endl;	//2
    ps1.reset();	//放弃ps1的拥有权,引用计数的减少
    cout << ps1.use_count() << endl;	//0
    cout << ps2.use_count() << endl;	//1
    system("pause");
}

运行结果

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值