473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
Nov 14 '05 #1
3 5090
sh**********@gmail.com (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) ks***@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.
Nov 14 '05 #2
Hi, Jonathan.

Maybe, we can make it in this way:

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

Best Regards,

Xiangliang Meng
"Jonathan Avraham" <sh**********@gmail.com> wrote in message
news:72**************************@posting.google.c om...
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.

Nov 14 '05 #3
"Xiangliang Meng" <xi*********@yahoo.com> 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) ks***@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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
7
by: wogston | last post by:
A) template <typename scalar, int size> struct basevector { enum { size = size }; scalar v; }; B)
7
by: Aryeh M. Friedman | last post by:
If have something like the following declartion: enum foo {One,Two,....,Ten}; How do I determine how many elements (enumerations)... in this case it is obviously 10 but I don't want to hard...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
5
by: Francois Grieu | last post by:
Can an enum be used as an array size? In other word, is this legal? enum {n=1}; int a; int main(void){return a;} TIA
5
by: nmtoan | last post by:
Hi, I could not find any answer to this simple question of mine. Suppose I have to write a program, the main parts of it are as follows: #include <blahblah.h> struct {
1
by: PSN | last post by:
can any one tell me if there is a way to redeclare a typedef enum to add new elements at the end .. Ex: "test.h" typedef enum test1 { hcone = 0, hctwo hcthree, hcfour,
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.