Char bit fields

A

Alex Vinokur

struct Foo
{
unsigned char : 2;
unsigned char : 6;
};

What is correct sizeof(Foo) ?

Four compilers:
aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
Intel(R) C++ Version 11.0.083
Microsoft C/C++ 14.00.50727
produce sizeof(Foo) == 1

IBM XL C/C++ Enterprise Edition V8.0 for AIX Version: 08.00.0000.0014
IBM XL C/C++ Enterprise Edition for AIX, V9.0 Version: 09.00.0000.0008
produce sizeof(Foo) == 4.

Alex Vinokur
 
A

Alf P. Steinbach

* Alex Vinokur:
struct Foo
{
unsigned char : 2;
unsigned char : 6;
};

What is correct sizeof(Foo) ?

At least 1.

Four compilers:
aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
Intel(R) C++ Version 11.0.083
Microsoft C/C++ 14.00.50727
produce sizeof(Foo) == 1

IBM XL C/C++ Enterprise Edition V8.0 for AIX Version: 08.00.0000.0014
IBM XL C/C++ Enterprise Edition for AIX, V9.0 Version: 09.00.0000.0008
produce sizeof(Foo) == 4.

So?


Cheers,

- Alf
 
R

Richard Herring

In message
Alex said:
struct Foo
{
unsigned char : 2;
unsigned char : 6;
};

What is correct sizeof(Foo) ?

Anything greater than 0.
Four compilers:
aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
Intel(R) C++ Version 11.0.083
Microsoft C/C++ 14.00.50727
produce sizeof(Foo) == 1

IBM XL C/C++ Enterprise Edition V8.0 for AIX Version: 08.00.0000.0014
IBM XL C/C++ Enterprise Edition for AIX, V9.0 Version: 09.00.0000.0008
produce sizeof(Foo) == 4.
It's hardly surprising that alignment and padding requirements of
different architectures tend to differ.
 
J

James Kanze

struct Foo
{
unsigned char : 2;
unsigned char : 6;
};
What is correct sizeof(Foo) ?

Either implementation defined or unspecified, I'm not sure
which. (Implementation defined means that the implementation
must document what it does---in this case, how it lays out bit
fields, etc. Unspecified frees the implementation of the
documentation requirement.)
 
J

Jerry Coffin

Either implementation defined or unspecified, I'm not sure
which. (Implementation defined means that the implementation
must document what it does---in this case, how it lays out bit
fields, etc. Unspecified frees the implementation of the
documentation requirement.)

I believe documentation is required:

§9.6/1: "Allocation of bit-fields within a class object is
implementation-defined. Alignment of bit-fields is implementation-
defined."
 
J

James Kanze

I believe documentation is required:
§9.6/1: "Allocation of bit-fields within a class object is
implementation-defined. Alignment of bit-fields is
implementation- defined."

Then documentation is required. (Good luck on finding it,
however.)

Thanks for looking it up for me. (I could have looked it up as
well, but was just too lazy to.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top