Size of enum‏

David Brown david.brown@hesbynett.no
Sun Feb 5 20:51:00 GMT 2012


On 05/02/12 17:29, Alexandre Almeida wrote:
>
> What do you think about making enum types have only the size needed
> for the number of constants held? If an enum type has 256 constants
> or less, for example, it needs only one byte. If it has between 257
> and 65536 constants, in the other hand, it needs two bytes.
>

I think you have the wrong mailing list - this is for the development of 
the gcc compiler, not for development /using/ gcc.  You probably want 
gcc-help@gcc.gnu.org.

I'm sure someone will correct me if my details are wrong, but my 
understanding is this:

Enum types in C are all "int" - "signed int" by default, or "unsigned 
int" if one or more elements requires the range of unsigned int, or if 
one or more has an unsigned int suffix (e.g., "1u").

Enum types in C++ can be any integer type big enough to cover the 
required range.  I think most C++ compilers use the smallest integer 
type that covers the range.

gcc has a switch "-fshort-enums" that makes the compiler use the 
smallest usable integer type for its enums, which is the effect you are 
looking for.  Be careful when mixing modules (including libraries) with 
different settings for this flag - enum types will be incompatible 
between them.





More information about the Gcc mailing list