cunixhaskellffic2hs

Large c2hs-inferred size of C enum


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?


Solution

  • 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
    {
        // ...
    };