为什么这仍然有效? [英] why is this still working??

查看:61
本文介绍了为什么这仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:


int& dangling_reference();


int main(int argc,char * argv [])

{

整数* a = new整数(4);

整数* b;

b = a;

删除a;


//悬空参考1

cout<<" a:"<< a->整数<<" b:"<< b->整数<< endl;


//悬空参考2

int x = dangling_reference();

cout<< " int x:"<< x;


cin.get();

}


int& dangling_reference(){

int x = 3;

返回x;

}


为什么不是我得到一个错误,为什么a->整数en x的值是正确的?

这是一个悬空参考错误??

奇怪我认为

This is my code:

int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;

// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don''t I get an error and why are the values of a->integer en x correct??
It''s this a dangling reference fault??

bizar i think

推荐答案

2004年10月20日星期三10:47:21 -0700,Hans Van den Eynden写道:
On Wed, 20 Oct 2004 10:47:21 -0700, Hans Van den Eynden wrote:
这是我的代码:

int& dangling_reference();
为什么我不会收到错误?为什么a->整数en x的值正确?
这是一个悬空参考错误??
This is my code:

int& dangling_reference(); Why don''t I get an error and why are the values of a->integer en x correct??
It''s this a dangling reference fault??



最有可能不会对堆栈中存储

x的位置造成任何混乱。


Most likely cause nothing messes with the location on the stack where
x is stored.


Hans Van den Eynden写道:
Hans Van den Eynden wrote:
这是我的代码:

int& dangling_reference();
int main(int argc,char * argv [])
{
Integer * a = new Integer(4);


WTF是''整数''?

整数* b;
b = a;
删除a;

//悬挂参考1
cout<<" a:"<< a->整数<<" b:"<< b->整数<< ENDL;


这不是悬挂的参考。那是'

的成员,这个对象的生命周期已经结束。发生未定义的行为。

//悬挂参考2
int x = dangling_reference();
cout<<" int x:"<< x;

cin.get();
}

int& dangling_reference(){
int x = 3;
返回x;
}
为什么我不会收到错误,为什么a-的值为>整数en x正确??


你想得到什么错误?行为未定义。您的实施可能(拥有所有权利)创建将给您带来某种错误的代码。或者它可以(拥有所有权利)创建代码

,它将格式化您计算机的硬盘驱动器。标准在这里没有要求



这是一个悬空参考故障??
This is my code:

int& dangling_reference();

int main(int argc, char *argv[])
{
Integer *a = new Integer(4);
WTF is ''Integer''?
Integer *b;
b=a;
delete a;

//dangling reference 1
cout<<"a: "<<a->integer<<"b: "<<b->integer<<endl;
That''s not a dangling reference. That''s accessing members of
an object whose lifetime has ended. Undefined behaviour occurs.

// dangling reference 2
int x=dangling_reference();
cout<<"int x: "<<x;

cin.get();
}

int& dangling_reference() {
int x=3;
return x;
}

Why don''t I get an error and why are the values of a->integer en x correct??
What error would you like to get? The behaviour is undefined. Your
implementation may (has all rights to) create the code that will give
you some kind of error. Or it may (has all rights to) create the code
that will format the hard drive of your computer. The Standard makes
no requirement here.
It''s this a dangling reference fault??




不,这是你的错。您编写了具有未定义行为的代码。


V



No, it''s your fault. You wrote the code that has undefined behaviour.

V


Hans Van den Eynden写道:
Hans Van den Eynden wrote:

为什么我不会收到错误?为什么a->整数en x的值正确?
这是一个悬空参考错误??

Why don''t I get an error and why are the values of a->integer en x correct??
It''s this a dangling reference fault??



没有悬挂引用错误这样的东西。两种情况都是

未定义的行为。当你违反

这样的规则时,没有要求编译器检测到

或者有任何特定行为。


There''s no such thing as a "dangling reference fault." Both cases are
undefined behavior. There is no requirement that the compiler detect
or otherwise have any specific behavior when you break the rules like
that.


这篇关于为什么这仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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