C++ 智能指针-原理和简单实现

C++的内存管理是个难题,容易出现内存泄漏、内存越界、指针非空内存已删除等种种问题,这些问题一旦出现,轻则程序占用内存越来越大,重则直接崩溃

1.在动态内存管理中,如果new上一块空间,但是没有delete,就会产生内存泄露的问题。 
2.但是有时候,我们new了,也delete了,但是还会出现问题。例如在new和delete之间调用了某个抛异常的函数,就有可能导致没有执行delete。

3.因此,只要有一种方法,能够在出了作用域之后能够自动释放掉申请的空间,就会让空间得到正确的释放。而一个对象出了作用域会自动调用自己的析构函数,只要在析构函数里能够释放自己开辟的空间,就能达到目的。 
4.智能指针就有上面的作用,能够自动的管理指针所指向的动态资源的释放。它不仅有着RAII的思想还能够像指针一样。(RAII:分配资源即初始化,即构造函数分配资源和初始化资源,在析构函数清理资源。像指针一样:能够解引用)。 
5.智能指针实质上就是一个模板类,成员变量包含一个任意类型的指针,构造函数获得资源并初始化,析构函数清理资源。 
注意:智能指针只能管理动态开辟的空间。


1.auto_ptr具有RAII和像指针一样的特点

模拟实现1:

// threadtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
// 智能指针的简单实现1
#include <iostream>
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <mutex>

using namespace std;

template<class T>
class Auto_ptr
{
public:
	Auto_ptr(T *ptr) :
		m_ptr(ptr)
	{}

	~Auto_ptr()
	{
		delete m_ptr;
	}

	// 像指针一样
	T& operator*()
	{
		return *m_ptr;
	}
	T* operator->()
	{
		return m_ptr;
	}

protected:
	T* m_ptr;
};

int main()
{
	int *pData = new int(10);
	Auto_ptr<int> p(pData);
	*p = 20;
	cout << *p << endl;
	return 0;
}

    但是如果只写上面的代码就会有一个问题,当我们想将一个AutoPtr赋值(或拷贝构造)给另一个AutoPtr,我们没写它的拷贝构造和赋值运算符重载,就会调用编译器的,这是浅拷贝,就会导致空间被多次释放。 
    auto_ptr思想是:管理权的转移。也就是当发现有一个auto_ptr对象a1赋值给另一个对象a2或者拷贝构造a2,那么a1获得的资源就交给a2,a2来管理a1指向的那块空间,a1就指向NULL,这样就保证了每块空间只有一个auto_ptr对象指向它,就不会有空间被释放多次的问题。
模拟实现2:修复上述问题

// threadtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
// 智能指针的简单实现1
#include <iostream>
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <mutex>

using namespace std;

template<class T>
class Auto_ptr
{
public:
	Auto_ptr(T *ptr) :
		m_ptr(ptr)
	{}

	~Auto_ptr()
	{
		delete m_ptr;
	}
	// copy构造函数,管理权转移
	Auto_ptr(Auto_ptr<T>& srcP) :
		m_ptr(srcP.m_ptr)
	{
		srcP.m_ptr = nullptr;
	}
	Auto_ptr<T>& operator=(Auto_ptr<T>& srcP)
	{
		if (this != srcP)
		{
			// 先释放自己的
			delete m_ptr;
			m_ptr = srcP.m_ptr;
			srcP.m_ptr = nullptr;
		}
		return *this;
	}
	// 像指针一样
	T& operator*()
	{
		return *m_ptr;
	}
	T* operator->()
	{
		return m_ptr;
	}

protected:
	T* m_ptr;
};

int main()
{
	int *pData = new int(10);
	Auto_ptr<int> p(pData);
	*p = 20;
	cout << *p << endl;
	Auto_ptr<int> p2(p);
	cout << *p2 << endl;
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值