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. 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
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.
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.
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
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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....???
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |