CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

auto_ptr 为什么不释放拥有权

楼主huwenmin(玉米)2003-09-02 00:48:25 在 C/C++ / C语言 提问

我得代码如下template   <class   T>  
  ostream&   operator<<   (ostream&   strm,const   auto_ptr<T>&   p)  
  {  
  if(p.get()==NULL)  
  {  
  strm<<"NULL";  
  }  
  else  
  {  
  strm<<*p;  
  }  
   
  return   strm;  
  }  
   
  int   main()  
  {  
  auto_ptr<int>   p(new   int(9));  
  auto_ptr<int>   q;  
   
  cout<<"after   initialization:"<<endl;  
  cout<<"p:"<<p<<endl;  
  cout<<"q:"<<q<<endl;  
   
  q=p;  
   
  cout<<"after   assigning   auto   pointers:"<<endl;  
  cout<<"p:"<<p<<endl;  
  cout<<"q:"<<q<<endl;  
   
  *q+=4;  
   
  p=q;  
   
  cout<<"after   change   and   reassignment:"<<endl;  
  cout<<"p:"<<p<<endl;  
  cout<<"q:"<<q<<endl;  
  return   0;  
  }  
  打印结果:  
   
  after   initialization  
  p:9  
  q:NULL  
  after   assigning     autopointer  
  p:9  
  q:9  
  after   change   sna   resignment  
  p:13  
  q:13  
  我用的是VC6,C++标准程序库   里   说p赋值给q后应该为NULL,不知道是怎么回事 问题点数:100、回复次数:5Top

1 楼oopig(面向对象的猪)回复于 2003-09-02 01:04:10 得分 20

从代码里看的很清楚,没有释放q的指针,只是把q的一个表示所有权的标志_Owns给设成false了。  
  //operator=函数  
  auto_ptr<_Ty>&   operator=(const   auto_ptr<_Ty>&   _Y)   _THROW0()  
  {  
  if   (this   !=   &_Y)  
  {if   (_Ptr   !=   _Y.get())  
  {if   (_Owns)  
  delete   _Ptr;  
  _Owns   =   _Y._Owns;   }  
  else   if   (_Y._Owns)  
  _Owns   =   true;  
  _Ptr   =   _Y.release();   }  
  return   (*this);    
  }  
  //release函数  
  _Ty   *release()   const   _THROW0()  
  {  
  ((auto_ptr<_Ty>   *)this)->_Owns   =   false;  
  return   (_Ptr);    
  }  
  Top

2 楼oopig(面向对象的猪)回复于 2003-09-02 01:11:34 得分 20

以上是vc6下的代码。但是在dev-c++下的行为却不是这样的,q=p这样的赋值语句会把p的指针给清空。源代码如下:  
      auto_ptr&   operator=(auto_ptr&   __a)   throw()  
          {  
              reset(__a.release());  
              return   *this;  
          }  
      element_type*   release()   throw()  
          {  
              element_type*   __tmp   =   _M_ptr;  
              _M_ptr   =   0;  
              return   __tmp;  
          }  
   
  所以只能在具体环境下注意一下所用的stl了。  
  Top

3 楼jyfcsdn()回复于 2003-09-02 08:48:47 得分 20

我用的是STLPort提供的SGI   STL,   结果如下。  
  可能不同的STL的实现手法不同  
  after   initialization:  
  p:9  
  q:NULL  
  after   assigning   auto   pointers:  
  p:NULL  
  q:9  
  after   change   and   reassignment:  
  p:13  
  q:NULLTop

4 楼xiaoyunet(快乐的小猪)回复于 2003-09-02 09:33:49 得分 20

不错,楼主挺仔细的。  
  其实这个是VC中stl的bug,因为vc德stl比较早了,原来的stl中auto_ptr是由这个bug的,后来修正了,可是vc中的已经出了,这个bug就留下来的。具体的可以看more   excpetion   C++这本书中有讲。  
  所以,如果你使用vc6的话,就不要使用auto_ptr,否则,呵呵,死的很难看。当然你可以用stlport4.5.3的版本替换vc6自带的stl版本。就可以解决这个问题了。Top

5 楼titati(titati)回复于 2003-09-02 10:28:09 得分 20

Right  
   
  我在VC7里编译运行上述代码,结果如下:  
  after   initialization  
  p:9  
  q:NULL  
  after   assigning     autopointer  
  p:NULL  
  q:9  
  after   change   sna   resignment  
  p:13  
  q:NULL  
   
  看来在7里已经修改了这个bug了  
  在最新的MSDN里:  
  auto_ptr   ClassSee   Also  
  auto_ptr   Members   |   <memory>   Members   |   Thread   Safety   in   the   Standard   C++   Library  
  Requirements  
  Header:   <memory>  
  The   template   class   describes   an   object   that   stores   a   pointer   to   an   allocated   object   of   type   Type*   that   ensures   that   the   object   to   which   it   points   gets   destroyed   automatically   when   control   leaves   a   block.  
   
  template   <class   Type>  
  class   auto_ptr  
  Parameter  
  Type    
  The   type   of   object   for   which   storage   is   being   allocated   or   deallocated.    
  Remarks  
  The   template   class   describes   an   object   that   stores   a   pointer   to   an   allocated   object   myptr   of   type   Type   *.   The   stored   pointer   must   either   be   null   or   designate   an   object   allocated   by   a   new   expression.   An   object   constructed   with   a   nonnull   pointer   owns   the   pointer.   It   transfers   ownership   if   its   stored   value   is   assigned   to   another   object.   (It   replaces   the   stored   value   after   a   transfer   with   a   null   pointer.)   The   destructor   for   auto_ptr<Type>   deletes   the   allocated   object   if   it   owns   it.   Hence,   an   object   of   class   auto_ptr<Type>   ensures   that   an   allocated   object   is   automatically   deleted   when   control   leaves   a   block,   even   through   a   thrown   exception.   You   should   not   construct   two   auto_ptr<Type>   objects   that   own   the   same   object.  
   
  You   can   pass   an   auto_ptr<Type>   object   by   value   as   an   argument   to   a   function   call.   You   can   return   such   an   object   by   value   as   well.   Both   operations   depend   on   the   implicit   construction   of   intermediate   objects   of   class   auto_ptr<Type>::auto_ptr_ref<Other>,   by   various   subtle   conversion   rules.   You   cannot,   however,   reliably   manage   a   sequence   of   auto_ptr<Type>   objects   with   a   Standard   Template   Library   container.  
   
  Requirements  
  Header:   <memory>  
   
  Top

相关问题

  • auto_ptr问题.
  • auto_ptr的问题
  • 关于释放控制权的问题
  • const auto_ptr保证拥有权不能转移的实现原理?
  • vector中能放auto_ptr吗?
  • 请教!auto_ptr的使用
  • 关于 auto_ptr 的 operator=
  • 问关于auto_ptr和auto_ptr_ref的问题
  • auto变量是在作用域结束后释放还是调用它的函数结束后释放呢?
  • auto_ptr_ref<T>干什么用的?

关键词

  • c++
  • stl
  • 代码
  • vc
  • release
  • template
  • null
  • auto
  • ptr
  • own

得分解答快速导航

  • 帖主:huwenmin
  • oopig
  • oopig
  • jyfcsdn
  • xiaoyunet
  • titati

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo