HELP: fixing the GCC enum storage size

From: kindsol (kindsol_at_hotmail.com)
Date: 04/29/04


Date: Wed, 28 Apr 2004 18:12:29 -0700

I have a problem with GCC 3.2 that I don't have with my MSVC 6.0 compiler.

My library has over 450 enumerations in it, almost all start with an
enumerator of -1. These enumerations can be members of structures that get
written to symmetrical memory structures on a hardware device. My library
(incorrectly) assumes that all enumerations will be 32bits in size.

I have found that if an enum contains a -1 and a positive integer (in my
case a bit mask) with bit 31 set, then the GCC compiler promotes my
enumeration storage size from a 32bit integer to a 64bit integer.

For example with a -1 and bit31 enumerators in my enum...
    typedef enum {
      TESTValueONE = -1,
      TESTValueTWO,
      TESTValueTHREE = 0x80000000,
    } TESTValue;

...I get 64bit values...

    bytes:64 value:0xffffffff
    bytes:64 value:0x0
    bytes:64 value:0x80000000

However if the -1 or the value with bit 31 set is removed... (here I remove
the bit31 value)

    typedef enum {
      TESTValueONE = -1,
      TESTValueTWO,
      TESTValueTHREE,
    } TESTValue;

I get 32bit values...
    bytes:32 value:0xffffffff
    bytes:32 value:0x0
    bytes:32 value:0x1

I have found plenty of information why this happens and I think that I
understand why this happens.

My problem is that this offsets my host's structure alignment so it will no
longer match the hardware's memory alignment.
I hoping to find solution that is more elegant than changing 450
enumerations in my library (not a simple search-and-replace)

Is there anyway to force GCC to treat all enums as a 32bit integer? (i.e.
compiler switch, gcc build option, gcc src code change?)

Any insight would be much appreciated!!

Thank you for your time,
        Kindsol

FYI: gcc version string: gcc (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

Here is my test code:

#include <stdlib.h>

typedef enum {
  TESTValueONE = -1,
  TESTValueTWO,
  TESTValueTHREE,
} TESTValue;

int main() {
  printf("bytes:%d\tvalue:0x%lx\n", sizeof(TESTValueONE)*8,TESTValueONE);
  printf("bytes:%d\tvalue:0x%lx\n", sizeof(TESTValueTWO)*8,TESTValueTWO);
  printf("bytes:%d\tvalue:0x%lx\n",
sizeof(TESTValueTHREE)*8,TESTValueTHREE);

  return (0);
}



Relevant Pages