问题描述
什么是这两个宏之间的区别?
的#define交换(A,B)(((一)^(B))及及((A)^ =(B)^ =(A)^ = (二)))
或者
的#define交换(A,B)(((一)^(B))及及((B)^ =(A)^ =(B),(一个)^ =(b)))
我看到了第二个宏:
Explanation:
The first one is modifying a
twice between two sequence points and hence behavior is undefined as per statement: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. That's it (no need to think about b
).
C11 documentation says:
6.5 Expressions (p2):
In (a) ^= (b) ^= (a) ^= (b)
, side effect on a
is unsequenced and hence invoke undefined behavior. It should be noted that C11 6.5 p1 says that:
This guarantees that in
(a) ^= (b) ^= (a) ^= (b)
| | | |
1 2 3 4
all subexpressions 1, 2, 3 and 4 are guaranteed to be computed before the result computation of left most ^=
operator. But, this doesn't guarantee that side effect of expression 3 is guaranteed before the value computation of the result of the left most ^=
operator.
这篇关于交换价值与XOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!