#132141 - simonjhall - Sat Jun 23, 2007 9:13 pm
Is there any way to force the type and size of an enumation in C, via some compiler option?
One thing I noticed in Quake was that the code (compiled with VS) expected four-byte enums (even if they've only got enough entries to warrant a single byte).
Also with this MPEG stuff there seems to be a variation between building with ARM and THUMB!
Ok, so the code has an enum in it,
but at one point (so far) in the code it's taken -1 and casted it to mpeg2_state_t, and returned it as type mpeg2_state_t. Then the code tests to see if the value is negative and does something etc etc.
On the PC with Visual Studio I get -1 'correctly' returned.
On the DS with gcc -marm I get -1.
On the DS with gcc -mthumb I get 255.
To handle the thumb version I added a dummy entry into the enumeration with value -1 and then I get a correct -1 back out (rather than 255).
So same compiler, difference results. Is there any way to get consistent results? I know you shouldn't make assumptions about the size of enumerated types and also their values in undefined cases, but there's gotta be something that can be done so as not to catch out other people porting code!
Peace.
_________________
Big thanks to everyone who donated for Quake2
One thing I noticed in Quake was that the code (compiled with VS) expected four-byte enums (even if they've only got enough entries to warrant a single byte).
Also with this MPEG stuff there seems to be a variation between building with ARM and THUMB!
Ok, so the code has an enum in it,
Code: |
typedef enum {
STATE_BUFFER = 0, STATE_SEQUENCE = 1, STATE_SEQUENCE_REPEATED = 2, STATE_GOP = 3, STATE_PICTURE = 4, STATE_SLICE_1ST = 5, STATE_PICTURE_2ND = 6, STATE_SLICE = 7, STATE_END = 8, STATE_INVALID = 9, STATE_INVALID_END = 10 } mpeg2_state_t; |
but at one point (so far) in the code it's taken -1 and casted it to mpeg2_state_t, and returned it as type mpeg2_state_t. Then the code tests to see if the value is negative and does something etc etc.
On the PC with Visual Studio I get -1 'correctly' returned.
On the DS with gcc -marm I get -1.
On the DS with gcc -mthumb I get 255.
To handle the thumb version I added a dummy entry into the enumeration with value -1 and then I get a correct -1 back out (rather than 255).
So same compiler, difference results. Is there any way to get consistent results? I know you shouldn't make assumptions about the size of enumerated types and also their values in undefined cases, but there's gotta be something that can be done so as not to catch out other people porting code!
Peace.
_________________
Big thanks to everyone who donated for Quake2