How do I set the size of an enumerated type?

Discussion to talk about software related topics only.
Post Reply
ddeham
Posts: 9
Joined: Tue Feb 24, 2009 7:37 am

How do I set the size of an enumerated type?

Post by ddeham »

Hi,

When I run this C program compiled with Dev-C++ 4.9.9.2, it gives the following output:
#include <stdio.h>
#include <stdlib.h>
#pragma enum=1
typedef enum {HUGHES = 0x01,TACHYON = 0x02,IDIRECT = 0x03,LINKSTAR = 0x04,GENERIC = 0x05} ModemBrand_Type;
int main(int argc, char *argv[])
{
ModemBrand_Type ModemBrand;
printf("sizeof(ModemBrand): %i",sizeof(ModemBrand));
getchar();
return 0;
}
#pragma enum=reset
-----------------------
sizeof(ModemBrand): 1

However, the Eclipse-Netburner Tools 2.6.2 says this when I try to use the pragma directive:
..\/flashmemory.h:22: warning: ignoring #pragma enum
..\/flashmemory.h:24: warning: ignoring #pragma enum

I also tried adding "-fshort-enums" on Properties / C/C++ Build / Settings / GNU C Complier / Miscellaneous / Other flags. The Netburner code still outputs this:
sizeof(ModemBrand): 4

How do I set the size of an enumerated type to one byte? HELP! Thanks.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: How do I set the size of an enumerated type?

Post by tod »

I don't know about DevC++ but I just tried it in Eclipse and the -fshort-enums works. It took the sizeof a standard C++ enum from 4 to 1. (Much to my surprise). Do you have compiler settings for C and a separate setting for C++ (Eclipse does). Did you set it for C++?

From the GCC manual
Warning: the -fshort-enums switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface.

[Editorial]I think the implication is that you don't use it to save three bytes, but there's nothing stopping you from making your code behave in unexpected ways. Not so much a problem for you, since you'll know what this flag means and that it is set, but if another programmer inherits your code they may take your name in vain. Also I have to wonder what happens if any arithmetic operations are performed on the enums.[/Editorial]
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: How do I set the size of an enumerated type?

Post by rnixon »

If you don't go with the default type of int for enum's you will have problems, at least with all the compilers I have used. Why do you have to have it as a byte?
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: How do I set the size of an enumerated type?

Post by rnixon »

Agree with Tod's editorial
ddeham
Posts: 9
Joined: Tue Feb 24, 2009 7:37 am

Re: How do I set the size of an enumerated type?

Post by ddeham »

rnixon wrote:If you don't go with the default type of int for enum's you will have problems, at least with all the compilers I have used. Why do you have to have it as a byte?
tod wrote:I don't know about DevC++ but I just tried it in Eclipse and the -fshort-enums works. It took the sizeof a standard C++ enum from 4 to 1. (Much to my surprise). Do you have compiler settings for C and a separate setting for C++ (Eclipse does). Did you set it for C++?
I was setting it in the wrong place (C, not C++). Once I set it in C++, it compiled with -fshort-enums. However, rnixon was right. I've got problems. That killed the web server and I don't know what else.
404 Not Found
The requested URL INDEX.HTM was not found on this server.

I really would prefer to have the
#pragma enum=1
#pragma enum=reset
work, just for my section of code. HOW DO I MAKE THIS HAPPEN? HELP!

Why do this? Why not? Googling shows that this is the most common reply to this question (why?). To save time and space, reduce program bloat, that's why! I have a lot of variables that get saved in the MOD5282's flash. The 8K user flash on our product gets backed up on another controller board via a serial connection at 9600 baud. The full 8K takes 10.5 seconds (there's some handshaking and checksum action going on). If I can reduce the space for the enum variables, I have more room to store other things and/or faster transfer times.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: How do I set the size of an enumerated type?

Post by rnixon »

Just my opinion, but the "why not" is that it is compiler specific and not immediately intuitive to someone else looking at your code down the road. For example, short and int types are different sizes on different platforms.

I certainly understand the desire to have things be small if saving to an 8k flash space (I didn't know that before). My own preference would be to not use enum and declare them individually. But I know everyone prefers to do things in their own way.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: How do I set the size of an enumerated type?

Post by tod »

ddeham,

If I had to save space I would do something like the following

Code: Select all

enum ModemType{HUGHES = 0x01,TACHYON ,IDIRECT ,LINKSTAR,GENERIC};

void SomeMethod()
{
    int8_t  smallInstanceOfModemType = HUGHES;
}
//OR
void SomeMethod (ModemType theModem)
{
    int8_t one_byte_flash_var = theModem; //Actually I would put in an explicit static_cast<int8_t> but it works without it, just less explicit
}
I don't see any support for the #pragma you mention in the GCC documentation for the 4.2.1 version of the compiler.

Tod
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: How do I set the size of an enumerated type?

Post by tod »

I just couldn't let this go for whatever weird reason. Try this:

Code: Select all

enum __attribute__((packed)) ModemType{HUGHES = 0x01,TACHYON ,IDIRECT ,LINKSTAR,GENERIC};
In my test it returned a size of 1.

I find this much better than a compiler directive. It's very clear to a maintenance programmer that something's going on here.

Tod
ddeham
Posts: 9
Joined: Tue Feb 24, 2009 7:37 am

Re: How do I set the size of an enumerated type?

Post by ddeham »

tod wrote:I just couldn't let this go for whatever weird reason. Try this:

Code: Select all

enum __attribute__((packed)) ModemType{HUGHES = 0x01,TACHYON ,IDIRECT ,LINKSTAR,GENERIC};
That worked! Thanks! I was using it as a typedef, so I just added __attribute__ ((packed)) to the typedef, and now it is one byte.
typedef enum ModemBrand_Type {HUGHES = 0x01, TACHYON = 0x02, IDIRECT = 0x03, LINKSTAR = 0x04, GENERIC = 0x05} __attribute__ ((packed));
Post Reply