enum allocated size

Discussion in 'C Programming' started by Jonathan Avraham, Mar 30, 2005.

  1. 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.
     
    Jonathan Avraham, Mar 30, 2005
    #1
    1. Advertising

  2. (Jonathan Avraham) writes:
    > 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?

    [...]

    We don't know. If gcc's (rather extensive) online documentation
    doesn't help, try gnu.gcc.help.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 30, 2005
    #2
    1. Advertising

  3. Hi, Jonathan.

    Maybe, we can make it in this way:

    struct protocol_Message{
    ....
    enum XXX field: 16;
    ....
    }

    Best Regards,

    Xiangliang Meng


    "Jonathan Avraham" <> wrote in message
    news:...
    > 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.
     
    Xiangliang Meng, Apr 1, 2005
    #3
  4. "Xiangliang Meng" <> writes:
    > Maybe, we can make it in this way:
    >
    > struct protocol_Message{
    > ...
    > enum XXX field: 16;
    > ...
    > }


    Please don't top-post.

    The above is not portable. The only allowed types for a bit field are
    signed int, unsigned int, int (which may be treated as either signed
    or unsigned), and (in C99) _Bool. Some compilers may allow enums as
    bit fields as an extension.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 1, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    549
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    470
    Rolf Magnus
    Nov 13, 2003
  4. Replies:
    5
    Views:
    623
    Matt Wharton
    Dec 9, 2004
  5. mrhicks
    Replies:
    2
    Views:
    427
    Dave Thompson
    Jun 10, 2004
Loading...

Share This Page