bug:std :: auto_ptr中的堆栈溢出 [英] bug: stack overflow in std::auto_ptr

查看:103
本文介绍了bug:std :: auto_ptr中的堆栈溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

请尝试此代码。我认为这是完全合法的。但是当在MSVC71下编译的
堆栈溢出发生在main的第一行时,因此

第二行永远不会被执行。

B.


#include< iostream>

#include< memory>


using namespace std;
< br $>
struct A

{

虚拟~A(){}

};

struct B:public A

{

};


auto_ptr< B> test()

{

返回auto_ptr< B>(新B);

};


int main()

{

auto_ptr< A> r = test();

std :: cout<< 你不会看到这个! << std :: endl;

}

Hi
Please try this code. I think that it''s perfectly legal. However when
compiled under MSVC71 stack overflow happens in first line of main, thus
second line is never executed.
B.

#include <iostream>
#include <memory>

using namespace std;

struct A
{
virtual ~A() {}
};

struct B : public A
{
};

auto_ptr<B> test()
{
return auto_ptr<B>(new B);
};

int main()
{
auto_ptr<A> r = test();
std::cout << "You won''t see this!" << std::endl;
}

推荐答案

Bronek,


编译你的代码用VC7.1给出了这个警告:


警告C4717:''std :: auto_ptr< B> :: operator< A> std :: auto_ptr_ref< A>'':

在所有控制路径上递归,函数将导致运行时堆栈

溢出


....这就是你在实践中发生的事情。


在线Comeau编译器不会编译你的代码,说:


""" ComeauTest.c",第23行:错误:class" std :: auto_ptr< A>"没有

合适的复制构造函数

auto_ptr< A> r = test();

"


Dave

-

MVP VC ++常见问题: http://www.mvps.org/vcfaq
Bronek,

Compiling your code with VC7.1 gives this warning:

warning C4717: ''std::auto_ptr<B>::operator<A> std::auto_ptr_ref<A>'' :
recursive on all control paths, function will cause runtime stack
overflow

.... which is what''s happening to you in practice.

The online Comeau compiler won''t compile your code, saying:

""ComeauTest.c", line 23: error: class "std::auto_ptr<A>" has no
suitable copy constructor
auto_ptr<A> r = test();
"

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq


2004年1月22日星期四14:49:54 +0000,David Lowndes写道:
On Thu, 22 Jan 2004 14:49:54 +0000, David Lowndes wrote:
警告C4717:''std :: auto_ptr< B> :: operator< a取代; std :: auto_ptr_ref< A>'':
递归所有控制路径,函数会导致运行时堆栈溢出

...这就是你发生的事情在实践中。


....并将我们置于未定义行为领域。

"" ComeauTest.c",第23行:错误:类" ;性病:: auto_ptr的< a取代;"没有合适的复制构造函数
auto_ptr< A> r = test();
warning C4717: ''std::auto_ptr<B>::operator<A> std::auto_ptr_ref<A>'' :
recursive on all control paths, function will cause runtime stack
overflow

... which is what''s happening to you in practice.
.... and puts us in the realm of Undefined Behaviour.
""ComeauTest.c", line 23: error: class "std::auto_ptr<A>" has no
suitable copy constructor
auto_ptr<A> r = test();




这意味着代码是非法的。是吗 ?无论如何我会说

" std :: auto_ptr是有缺陷的,必须修复 :((

B.



which means that code is illegal. Is it ? Anyway I''d say that
"std::auto_ptr is flawed and has to be fixed" :((
B.


>>""ComeauTest.c"第23行:错误:class" std :: auto_ptr< A>"没有
>> ""ComeauTest.c", line 23: error: class "std::auto_ptr<A>" has no
合适的复制构造函数
auto_ptr< A> r = test();
这意味着代码是非法的。是吗?
suitable copy constructor
auto_ptr<A> r = test();
which means that code is illegal. Is it ?




不熟悉我不能说的标准 - 但

Comeau编译器是通常是对的。

无论如何我会说
std :: auto_ptr有缺陷并且必须修复:((



Not being intimately familiar with the standards I can''t say - but the
Comeau compiler is usually right.
Anyway I''d say that
"std::auto_ptr is flawed and has to be fixed" :((



从我读过的关于std :: auto_ptr的内容来看,它有很多问题,可以比它的价值更麻烦。


Dave

-

MVP VC ++ FAQ: http://www.mvps.org/vcfaq


这篇关于bug:std :: auto_ptr中的堆栈溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆