473,387 Members | 1,650 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,387 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 15820
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.