Large c2hs-inferred size of C enum

73 Views Asked by At

When writing the Storable instance of a C enum that has 5 options (using c2hs), the {# sizeof #} macro returns 4 (i.e. 4 bytes). Isn't this extremely wasteful, when 3 bits would suffice? Does this depend on the size of a memory word?

1

There are 1 best solutions below

0
AlexD On BEST ANSWER

The size of enum is implementation-defined. The standard says:

6.7.2.2 Enumeration specifiers
...

Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined ...

BTW, in C++ one could specify the underlying type explicitly, e.g.:

enum E : int
{
    // ...
};