shared_ptr源码剖析

shared_ptr的源码:

主要又下列这几个文件组成:main.cpp sp_config.h sp_counted_base.h sp_counted_impl.h 
shared_count.h shared_ptr.h

sp_config.h

#ifndef _SP_CONFIG_H
#define _SP_CONFIG_H

#define DISPLAY//配置:在构造和析构的时候是否打印对应的字符串

#endif

shared_ptr.h

#ifndef _SHREAD_PTR_H
#define _SHREAD_PTR_H

#include"shared_count.h"

template<class T>
class shared_ptr
{
public:
	shared_ptr():px(0), pn()
	{
#ifdef DISPLAY
		cout<<"Create shared_ptr object."<<endl;
#endif
	}
	shared_ptr(T *p) : px(p), pn(p)
	{
#ifdef DISPLAY
		cout<<"Create shared_ptr object."<<endl;
#endif
	}
	shared_ptr(const shared_ptr<T> &r) : px(r.px), pn(r.pn)
	{
		cout<<"Create shared_ptr object."<<endl;
	}
	shared_ptr<T>& operator=(const shared_ptr<T> &r)
	{
		if(this != &r)
		{
			px = r.px;
			pn = r.pn;
		}
		return *this;
	}
	~shared_ptr()
	{
#ifdef DISPLAY
		cout<<"Free shared_ptr object."<<endl;
#endif
	}
public:
	T& operator*()const
	{
		return *px;
	}
	T* operator->()const
	{
		return px;
	}
	T* get()const
	{return px;}
public:
	long use_count()const
	{
		return pn.use_count();
	}
private:
	T *px;
	shared_count pn;
};

#endif

shared_count.h

#ifndef _SHREAD_COUNT_H
#define _SHREAD_COUNT_H

#include"sp_counted_base.h"
#include"sp_counted_impl.h"

class shared_count
{
public:
	shared_count() : pi_(  0)
	{
#ifdef DISPLAY
		cout<<"Create shared_count object."<<endl;
#endif
	}
	template<class T>
	shared_count(T *p) : pi_(new sp_counted_impl<T>(p))
	{
#ifdef DISPLAY
		cout<<"Create shared_count object."<<endl;
#endif
	}
	shared_count(const shared_count &r) : pi_(r.pi_)
	{
		if(pi_ != 0)
			pi_ ->add_ref_copy();
	}
	shared_count & operator=(const shared_count &r)
	{
		if(this != &r)
		{
			pi_ =r.pi_;
			if(pi_ != 0)
				pi_ -> add_ref_copy();
		}
		return *this;
	}
	~shared_count()
	{
#ifdef DISPLAY
		cout<<"Free shared_count object."<<endl;
#endif
		if(pi_)
			pi_->release();
	}
public:
	long use_count()const
	{
		return pi_!=0 ? pi_->use_count() : 0;
	}
private:
	sp_counted_base * pi_; 
};

#endif

sp_counted_base.h

#ifndef _SP_COUNTED_BASE_H
#define _SP_COUNTED_BASE_H

#include"sp_config.h"


#include<iostream>
using namespace std;

class sp_counted_base
{
public:
	sp_counted_base() : use_count_(1)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_base object."<<endl;
#endif
	}
	virtual ~sp_counted_base()
	{
#ifdef DISPLAY
		cout<<"Free sp_counted_base object."<<endl;
#endif
	}
public:
	virtual void dispose() = 0;//要实现多态,所以才采用了父类的指针
public:
	long use_count()const
	{
		return use_count_;
	}
	void add_ref_copy()
	{
		++use_count_;
	}
	void release()
	{
		if(--use_count_ == 0)
		{
			dispose();
			delete this;
		}
	}
private:
	long use_count_;
};

#endif

sp_counted_impl.h

#ifndef _SP_COUNTED_IMPL_H
#define _SP_COUNTED_IMPL_H

#include"sp_counted_base.h"



template<class T>
class sp_counted_impl : public sp_counted_base
{
public:
	sp_counted_impl() : px_(0)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_impl object."<<endl;
#endif
	}
	sp_counted_impl(T *p) : px_(p)
	{
#ifdef DISPLAY
		cout<<"Create sp_counted_impl object."<<endl;
#endif
	}
	~sp_counted_impl() 
	{
#ifdef DISPLAY
		cout<<"Free sp_counted_impl object."<<endl;
#endif
		px_ = 0;
	}
public:
	void dispose()
	{
		delete px_;
	}

private:
	T *px_;
};

#endif

main.cpp

#include<iostream>
#include<vld.h>
#include"shared_ptr.h"
#include"sp_config.h"
//#include<string>
using namespace std;


class Test
{
public:
	void fun()
	{cout<<"Test::fun()."<<endl;}
};



void main()
{
	int *p = new int(10);
	/*
	要构造一个ps,需要先构造一个sp_counted_base,再构造一个sp_counted_impl,
	再构造一个shared_count
	*/
	shared_ptr<int> ps(p);
	
	cout<<"ps = "<<*ps<<endl;
	cout<<"ps use_count = "<<ps.use_count()<<endl;
	shared_ptr<int> ps1;
	ps1 = ps;
	cout<<"ps use_count = "<<ps.use_count()<<endl;
	shared_ptr<int>ps2 = ps;
	cout<<"ps use_count = "<<ps.use_count()<<endl;

}

下面的图可以帮助大家理解shared_ptr的内存模型。

  • 构造时候的内存模型:

shared_ptr构造

  1. 根据主函数中的代码,首先构造一个shared_ptr的对象ps,ps中有指向T类型的指针px和shared_count的对象pn
  2. 需要先构造pn,其构造函数将new sp_counted_impl< T > ( p ) 赋值给其成员pi_。pi_是指向sp_counted_base类型的指针
  3. 调用 sp_counted_impl的构造函数,其成员px_也是指向T类型的指针。
  4. 在构造sp_counted_impl对象的时候,需要先构造其父类sp_counted_base
  5. 在sp_counted_base中只有一个成员use_coun_(这就是最终用来计数的变量)

PS:父类的指针pi_指向了子类对象。

  • 拷贝或者赋值时候的逻辑

shared_ptr拷贝和赋值

  1. 拷贝的时候,让对象ps1的成员px指针指向ps的成员px所指的位置
  2. 拷贝构造ps1的pn对象,让ps1中的pn的pi_指针指向sp_counted_impl,并将use_count_++
  • 析构时候的逻辑:

shared_ptr析构
3. 析构的shared_ptr的 时候,首先会析构shared_count类型的对象pn
4. shared_count中的析构函数会调用 其成员pi_的release()函数将引用计数–。如果引用计数变成0的话,调用dispose()函数由于在此类中dispose函数定义的是虚方法,所以会调用子类的dispose()函数方法。
5. 子类的dispose()方法会先释放掉px_所指向的空间。
6. 最后在sp_count_base类中delete this,

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shared_ptr是C++11中引入的一种智能指针,用于管理动态分配的内存。它可以自动释放内存,避免了手动释放内存的繁琐过程,同时也避免了内存泄漏和悬空指针的问题。 shared_ptr源码实现主要包括两个部分:引用计数和内存释放。 引用计数是指在多个shared_ptr对象共享同一个指针时,通过计数器来记录指针被引用的次数。每当一个新的shared_ptr对象指向该指针时,计数器加1;当一个shared_ptr对象被销毁时,计数器减1。当计数器为0时,表示没有任何shared_ptr对象指向该指针,此时可以释放该指针所指向的内存。 内存释放是指在引用计数为0时,释放该指针所指向的内存。shared_ptr使用delete操作符来释放内存,因此需要保证该指针是通过new操作符分配的内存。 下面是shared_ptr的简化版源码实现: ```c++ template<typename T> class shared_ptr { public: shared_ptr(T* ptr = nullptr) : ptr_(ptr), count_(new int(1)) {} ~shared_ptr() { if (--(*count_) == 0) { delete ptr_; delete count_; } } shared_ptr(const shared_ptr& other) : ptr_(other.ptr_), count_(other.count_) { ++(*count_); } shared_ptr& operator=(const shared_ptr& other) { if (this != &other) { if (--(*count_) == 0) { delete ptr_; delete count_; } ptr_ = other.ptr_; count_ = other.count_; ++(*count_); } return *this; } private: T* ptr_; int* count_; }; ``` 在上面的代码中,ptr_表示指向动态分配内存的指针,count_表示引用计数。构造函数中,如果ptr_为空,则count_初始化为1;否则,count_初始化为新分配的int类型指针,其值为1。析构函数中,如果引用计数为0,则释放ptr_和count_所指向的内存。拷贝构造函数和赋值运算符重载函数中,需要先将原有的引用计数减1,然后再将新的引用计数加1。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值