When you spend a full day tracking down a bug :-( Bug in SDCC 4.0?
You trust the compiler - in 99,99% of the time the problem is in your own code. And I'm no expert in C, but current behaviour is strange. It originates from my current project of about 8000 lines of code, and was really hard to find. I have isolated the problem, and it goes like this:
#include typedef struct { unsigned char uFileIndex[ 1 ]; } SpriteMap; #define HOW_MANY 10 void pr( unsigned char x ) { printf( "Value: %d\r\n", x ); } void doStuff( SpriteMap p[] ) { unsigned char n; /* unsigned char x = p[ 5 ].uFileIndex[ 0 ]; for( n=0;n<4;n++ ) pr( x ); */ for( n=0;n<4;n++ ) pr( p[ 5 ].uFileIndex[ 0 ] ); } void main(void) { unsigned char n; SpriteMap p[ HOW_MANY ]; for( n<0;n<HOW_MANY;n++ ) // just fill in some values p[ n ].uFileIndex[ 0 ] = n; doStuff( p ); }
So, I have 10 SpriteMap-objects. They each hold a value equal to their index. So, if I want to get the value from the fifth object, I expect to get 5. If I get that value multiple times, I expect to get 5 each time. In my sample, I try to get the value 4 times. I also print it. Here is the result:
Value: 5 Value: 0 Value: 0 Value: 224
If I uncomment the part where I put the value in a variable (x), I get this (correct) output:
Value: 5 Value: 5 Value: 5 Value: 5 Value: 5 Value: 5 Value: 5 Value: 5
I have converted the code to use pointers instead of arrays - same result. If I put the "SpriteMap" as global variable, and not pass as parameter, I get all 5's (correct). If I replace the function-call to "pr" with just printf, I get all 5's (correct).
This sounds crazy to me. I had to spend time in the openmsx debugger to find this (after hours of moving things around).
So either I'm doing something wrong, or SDCC is.