问题描述
假设 A
, B
, A
,和 b
都是变量, A
, b的地址
, A
和 b
都是不同的。然后,以下code:
Suppose A
, B
, a
, and b
are all variables, and the addresses of A
, B
, a
, and b
are all different. Then, for the following code:
A = a;
B = b;
不要C和C ++标准明确要求 A = A
严格之前执行B =
?鉴于 A
, B
, A
的地址,和 b
都是不同的,都允许编译器交换两个语句的执行顺序为了某种目的,如优化?
Do the C and C++ standard explicitly require A=a
be strictly executed before B=b
? Given that the addresses of A
, B
, a
, and b
are all different, are compilers allowed to swap the execution sequence of two statements for some purpose such as optimization?
如果回答我的问题是,在C和C ++不同,我想知道这两个。
If the answer to my question is different in C and C++, I would like to know both.
编辑:这个问题的背景如下。在板游戏AI设计,对于优化的人使用时,其正确性强烈依赖于执行顺序,如果我们不加挥发性
限制。
The background of the question is the following. In board game AI design, for optimization people use lock-less shared-hash table, whose correctness strongly depends on the execution order if we do not add volatile
restriction.
推荐答案
这两种标准都允许作为不改变观察到的行为进行乱序执行这些指令,那么长。这就是所谓的AS-if规则:
Both standards allow for these instructions to be performed out of order, so long as that does not change observable behaviour. This is known as the as-if rule:
- What exactly is the "as-if" rule?
- http://en.cppreference.com/w/cpp/language/as_if
请注意,由于在评论中指出,什么是观察的行为的意思是定义的行为程序的观察行为。如果你的程序有不确定的行为,那么编译器是从推理的借口。
Note that as is pointed out in the comments, what is meant by "observable behaviour" is the observable behaviour of a program with defined behaviour. If your program has undefined behaviour, then the compiler is excused from reasoning about that.
这篇关于对于{A = A; B =; },将" A = A"前&QUOT严格执行; B = B"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!