Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Char bit fields

0 views
Skip to first unread message

Alex Vinokur

unread,
Jul 28, 2009, 8:56:21 AM7/28/09
to
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


Alf P. Steinbach

unread,
Jul 28, 2009, 9:06:58 AM7/28/09
to
* 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

Richard Herring

unread,
Jul 28, 2009, 9:41:20 AM7/28/09
to
In message
<89ed4f4b-6c51-45b8...@w6g2000yqw.googlegroups.com>, Alex
Vinokur <ale...@users.sourceforge.net> writes

>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.

--
Richard Herring

James Kanze

unread,
Jul 29, 2009, 4:10:16 AM7/29/09
to
On Jul 28, 2:56 pm, Alex Vinokur <ale...@users.sourceforge.net> wrote:
> 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.)

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jerry Coffin

unread,
Jul 29, 2009, 10:43:37 AM7/29/09
to
In article <3432ad08-4388-47fd-9193-d4f28d3944e4
@t13g2000yqt.googlegroups.com>, james...@gmail.com says...

>
> On Jul 28, 2:56 pm, Alex Vinokur <ale...@users.sourceforge.net> wrote:
> > 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.)

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."

--
Later,
Jerry.

James Kanze

unread,
Jul 29, 2009, 4:18:12 PM7/29/09
to
On Jul 29, 4:43 pm, Jerry Coffin <jerryvcof...@yahoo.com> wrote:
> In article <3432ad08-4388-47fd-9193-d4f28d3944e4
> @t13g2000yqt.googlegroups.com>, james.ka...@gmail.com says...

> > On Jul 28, 2:56 pm, Alex Vinokur
> > <ale...@users.sourceforge.net> wrote:
> > > 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.)

> 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.)

0 new messages