i ++ + i ++问题 [英] i++ + i++ questions
问题描述
我有一个同事提出这个问题:
int i = 0;
int j,k;
j = i ++ + i ++;
k = ++ i + ++ i;
并询问j和k将具有什么。我希望编译器将添加
i到i(0 + 0),将j存储在j中,然后将i增加两次。在下一个
行,我们从i = 2开始。我增加两次,使它现在等于4.
然后它被添加到它自己(i + i)结果8存储在k中。我运行
它,它就是这样做的。我的同事声称这实际上是未定义的,并且你不能在你写的表达式中读取变量。
这真的是未定义的,我的编译器都做了我上面描述的那些
因为那是他们编写的编译器吗?或者这个功能是c ++应该有效的方式吗?
" Ook" < nousenetspam at dead ice dot us>在消息中写道
news:zu ******************** @ giganews.com ...I有一个同事提出这个问题:
int i = 0;
int j,k;
j = i ++ + i ++;
k = ++ i + + + i;
并询问j和k会有什么。我希望编译器将i添加到i(0 + 0),将0存储在j中,然后将i增加两次。在
下一行,我们从i = 2开始。我增加两次,使它现在等于4.然后它被添加到它自己(i + i)并且结果8被存储
在k。我运行它,它做到了这一点。我的同事表示,这实际上是未定义的,并且您无法在表达式中读取变量。
您在其中编写它。这是不是真的未定义,我的两个编译器都做了我上面描述的,因为这是他们编写的编译器?或者这个功能是c ++应该运行的方式吗?
你真的不能说,因为它会导致未定义的行为......你是
只允许在序列点之间最多更改一次值(
short)。只需查看C ++序列点即可。在网上,应该有
更清楚的解释,有什么序列点等的例子。
-
Ferdi Smit
Ook写道:我有一个同事提出这个问题:
int i = 0;
int j,k;
j = i ++ + i ++;
k = ++ i + ++ i;
并询问j和k会有什么。
行为是未定义的。
我希望编译器会将i / i添加到i(0 + 0),将0存储在j,然后增加i两次。
基于什么?我的意思是,你的期望必须来自
,对吧?你期望的来源是什么?
为什么不能从左边的i ++中取零值,增加i,然后
取1来自正确的i ++的值,增加i,并产生结果
为1?
为什么我不能记下我是在这个
表达式中递增,将旧值(0)加在一起,然后将新的
值1存储到i中,而不是关心写入两个增量?
语言标准中没有任何内容与任何这些可能的
解释相矛盾。
C ++实现可以暂停诊断消息,而不是
完全翻译程序。或者它可以默默地生成一个程序,它根本不起作用:它可能会崩溃,或计算任何值。
当心:你的同事可能是一名专家,负责发现你组织中的不称职者。
我的同事声称这实际上是未定义的
你可以不要在你写的表达式中读取变量。
Busted!可能像95%的C ++编程人群一样
不知道这些东西,所以你不像一个孤独的白痴或任何东西。
Ook写道:我有一个同事提出这个问题:
int i = 0;
int j,k; < br => j = i ++ + i ++;
k = ++ i + ++ i;
并询问j和k将具有什么。我希望编译器会将i / i添加到i(0 + 0),将j存储在j中,然后再增加i两次。在下一行
,我们从i = 2开始。我增加两次,使它现在等于4.
然后它被添加到它自己(i + i),结果,8,存储在k。我跑了
它,它就是这样做的。我的同事表示这实际上是未定义的,并且你不能在你编写它的表达式中读取变量。
这真的是未定义的,我的编译器都做了我上面描述的那些
因为那是他们编写的编译器吗?或者这个功能是c ++应该运行的方式吗?
它是未定义的。
I had a coworker present this problem:
int i = 0;
int j,k;
j = i++ + i++;
k = ++i + ++i;
And asked what j and k will have. I would expect that the compiler would add
i to i (0+0), store the 0 in j, and then increment i twice. On the next
line, we start with i = 2. I is incremented twice so that it now equals 4.
Then it is added to itself (i + i) and the result, 8, is stored in k. I run
it, and it does this. My coworker stated that this was actually undefined
and that you can''t read a variable in an expression where you write it. Is
this really undefined and both of my compilers do what I described above
because that is what they wrote the compiler? Or is this functionality the
way c++ should work?
"Ook" <nousenetspam at dead ice dot us> wrote in message
news:zu********************@giganews.com...I had a coworker present this problem:
int i = 0;
int j,k;
j = i++ + i++;
k = ++i + ++i;
And asked what j and k will have. I would expect that the compiler would
add i to i (0+0), store the 0 in j, and then increment i twice. On the
next line, we start with i = 2. I is incremented twice so that it now
equals 4. Then it is added to itself (i + i) and the result, 8, is stored
in k. I run it, and it does this. My coworker stated that this was
actually undefined and that you can''t read a variable in an expression
where you write it. Is this really undefined and both of my compilers do
what I described above because that is what they wrote the compiler? Or is
this functionality the way c++ should work?
You can''t really tell, because it results in undefined behaviour... You are
only allowed to change a value at most once between sequence points (in
short). Just look up "C++ sequence points" on the net, and there should be
some clearer explanation with examples of what sequence points are etc.
--
Ferdi Smit
Ook wrote:I had a coworker present this problem:
int i = 0;
int j,k;
j = i++ + i++;
k = ++i + ++i;
And asked what j and k will have.
The behavior is undefined.
I would expect that the compiler would add
i to i (0+0), store the 0 in j, and then increment i twice.
Based, exactly, on what? I mean, you expectation has to come from
somewhere, right? What is the source of your expectation?
Why can''t it take the zero value from the left i++, increment i, then
take the 1 value from the right i++, increment i, and produce a result
of 1?
Why can''t it make a note that i is to be incremented in this
expression, add together the old value (0), and then just store the new
value 1 into i, not caring that two increments were written?
Nothing in the language standard contradicts any of these possible
interpretations.
The C++ implementation can halt with a diagnostic message, and not
translate the program at all. Or it can silently produce a program that
doesn''t work at all: it may crash, or compute any values whatsoever.
Watch out: your coworker may be an expert who is on a mission to
uncover the incompetents in your organization.
My coworker stated that this was actually undefined
and that you can''t read a variable in an expression where you write it.
Busted! Probably something like 95% of the C++ programming population
doesn''t know this stuff, so you''re not like a lone idiot or anything.
Ook wrote:I had a coworker present this problem:
int i = 0;
int j,k;
j = i++ + i++;
k = ++i + ++i;
And asked what j and k will have. I would expect that the compiler would add
i to i (0+0), store the 0 in j, and then increment i twice. On the next
line, we start with i = 2. I is incremented twice so that it now equals 4.
Then it is added to itself (i + i) and the result, 8, is stored in k. I run
it, and it does this. My coworker stated that this was actually undefined
and that you can''t read a variable in an expression where you write it. Is
this really undefined and both of my compilers do what I described above
because that is what they wrote the compiler? Or is this functionality the
way c++ should work?
It''s undefined.
这篇关于i ++ + i ++问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!