不寻常的操作员行为 [英] Unusual operator behavior

查看:79
本文介绍了不寻常的操作员行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!

我想知道以下一段代码中的表达式如何评价以及为什么:


#include< iostream>

使用命名空间std;


int main()

{

int n = 5,p;

n = n + n ++;

cout<< n = << n<< endl;

n = 5;

p = n + n ++;

cout<< p = << p << endl;


返回0;

}

我用g ++和bcc32编译了它,结果是相同的:

n = 11

p = 10

第一个结果似乎很明显。但我不知道对于p变量的评估是什么。

。我真的很感兴趣它是如何工作的。提前谢谢,

Karlo。

Greetings!
I''m wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don''t know what''s going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.

推荐答案

Karlo Basic写道:
Karlo Basic wrote:
我想知道下面一段代码中的表达式如何评估以及为什么:

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

int main()
{n / 5,p;
n = n + n ++;


此表达式多次修改n的存储值。

未定义的行为。

cout< < n = << n<< endl;
n = 5;
p = n + n ++;


此表达式修改''n'',同时读取旧值

,用于确定其新值以外的目的。未定义的行为。

cout<< p = << p << endl;

返回0;
}
我用g ++和bcc32编译了它,结果是一样的:
n = 11
p = 10
第一个结果似乎很明显。但我不知道对p变量的评估是怎么回事。我真的很感兴趣它是如何工作的。
I''m wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
This expression modifies the stored value of ''n'' more than once.
Undefined behavior.
cout << "n = " << n << endl;
n = 5;
p = n + n++;
This expression modifies ''n'' and at the same time reads its old value
for a purpose other than determining its new value. Undefined behavior.
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don''t know what''s going on with
the evaluation of the p variable. I am really interested on how it
works.




没有办法预测或解释它是如何工作的。两个表达式

导致未定义的行为。


-

祝你好运,

Andrey Tarasevich



There''s no way to predict or explain how it will work. Both expressions
result in undefined behavior.

--
Best regards,
Andrey Tarasevich





Karlo Basic写道:


Karlo Basic wrote:

问候!<我想知道下面一段代码中的表达式如何评估以及为什么:

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

int main()
{n / 5,p;
n = n + n ++;


在序列点之间修改变量两次 - >未定义的行为

cout<< n = << n<< endl;
n = 5;
p = n + n ++;
cout<< p = << p << endl;

返回0;
}
我用g ++和bcc32编译了它,结果是一样的:
n = 11
p = 10
第一个结果似乎很明显。


不是真的。

但是我不知道对p变量的评估是怎么回事。我真的很感兴趣它是如何工作的。

Greetings!
I''m wondering how do the expressions in the following piece of code
evaluate and why:

#include <iostream>
using namespace std;

int main()
{
int n = 5, p;
n = n + n++;
modifying a variable twice between sequence points -> undefined behaviour
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;

return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious.
Not really.
But I don''t know what''s going on with
the evaluation of the p variable. I am really interested on how it
works.




没有正确的答案。编译器可以做任何想做的事情。

你的程序产生了不确定的行为,因为没有,所以考虑一个'正确的解决方案'是没有意义的。


-

Karl Heinz Buchegger
kb * *****@gascad.at


标准的相关部分是第5节第4段


除非另有说明,否则单个

运算符的操作数和单个表达式的子表达式的评估顺序以及发生副作用的订单

未指定.53)在

上一个和下一个序列点之间,一个标量对象应该通过表达式的评估最多修改一次它存储的

值。

此外,只能访问先前值以确定要存储的

值。对于完整表达式的子表达式的每个允许顺序,应满足本段的要求

否则行为未定义。 [例如:

i = v [i ++]; //行为未指定

i = 7,i ++,i ++; //我变成了9

i = ++ i + 1; //行为未指定

i = i + 1; // i的值递增

?结束示例]

The relevant part of the standard is Section 5 paragraph 4

Except where noted, the order of evaluation of operands of individual
operators and subexpressions of indi-vidual expressions, and the order
in which side effects take place, is unspecified.53) Between the
previous and next sequence point a scalar object shall have its stored
value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored. The requirements of this paragraph shall be met for
each allowable ordering of the subexpressions of a full expression;
otherwise the behavior is undefined. [Example:
i = v[i++]; // the behavior is unspecified
i = 7, i++, i++; // i becomes 9
i = ++i + 1; // the behavior is unspecified
i = i + 1; // the value of i is incremented
?end example]


这篇关于不寻常的操作员行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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