它是C ++ Standard中未定义的行为吗? [英] Is it an undefined behavior in C++ Standard?

查看:90
本文介绍了它是C ++ Standard中未定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// classA.cpp:定义控制台应用程序的入口点。

//


#include" stdafx.h"


#include< iostream>


using namespace std;


static unsigned int count = 0 ;


A级

{

公开:

A(){

p = this;

}


~A(){

if(p){

cout<< count ++<< endl;

删除p;

p = 0;

}

}


私人:

A * p;

};


int _tmain(int argc,_TCHAR * argv [ ])

{

A a;


返回0;

}


/ *编译器:visual c ++ 2005 express edition beta版中的MSC ++编译器

命令:cl / nologo / EHsc classA.cpp

* /


/ *结果:

0

1

2

...

23485

按任意键继续

* /


/ *

问题:这是C ++标准中的未定义行为吗?


详细说明:当析构函数调用''delete p''时,p是A类的一个点,它将再次导致析构函数


是否有任何限制递归调用析构函数?

* / ???

// classA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

using namespace std;

static unsigned int count = 0;

class A
{
public:
A() {
p = this;
}

~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}

private:
A *p;
};

int _tmain(int argc, _TCHAR* argv[])
{
A a;

return 0;
}

/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/

/* result:
0
1
2
...
23485
press any key to continue
*/

/*
question: Is it an undefined behavior in C++ Standard?

details: p is a point of class A, when destructor call ''delete p'',it will
cause destructor again.
Is there any limit calling destructor recursively?
*/???

推荐答案



" aka" < AK ******** @ hotmail.com> skrev i en meddelelse

news:d0 ********** @ news.yaako.com ...

"aka" <ak********@hotmail.com> skrev i en meddelelse
news:d0**********@news.yaako.com...
// classA.cpp:定义控制台应用程序的入口点。
//

#include" stdafx.h"

#include< iostream>
using namespace std;

static unsigned int count = 0;

A类
{
公开:
A(){< br => p = this;
}
~A(){
if(p){
cout<< count ++<< endl;
删除p;
p = 0;
}
}

私人:
A * p;
} ;

int _tmain(int argc,_TCHAR * argv [])
{
A a;

返回0;
}

/ *编译器:visual c ++ 2005 express edition中的MSC ++编译器beta
命令:cl / nologo / EHsc classA.cpp
* /

/ *结果:
0
2
...
23485
按任意键继续
* /

/ *
问题:它是C ++ Standard中的未定义行为吗?
是什么?你所做的是多次调用一个对象的析构函数

并导致未定义的行为。
详细信息:当析构函数调用''delete p'时,p是A类的一个点,它会再次导致析构函数。
是否有任何限制递归调用析构函数?


否。您可以随心所欲。几乎!你的程序

不在你的程序中 - 它有无限的递归。


我也看不到你想要做什么。

/ Peter

* / ???
// classA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

using namespace std;

static unsigned int count = 0;

class A
{
public:
A() {
p = this;
}

~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}

private:
A *p;
};

int _tmain(int argc, _TCHAR* argv[])
{
A a;

return 0;
}

/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/

/* result:
0
1
2
...
23485
press any key to continue
*/

/*
question: Is it an undefined behavior in C++ Standard? What is? What you do is calling the destructor of an object more than once
and that causes undefined behaviour.
details: p is a point of class A, when destructor call ''delete p'',it will
cause destructor again.
Is there any limit calling destructor recursively?
No. You can do as long as you please. Almost! Your program
Not in your program - it has endless recursion.

Also I fail to sees what youre attempting to do.
/Peter
*/???



aka写道:
// classA.cpp:定义控制台应用程序的入口点。
//
#include" stdafx.h"

#include< iostream> ;

使用命名空间std;

static unsigned int count = 0;

A类
{
公开:
A(){
p = this;


为什么?

}

~A(){
if(p){
cout << count ++<< endl;
删除p;
p = 0;
}
}

私人:
A * p;
} ;

int _tmain(int argc,_TCHAR * argv [])
{
A a;

返回0;
}

/ *编译器:visual c ++ 2005 express edition中的MSC ++编译器beta
命令:cl / nologo / EHsc classA.cpp
* /

/ *结果:
0
2
...
23485
按任意键继续
* /

/ *
问题:它是C ++ Standard中的未定义行为吗?



是的。


详情:p是A类的一个点,当析构函数调用''delete p''时,它会再次导致析构函数。
是否有任何限制递归调用析构函数?
// classA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

using namespace std;

static unsigned int count = 0;

class A
{
public:
A() {
p = this;
Why?
}

~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}

private:
A *p;
};

int _tmain(int argc, _TCHAR* argv[])
{
A a;

return 0;
}

/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/

/* result:
0
1
2
...
23485
press any key to continue
*/

/*
question: Is it an undefined behavior in C++ Standard?

Yes.


details: p is a point of class A, when destructor call ''delete p'',it will
cause destructor again.
Is there any limit calling destructor recursively?



只要它可以。 :-)


-

Ioannis Vranos

http://www23.brinkster.com/noicys


aka写道:
aka wrote:
// classA.cpp:定义控制台应用程序的入口点。
//

#include" stdafx.h"

#include< iostream>
使用命名空间std;

static unsigned int count = 0;

A类
{
公开:
A(){
p = this;


您将指向对象本身的指针存储为成员吗?那会是什么好

for?

}

~A(){
if(p){


p从不为0,除非您的程序出错。

cout<< count ++<< endl;
删除p;


这会以递归方式调用析构函数。这是不允许的。对于每个

对象,构造函数和析构函数只被调用一次。

另一个问题是你必须永远不要在你的对象上使用delete

没有得到新的。所以这对任何非动态分配的

对象都无效。

p = 0;
}
}

private:
A * p;
};

int _tmain(int argc,_TCHAR * argv [])
{
A a;

返回0;
}
/ *编译器:visual c ++ 2005 express edition中的MSC ++编译器测试版:
命令:cl / nologo / EHsc classA。 cpp
* /

/ *结果:
0
1
2
...
23485
按任意键继续
* /

/ *
问题:这是C ++标准中的未定义行为吗?


是的。

详情:p是A类的一个点,当析构函数调用''delete p''时,它会导致析构函数再次。


出于什么原因?你想要完成什么?

是否有任何限制递归调用析构函数?
// classA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

using namespace std;

static unsigned int count = 0;

class A
{
public:
A() {
p = this;
You store a pointer to the object itself as member? What would that be good
for?
}

~A() {
if (p) {
p is never 0 unless there is an error in your program.
cout << count++ << endl;
delete p;
This would recursively call the destructor. This is not allowed. For each
object, a constructor as well as the destructor gets called exactly once.
Another problem is that you must never use delete on an object that you
didn''t get from new. So this isn''t valid for any non-dynamically allocated
objects.
p = 0;
}
}

private:
A *p;
};

int _tmain(int argc, _TCHAR* argv[])
{
A a;

return 0;
}

/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/

/* result:
0
1
2
...
23485
press any key to continue
*/

/*
question: Is it an undefined behavior in C++ Standard?
Yes.
details: p is a point of class A, when destructor call ''delete p'',it will
cause destructor again.
For what reason? What are you trying to accomplish?
Is there any limit calling destructor recursively?




是的。它必须_never_递归调用。另外,你的''删除p''没有
只是调用析构函数,它还试图释放对象

占用的内存,这只适用于对象是动态分配的。

实际上,我对你的程序没有崩溃感到惊讶。



Yes. It must _never_ be called recursively. Also, your ''delete p'' doesn''t
just call the destructor, it also tries to free the memory that the object
occupied, which only works if the object is allocated dynamically.
Actually, I''m surprised that your program doesn''t crash.


这篇关于它是C ++ Standard中未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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