作者LPH66 (-858993460)
看板C_and_CPP
标题Re: [问题] 请教如何只用x,y两变数来交换彼此数值
时间Mon Apr 11 23:40:29 2011
: → littleshan:不要这样写!这是 implementation-defined behavior 04/11 15:48
: → littleshan:就是十诫之八啦 04/11 15:49
基本上这个写法的确是 implementation-defined behavior
不管有没有加括号都一样
事情是这样的
a ^= b ^= a ^= b;
1 2 3 4 5 6 7
这样写的人想要做的事的顺序是
值为
(7) 取 b 的值 原来的 b
(5) 取 a 的值 原来的 a
(6) 做 (5)^(7) 并把值放回 a 去 原来的 a^b
(3) 取 b 的值 原来的 b
(4) 做 (3)^(6) 并把值放回 b 去 原来的 a
(1) 取 a 的值 原来的 a^b
(2) 做 (1)^(4) 并把值放回 a 去 原来的 b
但有些 compiler (如我手上的 g++ 4.1.0 for win) 会编成这个顺序
值为
(1) 取 a 的值 原来的 a
(3) 取 b 的值 原来的 b
(5) 取 a 的值 原来的 a
(7) 取 b 的值 原来的 b
(6) 做 (5)^(7) 并把值放回 a 去 原来的 a^b
(4) 做 (3)^(6) 并把值放回 b 去 原来的 a
(2) 做 (1)^(4) 并把值放回 a 去 一定是 0
结果是原来 a 的值在 b 但 a 却变成 0 了
问题在於
虽然这个式子的确是分析成
^= (2)
/ \
a ^= (4)
(1) / \
b ^= (6)
(3) / \
a b
(5) (7)
这个样子 但在计算 a ^= b 时 a b 取值的顺序却是 implementation-defined
也就是在上面那棵语法树的後序走访中先走左子树还是先走右子树
如果先走左子树 就是 1 3 5 7 6 4 2 的顺序
先走右子树 就是 7 5 6 3 4 1 2 的顺序
而这两个顺序 上面我也写出来了
後者是顺利交换没错 前者却是把 a 搬到 b 後把 a 炸成 0 !
所以结论是
千万不要用这个写法
---
其实简单一句话就是十三诫之八里说的
你不可以在一个运算式(expression)中,对一个基本型态的变数修改其值
超过一次以上。否则,将导致未定义的行为(undefined behavior)
如果真要用 xor 来交换 乖乖写三条
x ^= y; y ^= x; x ^= y;
就什麽事都没有....
当然最好的写法还是 int temp = x; x = y; y = temp; 就是了
--
'You've sort of made up for it tonight,' said Harry. 'Getting the
sword. Finishing the Horcrux. Saving my life.'
'That makes me sound a lot cooler then I was,' Ron mumbled.
'Stuff like that always sounds cooler then it really was,' said
Harry. 'I've been trying to tell you that for years.'
-- Harry Potter and the Deathly Hollows, P.308
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.30.142
1F:推 attomahawk:推荐这棵 语法树! 04/12 00:02
2F:推 glennchen:好强大 04/12 00:07
3F:推 loveme00835:不管多炫, 在可读性上永远是个失败品, 是禁忌 04/12 00:09
4F:推 james732:原来如此! 04/12 00:12
5F:推 VictorTom:推语法树:) 04/12 00:16
6F:→ bibo9901:借问一下, 那写成 a+=b; b=a-b; a-=b; 可以吗 04/12 00:39
7F:推 purpose:有缓冲区溢位的风险,如 a = INT_MAX; b = 1; 04/12 00:41
8F:→ purpose:没有缓冲区,打太快了 04/12 00:41
9F:推 littleshan:overflow 是 undefined behavior 04/12 00:43
10F:推 xatier:语法树! 推 04/12 07:36
11F:推 wanwan2:赞!! 04/12 08:40
12F:推 genghiskii:3F正解 04/12 10:08
13F:推 j094097:大推 04/12 21:23
14F:推 yauhh:都忘了这件事,果然imperative就怕太依赖state 04/12 21:40
15F:推 h520:用树讲解好清楚啊!! 大推 04/14 13:42