auto_ptr 为什么不释放拥有权
我得代码如下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



