部分参考地址https://blog.csdn.net/yanglingwell/article/details/56011576

auto_ptr是c++标准库里的智能指针,但是具有以下几个明显的缺陷,使用时要注意

1.就是所谓的控制权转移,下面是模拟代码

    auto_Ptr(auto_Ptr<T>&ap)
{
_ptr = new T; //先分配空间
_ptr = ap._ptr; //再资源转移
ap._ptr = NULL; //将原来的指针置空
}

在赋值运算符重载和拷贝构造函数中将资源转移,来的指针被赋空,要是再进行一些使用的语句那么程序会崩溃

2.不能管理数组

    ~auto_ptr() _NOEXCEPT
{ // destroy the object
delete _Myptr;
}

    

void reset(_Ty *_Ptr = 0)
{ // destroy designated object and store new pointer
if (_Ptr != _Myptr)
delete _Myptr;
_Myptr = _Ptr;
}

 

这是auto_ptr内部析构和重置的代码,如果是数组需要delete[],所以不能完全释放数组的内存

另外分析一下auto_ptr的几个源代码

当我执行下面这句代码时

std::auto_ptr<int>b(auto_ptr<int>(new(int)));

执行顺序如下

     explicit auto_ptr(_Ty *_Ptr = ) _THROW0()
: _Myptr(_Ptr)
{ // construct from object pointer
}
//这个应该起隐式转换的作用
template<class _Other>
operator auto_ptr_ref<_Other>() _THROW0()
{ // convert to compatible auto_ptr_ref
_Other *_Cvtptr = _Myptr; // test implicit conversion
auto_ptr_ref<_Other> _Ans(_Cvtptr);
_Myptr = ; // pass ownership to auto_ptr_ref
return (_Ans);
} auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
{ // construct by assuming pointer from _Right auto_ptr_ref
_Ty *_Ptr = _Right._Ref;
_Right._Ref = ; // release old
_Myptr = _Ptr; // reset this
} ~auto_ptr() _NOEXCEPT
{ // destroy the object
delete _Myptr;
}

对于auto_ptr_ref是什么东西,下面是一段解释(我是没太懂为什么会调用operator auto_ptr_ref<_Other>())

Q: what is auto_ptr_ref, what it achieves and how it achieves it ?

A: It is rather confusing. Basically, auto_ptr_ref exists because the auto_ptr copy constructor isn’t really a copy constructor in the standard sense of the word.

Copy constructors typically have a signature that looks like this:

X(const X &b); 
The auto_ptr copy constructor has a signature that looks like this:

X(X &b) 
This is because auto_ptr needs to modify the object being copied from in order to set its pointer to 0 to facilitate the ownership semantics of auto_ptr.

Sometimes, temporaries cannot match a copy constructor that doesn’t declare its argument const. This is where auto_ptr_ref comes in. The compiler won’t be able to call the non-const version of the copy constructor, but it can call the conversion operator. The conversion operator creates an auto_ptr_ref object that’s just sort of a temporary holder for the pointer. The auto_ptr constructor or operator = is called with the auto_ptr_ref argument.

If you notice, the conversion operator in auto_ptr that automatically converts to an auto_ptr_ref does a release on the source auto_ptr, just like the copy constructor does.

It’s kind of a weird little dance that happens behind the scenes because auto_ptr modifies the thing being copied from.

简单地总结: auto_ptr_ref 主要解决用右值来构造 auto_ptr 的情况。 因为, auto_ptr(auto_ptr& r) 构造函数只能以左值引用做参数。当右值来构造 auto_ptr_ref 的时候,实际上实现过程如下(这其实是移动语义的早期实现版本): 

scoped_ptr类似于auto_ptr但是弥补了它的部分缺陷,scoped_ptr的所有权更加严格,不能转让,一旦scoped_pstr获取了对象的管理权,你就无法再从它那里取回来。

代码实现如下:

template<class T>
class scoped_ptr{
private:
T *px;
scoped_ptr(scoped_ptr const &);
scoped_ptr & operator=(scoped_ptr const &);
public:
explicit scoped_ptr(T *p = 0);
~scoped_ptr(); void reset(T *p = 0); T & operator*()const;
T * operator->()const;
T * get()const; operator unspecified-bool-type()const;
void swap(scoped_ptr & b);
};

可见,scoped_str的构造函数接受一个类型为T*的指针p,创建出一个scoped_ptr对象,并在内部保存指针参数p。p必须是一个new表达式动态分配的结果,或者是一个空指针(0)。当scoped_ptr对象的生命周期结束时,析构函数~scoped_ptr()会使用delete操作自动销毁所保存的指针对象,从而正确的回收资源。

scoped_ptr同时把拷贝构造函数和赋值操作都声明为私有的,禁止对智能指针的复制操作,保证了被它管理的指针不能被转让所有权。

最后加一点

!智能指针都应避免的一点:

string vacation("I wandered lonely as a cloud.");
shared_ptr<string> pvac(&vacation); // No

pvac过期时,程序将把delete运算符用于非堆内存,这是错误的。

智能指针之auto_ptr和scoped_ptr的更多相关文章

  1. 【C++深入浅出】智能指针之auto_ptr学习

    起:  C++98标准加入auto_ptr,即智能指针,C++11加入shared_ptr和weak_ptr两种智能指针,先从auto_ptr的定义学习一下auto_ptr的用法. template& ...

  2. [3] 智能指针std::auto_ptr

    [1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...

  3. 智能指针之 auto_ptr

    C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理,该智能指针在C++11中已经被弃用,转而由unique_ptr替代, 那这次使用和实现,就具体讲一下auto_pt ...

  4. C++ 智能指针 std::auto_ptr 分析

    背景介绍: RAll机制 定义一个类来封装资源的分配和释放,在构造函数中完成资源的分配和初始化,在析构函数中完成资源的清理,从而保证资源的正确初始化和清理 ps:智能指针就是RAll机制的一种应用,智 ...

  5. 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结

    看这里: http://blog.csdn.net/lollipop_jin/article/details/8499530 shared_ptr可以多线程同时读,但是涉及到写,需要加锁. share ...

  6. auto_ptr,shared_ptr 智能指针的使用

    Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...

  7. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  8. C++ 智能指针Auto_PTR 分析

    C++的动态内存的分配与释放是个挺折磨人的事情,尤其异常分支复杂时(比如一堆try catch中,各catch里需要做delete 掉相关的堆上分配的内存),极有可能产生内存泄露的情况.C++中提供了 ...

  9. 32.智能指针auto_ptr

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

随机推荐

  1. win10中VirtualBox联网设置

    <分享>关于win10操作系统中VirtualBox无法桥接的解决方法 版权声明:本文为博主原创文章,未经博主允许不得转载. 升级win10,本来是一件很好的事,想好好体验一下新版本的感觉 ...

  2. XAML语法及标记扩展、附加属性、特定的字符和空白

    1.对象元素语法 使用开闭标签将对象定义一个XML元素,这种语法与其他标记语言如HTML的元素语法非常相似,在以左右尖括号保卫要设置的类或结构的类型名称.对象元素可以声明0个或多个属性,以一个或多个空 ...

  3. 跨平台 GUI可视化 网络调试工具

    mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...

  4. Ruby 踩坑 “Failed to build gem native extension”

    ruby新手,总是会出现这样那样的问题,这里先记录下,希望能解决你得问题. 首先是安装ruby 环境,楼主愚钝,在公司和自己的电脑上来来回回整了好几天,每次安装 gem 包的时候总是报错,错误信息大致 ...

  5. ASP.NET Visual Studio2010 发布Web网站问题详解

    今天研究了一下如何发布web网站,之前总是没耐心,遇到点问题就没心情搞了,今天总算有点耐心搞明白了.其实遇到的问题还是挺多的,网上也没有太全的解释,所以结合自己还有别人的方法进行一下总结. 环境:Wi ...

  6. JAVA中LinkedLockingQueue的简单使用

    1.相关知识的了解 阻塞队列:当队列为空时,去队列中取数据会被阻塞.当队列满时,往队列中放数据会被阻塞.   非阻塞队列:当队列为空时,去队列取数据会直接返回失败,队列满时,往队列中放数据会直接返回失 ...

  7. JFrame自适应大小

    pack();函数调用PreferedSize(); 所以对于组件要setPreferedSize();

  8. pyhton基础

    python是一种什么语言?python是一种动态解释性的强类型定义的语言(1)编程语言分类 编译型: 把源程序的每一条语句都编译成机器语言,并保存成二进制文件, 这样运行时计算机可以直接以机器语言来 ...

  9. 爬取地图列表并下载-node.js

    var fs = require('fs'); var request = require('request'); var cheerio = require('cheerio'); var url ...

  10. javaScript 面向对象开发实例

    javaScript 面向对象开发实例 这个是结合require的模块化开发,首先创建构造函数: //test.js 1 function Test(lists) { var config={ nam ...