1
Septembers 2015-05-22 18:46:17 +08:00
cat /proc/cpuinfo
|
2
ShadowStar 2015-05-22 19:05:44 +08:00
未定义
|
3
choury 2015-05-22 19:20:09 +08:00 via Android
一个语句放两个++,结果是不确定的,和编译器实现有关
|
4
acros 2015-05-22 19:24:06 +08:00
这不是触发短路求值了么?
我去VS里面验证下,目前没gcc···· |
5
acros 2015-05-22 19:30:48 +08:00
木有试出来······
三楼说的应该是对的,我记得code warrior编译器对这种的处理就和vs相反的。 |
6
xylophone21 2015-05-22 19:33:00 +08:00 1
只读了第一个字节,并没有读第二个字节。这个怎么解释?
0x12 0x34 ==> 0x1200? 0x0034? 0x3412? 1. *(p->pointer++) << 8 溢出? 2. C规范定义中没有说|操作的左右顺序,但也没有说行为是未定义的。不过讲||时,它用了一个unlike,这种东西跟优先级差不多,虽然有规则,但没几个人记得住,用()规避的好。 6.5.12 Bitwise inclusive OR operator Syntax 1 inclusive-OR-expression: exclusive-OR-expression inclusive-OR-expression | exclusive-OR-expression Constraints 2 Each of the operands shall have integer type. Semantics 3 The usual arithmetic conversions are performed on the operands. 4 The result of the | operator is the bitwise inclusive OR of the operands (that is, each bit in the result is set if and only if at least one of the corresponding bits in the converted operands is set). 6.5.14 Logical OR operator Syntax 1 logical-OR-expression: logical-AND-expression logical-OR-expression || logical-AND-expression Constraints 2 Each of the operands shall have scalar type. Semantics 3 The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int. 4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated. |
7
xylophone21 2015-05-22 19:35:23 +08:00
|
8
leeyiw OP @xylophone21 我的意思是pointer只自增了1
|
9
leeyiw OP @xylophone21 你说的是逻辑或,我说的是按位或。
|
11
xylophone21 2015-05-22 19:48:28 +08:00
@leeyiw 这没有道理,除非你把 “|” 写成了 "||" 。
|
12
leeyiw OP @xylophone21 写的确实如上面代码所述,所以感觉很诡异
|
13
choury 2015-05-22 20:21:11 +08:00 via Android
|
14
zhicheng 2015-05-22 20:26:05 +08:00 via Android
教科书似的“未定义行为”。
|
15
Monad 2015-05-22 22:46:27 +08:00
§5/4.1 Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.
另外这种东西不应该是直接(int16_t*)p->pointer然后ntohs最后p->pointer += 2么 |
17
xylophone21 2015-05-23 22:42:26 +08:00
@Monad 他的数据是bigend,而现在的cpu以littleend的居多,你确定这样可以?
|
18
xylophone21 2015-05-23 22:43:04 +08:00
@leeyiw 那只能建议看汇编了
|
19
Monad 2015-05-23 22:58:17 +08:00
@xylophone21 就因为是Big Endian所以采用ntohs了, Network Byte Order 本来就是Big Endian的嘛
|