简体   繁体   English

一个编译器中的运算符优先级不同,为什么?

[英]Operator precedence different in one compiler, why?

I'm porting my game to Android which has around 200k lines of code and encountered a very strange bug, after 5 hours of digging I cracked it down to this line: 我将游戏移植到Android上,该游戏具有约20万行代码,并遇到了一个非常奇怪的错误,经过5个小时的挖掘,我将其分解为以下一行:

// background info
short m = ((dwMask << 4) | dwMask) << ASTAR_OFFSET_VIS;
short zm = (~ASTAR_MASK_DISCOVERED) | (~dwMask);
short *b = (short*)tilecache.GetDataPtr() + index;

// unexpected behavior
*b++ = (*b&zm) | m;

// works
*b = ((*b)&zm) | m; b++;

My guess is this compiler (GCC ARM) treats the ++ operator differently than all other compilers I've built this game on, which seems a little crazy, but not unbelievable. 我的猜测是,此编译器(GCC ARM)对++运算符的处理方式与我在其上构建此游戏的所有其他编译器不同,这似乎有些疯狂,但并不令人难以置信。 Previously the game has been built for Windows, Mac, iOS, and Windows CE and all processed the top version fine. 以前,该游戏是为Windows,Mac,iOS和Windows CE构建的,并且都可以处理顶级版本。

I think it's evaluating (*b&zm) | 我认为它正在评估(* b&zm)| m then incrementing the pointer b++ then doing the assignment, because data is shifted left in my array each evaluation. m然后增加指针b ++,然后进行赋值,因为每次求值时,数据在我的数组中向左移动。

I have located numerous places in my code that use this type of syntax and changed them, but I want to make sure the problem is what I'm thinking it is, and if this is a compiler option I could switch to make it the same as the other compilers I use? 我在代码中找到了许多使用这种类型的语法的地方,并进行了更改,但是我想确保问题出在我的想像中,如果这是一个编译器选项,我可以进行切换以使其相同和我使用的其他编译器一样? In case I have other syntax like this in my code elsewhere. 如果我在其他地方的代码中有其他这样的语法。

The order in which arguments to functions or operators (including built-in operators) are evaluated is unspecified. 未指定对函数或运算符(包括内置运算符)的参数进行评估的顺序。 Some compilers will evaluate the expressions from left to right, others evaluate them from right left. 一些编译器将从左到右评估表达式,而其他编译器则从右左评估它们。 For some operators, eg, the comma, the ternary, and the logic operators the order in which the arguments are evaluated and when side-effects happen is specified: the first operand is evaluated first. 对于一些运营商,例如,逗号,三元,和逻辑运算符,其中所述参数进行评估和当指定的副作用事件发生的顺序:第一操作数被第一评价。

This is unrelated to operator precedence which determines in which order the operators are evaluated. 这与运算符优先级无关,后者确定运算符的评估顺序。 It is also unspecified when side-effects happen other than that they happen at the end of a full expression or after evaluating the first argument of one of the special operators. 除了在完整表达式的末尾或在评估其中一个特殊运算符的第一个参数之后发生的副作用以外,还没有确定是否发生副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C ++编译器在这个简单的程序中没有给出优先级(赋值下的递增运算符)? - Why the C++ compiler does not give precedence (increment operator under assignment) in this simple program? 在一条指令中声明更多指针的运算符优先级 - Operator Precedence in declaring more pointers in one instruction 为什么编译器可以找到这些运算符重载之一,却找不到另一个? - Why can the compiler find one of these operator overloads but not the other? 运营商优先权 - Operator Precedence ++和&amp;&amp;的运算符优先级 - Operator precedence of ++ and && 条件运算符中的运算符优先级 - Operator Precedence in Conditional Operator 为什么在这种情况下括号无法更改 c++ 运算符优先级? - why parenthesis is unable to change c++ operator precedence in this case? 为什么在表达式解析器中`TokPrec &lt; NextPrec` 时运算符优先级必须增加 1? - Why operator precedence must be increased by 1 when `TokPrec < NextPrec` in expression parser? 为什么我的“while”循环没有结束? 是由于运算符优先级还是其他原因? - Why is my 'while' loop not ending? Is it due to operator precedence, or anything else? C / C ++编译器如何根据运算符的优先级和关联性分隔标记? - C/C++ How does compiler separate tokens according to operator's precedence and associativity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM