enum allocated size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Avraham

    #1

    enum allocated size

    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.
  • Keith Thompson

    #2
    Re: enum allocated size

    shambler.com@gm ail.com (Jonathan Avraham) writes:[color=blue]
    > 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?[/color]
    [...]

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

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <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.

    Comment

    • Xiangliang Meng

      #3
      Re: enum allocated size

      Hi, Jonathan.

      Maybe, we can make it in this way:

      struct protocol_Messag e{
      ....
      enum XXX field: 16;
      ....
      }

      Best Regards,

      Xiangliang Meng


      "Jonathan Avraham" <shambler.com@g mail.com> wrote in message
      news:72fbb9fc.0 503292304.7c4d0 b93@posting.goo gle.com...[color=blue]
      > 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.[/color]


      Comment

      • Keith Thompson

        #4
        Re: enum allocated size

        "Xiangliang Meng" <xiangliangm@ya hoo.com> writes:[color=blue]
        > Maybe, we can make it in this way:
        >
        > struct protocol_Messag e{
        > ...
        > enum XXX field: 16;
        > ...
        > }[/color]

        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_Keit h) kst-u@mib.org <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.

        Comment

        Working...