C Enum warnings

I am updating my C code physics simulator to use enums from long #defines, but I am running into an odd error. The structure contains an enum as:

enum spec_mod_type_enum 
{ SPEC_MOD_PL=1, 
  SPEC_MOD_EXP=2,
} spec_mod_type[NXBANDS];

      

(NXBANDS is just a #defined value)

Due to an oversight, no key is added for -1, and in another file it changes as:

xplasma->spec_mod_type[n] = -1;

      

However, when compiling both clang and gcc, it crashes silently; the value is set to undefined, not -1, with unpleasant consequences. It's odd:

  • I could set values ​​outside of their range under the impression metrics.

  • We don't get any warnings about this when using -Wall (or -Wextra) when it seems like the exact enumeration of things should be warned.

Can anyone enlighten me as to why this might be happening? And / or what compiler flags would warn us about this, or at least change the default behavior for enums to allow this set?

+3


source to share


2 answers


The behavior of your program can vary from platform to platform:

The C standard allows the compiler to choose some basic integral type for the counter that is capable of displaying all the explicit values: 1 and 2 in your case.

Thus, the compiler can choose the unsigned type for your enum. Assigning a negative value in this case will result in a 2 ^ n bypass, where n is the number of bits used to represent the unsigned type.



Alternatively, he can choose a signed type, in which case -1 will be representable.

One means would be to introduce a negative dummy value into your counter.

+6


source


I see your problem. I think it is initialized to undefined -1 before it gets set to another enum value, right? I would recommend adding one more enum value SPEC_MOD_UNDEFINED. Then you can first initialize this first and then install another.

Also, if you want to iterate over all the values ​​of an enumeration, it will be useful to add NUM_SPEC_MOD at the end, which will then take number one above the previous one.

Then you can do

for(int i = SPEC_MOD_PL; i < NUM_SPEC_MOD;i++)

      



for all values. And only if necessary, assign specific values ​​to enumerations, this automatically does it on its own. But seeing that you are modifying the existing code, I can see why you might want to add certain numbers. For my example, the numbers must be integers so that all numbers refer to a particular state.

This works because C provides an integer representation of enumeration values ​​directly to the programmer. Integer and enumerated values ​​can be mixed freely, and all arithmetic operations on enumerated values ​​are allowed. Even though the enum variable can contain an integer that does not represent any of the enumeration values. In fact, according to the language definition, your code will define SPEC_MOD_PL and SPEC_MOD_EXP as constants of type int, which will only be converted (silently) to enum spec_mod_type if stored in a variable of that type.

( from Wikipedia Article with an enumerated type )

0


source







All Articles