#include <stdio.h>
int main(void)
{
int i = 0;
i = i++ + ++i;
printf("%d\n", i); // 3
i = 1;
i = (i++);
printf("%d\n", i); // 2 Should be 1, no ?
volatile int u = 0;
u = u++ + ++u;
printf("%d\n", u); // 1
u = 1;
u = (u++);
printf("%d\n", u); // 2 Should also be one, no ?
register int v = 0;
v = v++ + ++v;
printf("%d\n", v); // 3 (Should be the same as u ?)
int w = 0;
printf("%d %d\n", ++w, w); // shouldn't this print 1 1
int x[2] = { 5, 8 }, y = 0;
x[y] = y ++;
printf("%d %d\n", x[0], x[1]); // shouldn't this print 0 8? or 5 0?
}
当前回答
只要编译和反汇编你的代码行,如果你如此倾向于知道它是如何得到你所得到的。
这是我从我的机器上得到的,以及我认为正在发生的事情:
$ cat evil.c
void evil(){
int i = 0;
i+= i++ + ++i;
}
$ gcc evil.c -c -o evil.bin
$ gdb evil.bin
(gdb) disassemble evil
Dump of assembler code for function evil:
0x00000000 <+0>: push %ebp
0x00000001 <+1>: mov %esp,%ebp
0x00000003 <+3>: sub $0x10,%esp
0x00000006 <+6>: movl $0x0,-0x4(%ebp) // i = 0 i = 0
0x0000000d <+13>: addl $0x1,-0x4(%ebp) // i++ i = 1
0x00000011 <+17>: mov -0x4(%ebp),%eax // j = i i = 1 j = 1
0x00000014 <+20>: add %eax,%eax // j += j i = 1 j = 2
0x00000016 <+22>: add %eax,-0x4(%ebp) // i += j i = 3
0x00000019 <+25>: addl $0x1,-0x4(%ebp) // i++ i = 4
0x0000001d <+29>: leave
0x0000001e <+30>: ret
End of assembler dump.
(我…假设0x00000014指令是某种编译器优化?)
其他回答
虽然任何编译器和处理器实际上都不太可能这样做,但在C标准下,编译器使用以下序列实现"i++"是合法的:
In a single operation, read `i` and lock it to prevent access until further notice
Compute (1+read_value)
In a single operation, unlock `i` and store the computed value
虽然我不认为有任何处理器支持这样的硬件,可以有效地完成这样的事情,人们可以很容易地想象这样的行为会使多线程代码更容易(例如,它将保证如果两个线程同时尝试执行上面的序列,I将增加2),而且未来的某些处理器可能会提供类似的功能,这并不是完全不可想象的。
If the compiler were to write i++ as indicated above (legal under the standard) and were to intersperse the above instructions throughout the evaluation of the overall expression (also legal), and if it didn't happen to notice that one of the other instructions happened to access i, it would be possible (and legal) for the compiler to generate a sequence of instructions that would deadlock. To be sure, a compiler would almost certainly detect the problem in the case where the same variable i is used in both places, but if a routine accepts references to two pointers p and q, and uses (*p) and (*q) in the above expression (rather than using i twice) the compiler would not be required to recognize or avoid the deadlock that would occur if the same object's address were passed for both p and q.
C标准规定,一个变量最多只能在两个序列点之间赋值一次。例如,分号是一个序列点。 所以每个形式的表述
i = i++;
i = i++ + ++i;
等等都违反了这个规则。该标准还说行为是未定义的,而不是未指定的。一些编译器确实检测这些并产生一些结果,但这不是每个标准。
但是,两个不同的变量可以在两个序列点之间递增。
while(*src++ = *dst++);
以上是在复制/分析字符串时常见的编码实践。
ISO W14站点的文档n1188提供了关于这种计算的一个很好的解释。
我解释这些想法。
适用于这种情况的ISO 9899标准的主要规则是6.5p2。
在前一个序列点和下一个序列点之间,通过表达式求值,对象的存储值最多修改一次。此外,前面的值只能被读取,以确定要存储的值。
像i=i++这样的表达式中的序列点在i=i之前和i++之后。
In the paper that I quoted above it is explained that you can figure out the program as being formed by small boxes, each box containing the instructions between 2 consecutive sequence points. The sequence points are defined in annex C of the standard, in the case of i=i++ there are 2 sequence points that delimit a full-expression. Such an expression is syntactically equivalent with an entry of expression-statement in the Backus-Naur form of the grammar (a grammar is provided in annex A of the Standard).
所以盒子里的指令顺序没有明确的顺序。
i=i++
可以解释为
tmp = i
i=i+1
i = tmp
或者是
tmp = i
i = tmp
i=i+1
因为解释代码i=i++的所有这些形式都是有效的,并且都生成不同的答案,所以行为是未定义的。
因此,序列点可以从组成程序的每个方框的开始和结束处看到(方框是C语言中的原子单位),并且在方框中,指令的顺序并不在所有情况下都是定义的。改变顺序有时会改变结果。
编辑:
其他解释这种歧义的很好的来源是c-faq网站(也出版了一本书)的条目,即这里、这里和这里。
我认为C99标准的相关部分是6.5表达式,§2
在前一个序列点和下一个序列点之间,一个对象应该有它的存储值 由表达式求值最多修改一次。此外,先验值 应该是只读的,以确定要存储的值。
和6.5.16赋值操作符,§4:
操作数的求值顺序未指定。如果试图修改 赋值运算符的结果或在下一个序列点之后访问它的结果 行为是未定义的。
你的问题可能不是“为什么这些构念在C语言中是未定义的行为?”你的问题可能是,“为什么这段代码(使用++)没有给我预期的值?”,有人将你的问题标记为重复,并将你发送到这里。
这个答案试图回答这个问题:为什么您的代码没有给您预期的答案,以及您如何学会识别(和避免)不能按预期工作的表达式。
我假设您已经听说了C的c++和——操作符的基本定义,以及前缀形式++x与后缀形式x++的区别。但是这些运算符很难理解,所以为了确保你理解了,也许你写了一个很小的测试程序,涉及到
int x = 5;
printf("%d %d %d\n", x, ++x, x++);
但是,让你惊讶的是,这个程序并没有帮助你理解——它输出了一些奇怪的、无法解释的输出,这表明++可能做了一些完全不同的事情,完全不是你想的那样。
或者,也许你正在看到一个难以理解的表达,比如
int x = 5;
x = x++ + ++x;
printf("%d\n", x);
也许有人把代码作为谜题给你。这段代码也没有意义,特别是在运行它的时候——如果在两个不同的编译器下编译和运行它,可能会得到两个不同的答案!这是怎么回事?哪个答案是正确的?(答案是两者都是,或者都不是。)
正如您现在所听到的,这些表达式是未定义的,这意味着C语言不能保证它们将做什么。这是一个奇怪而令人不安的结果,因为您可能认为您可以编写的任何程序,只要它编译并运行,就会生成一个唯一的、定义良好的输出。但在未定义行为的情况下,就不是这样了。
什么使表达式没有定义?包含++和——的表达式总是未定义的吗?当然不是:这些都是有用的运算符,如果使用得当,它们的定义是完美的。
对于我们正在讨论的表达式,当同时发生太多事情时,当我们无法说出事情发生的顺序,但当顺序对我们得到的结果很重要时,它们就没有定义了。
让我们回到我在这个回答中使用的两个例子。当我写的时候
printf("%d %d %d\n", x, ++x, x++);
the question is, before actually calling printf, does the compiler compute the value of x first, or x++, or maybe ++x? But it turns out we don't know. There's no rule in C which says that the arguments to a function get evaluated left-to-right, or right-to-left, or in some other order. So we can't say whether the compiler will do x first, then ++x, then x++, or x++ then ++x then x, or some other order. But the order clearly matters, because depending on which order the compiler uses, we'll clearly get a different series of numbers printed out.
那么这个疯狂的表达呢?
x = x++ + ++x;
The problem with this expression is that it contains three different attempts to modify the value of x: (1) the x++ part tries to take x's value, add 1, store the new value in x, and return the old value; (2) the ++x part tries to take x's value, add 1, store the new value in x, and return the new value; and (3) the x = part tries to assign the sum of the other two back to x. Which of those three attempted assignments will "win"? Which of the three values will actually determine the final value of x? Again, and perhaps surprisingly, there's no rule in C to tell us.
You might imagine that precedence or associativity or left-to-right evaluation tells you what order things happen in, but they do not. You may not believe me, but please take my word for it, and I'll say it again: precedence and associativity do not determine every aspect of the evaluation order of an expression in C. In particular, if within one expression there are multiple different spots where we try to assign a new value to something like x, precedence and associativity do not tell us which of those attempts happens first, or last, or anything.
有了这些背景知识和介绍,如果你想确保你所有的程序都是定义良好的,哪些表达式可以写,哪些表达式不能写?
这些表达都不错:
y = x++;
z = x++ + y++;
x = x + 1;
x = a[i++];
x = a[i++] + b[j++];
x[i++] = a[j++] + b[k++];
x = *p++;
x = *p++ + *q++;
这些表达式都是未定义的:
x = x++;
x = x++ + ++x;
y = x + x++;
a[i] = i++;
a[i++] = i;
printf("%d %d %d\n", x, ++x, x++);
最后一个问题是,你如何分辨哪些表达式是定义良好的,哪些表达式是未定义的?
就像我之前说的,未定义表达式是那些同时有太多事情发生的表达式,你不能确定事情发生的顺序,以及顺序在哪里很重要:
如果有一个变量在两个或多个不同的地方被修改(赋值给),您如何知道哪个修改先发生? 如果有一个变量在一个地方被修改,而它的值在另一个地方被使用,你怎么知道它使用的是旧值还是新值?
以#1为例,在表达式中
x = x++ + ++x;
有三次修改x的尝试。
作为#2的例子,在表达式中
y = x + x++;
我们都使用x的值,并修改它。
所以这就是答案:确保在您编写的任何表达式中,每个变量最多被修改一次,如果一个变量被修改,您也不要试图在其他地方使用该变量的值。
还有一件事。您可能想知道如何“修复”我在回答这个问题时提出的未定义表达式。
在printf("%d %d %d\n", x, ++x, x++);的情况下,这很简单-只需将它写成三个独立的printf调用:
printf("%d ", x);
printf("%d ", ++x);
printf("%d\n", x++);
现在行为已经完全定义好了,您将得到合理的结果。
另一方面,在x = x++ + ++x的情况下,没有办法修复它。没有办法编写它来保证它的行为符合您的期望——但这没关系,因为无论如何在实际程序中都不会编写像x = x++ + ++x这样的表达式。
推荐文章
- SQL逻辑运算符优先级:And和Or
- C多行宏:do/while(0) vs作用域块
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 如果不是内存地址,C指针到底是什么?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 保护可执行文件不受逆向工程的影响?
- 从C语言的函数返回一个struct
- C99 'restrict'关键字的实际用法?
- 检查子字符串存在于C中的字符串中
- 从标准输入中捕获字符,而不需要等待按enter键