473,326 Members | 1,972 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,326 software developers and data experts.

sizeof(enum) == sizeof(int) ???

A simple struct

struct Foo {
float x;
char y;
};

The above class occupies 8 bytes on my Win32 system
according to sizeof(Foo). This makes sense because
of the platform's alignment requirements. However,
I can force the struct to be packed by using a non-
standard compiler extension (in my case GCC's "packed"
attribute):

struct Foo {
float x;
char y;
} __attribute__((packed));

Now sizeof(Foo) is 5 bytes. However, when I replace
char y with an enum, say

struct Foo {
float x;
enum { Y } y;
};

the sizeof(Foo) remains 8 whether I pack it or not.
I get the same behavior with MSVC's #pragma pack.

Is an enum always the same size as an int? I thought
an enum was supposed to be as small as needed to hold
all of its values, in this case 1 byte.
Jul 22 '05 #1
7 15799
Derek wrote:
A simple struct

struct Foo {
float x;
char y;
};

The above class occupies 8 bytes on my Win32 system
according to sizeof(Foo). This makes sense because
of the platform's alignment requirements. However,
I can force the struct to be packed by using a non-
standard compiler extension (in my case GCC's "packed"
attribute):

struct Foo {
float x;
char y;
} __attribute__((packed));

Now sizeof(Foo) is 5 bytes. However, when I replace
char y with an enum, say

struct Foo {
float x;
enum { Y } y;
};

the sizeof(Foo) remains 8 whether I pack it or not.
I get the same behavior with MSVC's #pragma pack.

Is an enum always the same size as an int?
No. But the compiler has the final say in that, not you, apparently.
I thought
an enum was supposed to be as small as needed to hold
all of its values, in this case 1 byte.


No, it's up to the implementation to use the type it deems appropriate.
It has to be big enough to hold all the values, but nothing is said that
it has to be no bigger.

Victor
Jul 22 '05 #2
187
Victor Bazarov wrote:
Derek wrote:
A simple struct

struct Foo {
float x;
char y;
};

The above class occupies 8 bytes on my Win32 system
according to sizeof(Foo). This makes sense because
of the platform's alignment requirements. However,
I can force the struct to be packed by using a non-
standard compiler extension (in my case GCC's "packed"
attribute):

struct Foo {
float x;
char y;
} __attribute__((packed));

Now sizeof(Foo) is 5 bytes. However, when I replace
char y with an enum, say

struct Foo {
float x;
enum { Y } y;
};

the sizeof(Foo) remains 8 whether I pack it or not.
I get the same behavior with MSVC's #pragma pack.

Is an enum always the same size as an int?


No. But the compiler has the final say in that, not you, apparently.
> I thought
an enum was supposed to be as small as needed to hold
all of its values, in this case 1 byte.


No, it's up to the implementation to use the type it deems
appropriate. It has to be big enough to hold all the values, but
nothing is said that it has to be no bigger.


Actually I do believe enums are usually considered to be an integral
type, so if thats always so, then the subject header is correct.
Jul 22 '05 #3
Derek wrote:

Is an enum always the same size as an int? I thought
an enum was supposed to be as small as needed to hold
all of its values, in this case 1 byte.


Enum is based on some implementation-defined integral
type with the only caveat in that it shall not be larger
than sizeof(int) unless the enumerators require a larger
range than int can support.

It's perfoectly acceptable for a compiler to use int for
all enums with values that can fit into one.
Jul 22 '05 #4
187 wrote:
[..]
Actually I do believe enums are usually considered to be an integral
type, so if thats always so, then the subject header is correct.


Enums considered integral? By whom? How usual is that misconception?
Integral types are (and please memorize this):

signed char |
short int | These are signed
int | integer types
long int |
unsigned char |
unsigned short int | These are unsigned
unsigned int | integer types
unsigned long int |
bool
char
wchar_t

(eleven types total). There are no more integral types. Enumerations
are _compound_ types.

V
Jul 22 '05 #5
Victor Bazarov wrote:
(eleven types total). There are no more integral types. Enumerations
are _compound_ types.

While enums are not integral types, they are closely allied with them.
Each enum has an "underlying type" which is one of the integral types
which specifies how the enumerators are encoded.
Jul 22 '05 #6
Ron Natalie wrote:
Victor Bazarov wrote:
(eleven types total). There are no more integral types. Enumerations
are _compound_ types.

While enums are not integral types, they are closely allied with them.
Each enum has an "underlying type" which is one of the integral types
which specifies how the enumerators are encoded.


I don't contest close relationship or existence of an underlying type.
It wasn't my point.
Jul 22 '05 #7
Victor Bazarov wrote:

I don't contest close relationship or existence of an underlying type.
It wasn't my point.


My point isn't to argue with you, but to increase the understanding of
the person who posed the original question. While there was nothing
factually wrong with your answer, I felt it an incomplete answer.
Jul 22 '05 #8

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

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
28
by: Richard Cavell | last post by:
Hi, Is there some kind of canonical list, or would someone like to give a brief rundown, as to: sizeof(int) sizeof(long int) sizeof(long long) etc
2
by: blufox | last post by:
I read somewhere that standard Ansi C on 32 bit architecures assumes a lot of things which lead to poor code. For example sizeof( int ) = sizeof( void * ) sizeof( long ) = sizeof( int ) Why...
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...
40
by: Spiros Bousbouras | last post by:
Do you have an example of an implementation where sizeof(short int) does not divide sizeof(int) or sizeof(int) does not divide sizeof(long int) or sizeof(long int) does not divide sizeof(long long...
5
by: aarklon | last post by:
Hi all, why printf("....%d",sizeof((int)(double)(char) i)) always gives the size of int ??? is it because sizeof doesn't evaluate its operand....???
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.