enum allocated size

From: Jonathan Avraham (shambler.com_at_gmail.com)
Date: 03/30/05


Date: 29 Mar 2005 23:04:03 -0800

Hi,

If you don't want to read the whole story:
I'm asking - Can I force gcc to treat all enums as 2-byte integers?

If you need more details - read on:
I'm trying to write an embedded program in C that communicates over ip and
uses a well defined protocol in gcc (currently I'm compiling for
WindRiver's VxWorks simulator, the system will eventually run on mips
architecture). In this protocol, some of the messages are defined as
structs which have fields that are enumerated. I need these fields to
take two bytes - this is what the protocol requires. However, by
default, gcc allocates 4 bytes per enum. Using -fshort-enums I can
tell it to automatically determine the required size, but this is
again a problem - most of the enums have less than 256 literals, which
means they will take only 1 byte instead of 2 when allocated.
Ofcourse, I can use somewhat ackward workarounds by defining another
literal such as LAST = 0xFFFF or just defining those structs with
16-bit integers instead of enums, but I would much prefer using a
compiler directive/command line option which will force enums to use 2
bytes, regardless of required size, if one exists. I know this kind of
option exists in other compilers I worked with in the past, such as
diab or even bcc (if I'm not mistaken) but I haven't been able to find
such an option in gcc.

Does anyone know of such option?

Thanks,
Jonathan Avraham.



Relevant Pages

  • Re: enum allocated size
    ... Best Regards, ... > I'm asking - Can I force gcc to treat all enums as 2-byte integers? ... > structs which have fields that are enumerated. ...
    (comp.lang.c)
  • Re: assignment of integer to enumerated value
    ... John Goche wrote: ... The following program compiler and runs fine under gcc and produces ... Enums are broken in C, so it is legal to assign a int ...
    (comp.lang.c)
  • Re: malloc call not returning
    ... it checks some missing type-casts and warns me if I switch the usage of ... booleans and enums. ... The gcc does ...
    (comp.lang.c)
  • Re: [PATCH 1/4] NTFS: Fix sparse warnings that have crept in over time.
    ... Or just make those two different enums. ... Basically, gcc is hopelessly ... b0rken in version-dependent way when it comes to multi-element enums ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Enumerations as case labels
    ... > I'm using enums as case labels throughout my code, ... when compiling it with the compiler that ships ... should only work inside the class member function (since there probably ...
    (comp.lang.cpp)