问题描述
我读过,Could谁能解释这些不确定的行为,(I = I + + + + I,I = I + +,等...)和试图理解的的浪费2个多小时的时间试图解释通过gcc编译器下面的结果后>序列上的点。
I've read, Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc...) and tried understanding Sequence points on "comp.lang.c FAQ" after wasting more than 2 hours of time trying to explain the following results by gcc compiler.
expression(i=1;j=2) i j k
k = i++ + j++; 2 3 3
k = i++ + ++j; 2 3 4
k = ++i + j++; 2 3 4
k = ++i + ++j; 2 3 5
k = i++ + i++; 3 2
k = i++ + ++i; 3 4
k = ++i + i++; 3 4
k = ++i + ++i; 3 6
i = i++ + j++; 4 3
i = i++ + ++j; 5 3
i = ++i + j++; 4 3
i = ++i + ++j; 5 3
i = i++ + i++; 4
i = i++ + ++i; 5
i = ++i + i++; 5
i = ++i + ++i; 6
问:
-
我想知道,如果在上面的图所示(4组)中的所有前pressions有不确定的行为吗?如果只有其中的一些具有不确定的行为哪些呢,哪些没有?
I want to know if all the expressions shown (in 4 groups) in above figure have undefined behavior? If only some of them have undefined behavior which ones does and which ones doesn't?
有关规定的行为前pressions,请你能显示(不解释)的编译器是如何评价他们。只是为了确保,如果我得到这个pre-增量和放大器;后递增正确。
For defined behaviour expressions, kindly can you show (not explain) how compiler evaluates them. Just to make sure, if I got this pre-increment & post increment correctly.
背景:
今天,我参加了一个校园采访,其中有人问我解释的结果我+ + + + I
为给定值 I
。在编译GCC的前pression后,我意识到,我在接受采访时给出了答案是错的。我决定不再做这样的错误在未来,因此,尝试编写的 pre和后增量运营商的所有可能的组合的和编译他们的gcc,然后试图解释的结果。我奋斗了2个多小时。我找不到这些前pressions评价的单一行为。于是,我放弃了,转身计算器。阅读档案点点后,发现有类似序列点
和未定义的行为。
Today, I've attended a campus interview, in which I was asked to explain the results of i++ + ++i
for a given value of i
. After compiling that expression in gcc, I realized that the answer I gave in interview was wrong. I decided not to make such mistake in future and hence, tried to compile all possible combinations of pre and post increment operators and compile them in gcc and then try to explain the results. I struggled for more than 2 hours. I couldn't find single behaviour of evaluation of these expressions. So, I gave up and turned to stackoverflow. After little bit of reading archives, found that there is something like sequence point
and undefined behaviour.
推荐答案
除第一组,在其他三组所有前pressions具有不确定的行为。
Except the first group, all expressions in the other three groups have undefined behaviour.
如何定义behviour评估(第1组):
i=1, j=2;
k=i++ + j++; // 1 + 2 = 3
k=i++ + ++j; // 1 + 3 = 4
k=++i + ++j; // 2 + 3 = 5
k=++i + j++; // 2 + 2 = 4
这是相当直截了当。后递增VS pre-增量的事情。
It's fairly straight forward. post-increment vs pre-increment thing.
在第2组和第4组,它很容易看到不确定的行为。
In group 2 and group 4, it's quite easy to see the undefined behaviours.
第2组是未定义的行为,因为 =
运营商没有引入序列点。
Group 2 has undefined behaviour because =
operator doesn't introduce a sequence point.
这篇关于其中后与放大器的下列组合; pre-增量运营商已经用C不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!