前言
本文参考:https://www.jianshu.com/p/e4919f1c3a28
普通指针的问题 和 什么是智能指针,请参考我之前写的博客:https://blog.csdn.net/QQ1402369668/article/details/104569772
C++98提供了第一种智能指针:auto_ptr
class Test
{
public:
Test(int a = 0 ) : m_a(a) { }
~Test( )
{
cout << "Calling destructor" << endl;
}
public: int m_a;
};
void main( )
{
std::auto_ptr<Test> p( new Test(5) );
cout << p->m_a << endl;
}
分析:栈变量p离开作用域时,它所托管的内存块也会被自动释放(delete )。
问题1:
//***************************************************************
class Test
{
public:
Test(int a = 0 ) : m_a(a)
{
}
~Test( )
{
cout<<"Calling destructor"<<endl;
}
public:
int m_a;
};
//***************************************************************
void Fun(auto_ptr<Test> p1 )
{
cout<<p1->m_a<<endl;
}
//***************************************************************
void main( )
{
std::auto_ptr<Test> p( new Test(5) );
Fun(p);
cout<<p->m_a<<endl; //error
}
分析:当Fun
调用时, p
把关联的内存块的所有权传给了auto_ptr p1
, p1
是p
的copy。
现在Fun
函数执行完了,p1
离开了作用域,所以p1
关联的内存块也就释放了。那么p
呢?p
什么都没了,这就是crash的原因了,下一行代码还试图访问p
,好像p
还拥有什么资源似的。
问题2
还有另外一个缺点。auto_ptr不能指向一组对象,就是说它不能和操作符new[]一起使用。
//***************************************************************
void main( )
{
std::auto_ptr<Test> p(new Test[5]);
}
上面的代码将产生一个运行时错误。因为当auto_ptr
离开作用域时,delete
被默认用来释放关联的内存空间。当auto_ptr
只指向一个对象时,这当然是没问题的,但是在上面的代码里,我们在堆里创建了一组对象,应该使用delete[]
来释放,而不是delete
.
问题3:
auto_ptr
不能和标准容器(vector,list,map…)一起使用。
C++11智能指针
因为auto_ptr
容易产生错误,所以它也将被废弃了。C++11
提供了一组新的智能指针,每一个都各有用武之地。
- shared_ptr
- unique_ptr
- weak_ptr