What is the meaning of _BIG_ENUM=0xFFFFFFFF, in the last of the enum?

2

I am reading some codes and I found this _BIG_ENUM=0xFFFFFFFF in the last value of the enum.what is the correct meaning of this line. And this _BIG_ENUM is not used anywhere in the code;

#define CLS_COMM_CONFIG PORT_0,BAUD_9600,DATA_BIT_8,ONE_STOP,NO_FLOW_CONTROL
#define PLS_COMM_CONFIG PORT_1,BAUD_115200,DATA_BIT_8,ONE_STOP,NO_FLOW_CONTROL

typedef enum _comm_config
{

 _Zero=0,
 PORT_0=_Zero,
 BAUD_2400=_Zero,
 NONE=_Zero,
 HALF_STOP=_Zero,
 DATA_BIT_5=_Zero,
 NO_FLOW_CONTROL=_Zero,

 _One = 1,
 PORT_1=_One,
 BAUD_4800=_One,
 ODD=_One,
 ONE_STOP=_One,
 DATA_BIT_6=_One,

 _Two=2,
 PORT_2=_Two,
 BAUD_9600=_Two,
 EVEN=_Two,
 TWO_STOP=_Two,
 DATA_BIT_7=_Two,

 _Three=3,
 PORT_3=_Three,
 BAUD_19200=_Three,
 DATA_BIT_8=_Three,

 _Four=5,
 PORT_5=_Four,
 BAUD_115200=_Four,
 DATA_BIT_9=_Four,

 _BIG_ENUM=0xFFFFFFFF,

}COMMConfig;
c
enums
asked on Stack Overflow Oct 17, 2019 by Bhura • edited Oct 17, 2019 by Bhura

2 Answers

5

It doesn't make any sense and is a bug.

I suppose the programmer didn't quite know how enums work and thought they could enforce enums to become 32 bit by assigning a large integer constant to one of the enumeration constants. This is true, but they picked a bad value which won't work as they thought it would.

The problem is that while enumeration variables may have implementation-defined sizes, enumeration constants such as _BIG_ENUM are always of type int 1).

But 0xFFFFFFFF won't fit in a 32 bit int so this is a bug. The hex constant 0xFFFFFFFF is actually of type unsigned int (assuming 32 bit) and it won't fit, so there will be an implementation-defined conversion from signed to unsigned. Meaning we end up with the value -1 on 2's complement systems. Although gcc and clang with strict standard settings even refuse to compile the code when the enumeration constant is given an integer constant larger than INT_MAX.

When faced with an enumeration constant of value -1, the compiler is free to pick any signed type2) for enumeration variables of that type, not necessarily a 32 bit one.

The code can be fixed by changing it to _BIG_ENUM=INT_MAX, (limits.h). Then the enumerated type will either become int or unsigned int.


1) C17 6.7.2.2/1

The expression that defines the value of an enumeration constant shall be an integer constant expression that has a value representable as an int.

2) C16 6.7.2.2/4

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined,128) but shall be capable of representing the values of all the members of the enumeration.

128) An implementation may delay the choice of which integer type until all enumeration constants have been seen.

answered on Stack Overflow Oct 17, 2019 by Lundin • edited Oct 17, 2019 by Lundin
2

Some compilers can optimize enum to smaller word-sizes, but this could cause problem with interoperability when not all of the code is compiled with the same compiler.

If an enum is assigned a 32-bit value this optimization is prevented, forcing this enum to be encoded as a 32 bit integer.

answered on Stack Overflow Oct 17, 2019 by Simson • edited Oct 17, 2019 by Simson

User contributions licensed under CC BY-SA 3.0