enum allocated size

J

Jonathan Avraham

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.
 
K

Keith Thompson

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.
 
X

Xiangliang Meng

Hi, Jonathan.

Maybe, we can make it in this way:

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

Best Regards,

Xiangliang Meng
 
K

Keith Thompson

Xiangliang Meng said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top