RSS Bot Posted May 28, 2009 Share Posted May 28, 2009 What do you think the following C should do? unsigned byte zmem[128*1024]; int read_word( int a, x ) { a = zmem[a++]<<8 + zmem[a]; ... } There are three (!) errors in the above code: precedence error : addition is done before shift order of operations : is a incremented before the second array reference? compiler bug : a contains a++ after the operation All three can be worked around by using a temporary variable. Here's another gotcha I ran into: int PC; void run( void ) { ... op2code( op & 31, zmem[PC++], zmem[PC++] ); ... } Looks reasonable, provide the next two bytes in the instruction stream as operands. Again, there are several potential bugs lurking: is PC incremented before or after the function call (tested - it's before, at least with my compiler) order of operations : which operand gets which byte? is actually PC incremented before the second array reference? #3 turned out to be not true - the same value was passed in for both operands. Again, recoding with temporary variables worked around the problem. In both of these cases, the code looks sane. The compiler doesn't flag them as errors. But the results aren't what you'd expect. http://www.atariage.com/forums/index.php?a...;showentry=6130 Link to comment Share on other sites More sharing options...
Recommended Posts