Connecting Tech Pros Worldwide Forums | Help | Site Map

Bit-field Question

Fao, Sean
Guest
 
Posts: n/a
#1: Jan 13 '06
I was just thumbing through K&R 2nd Edition and thought I'd read up on
bit-fields, which I [personally] haven't had much use for in my career.
Anyhow, K&R says that, "Fields may be declared only as ints; for
portability, specify signed or unsigned explicitly". So it got me
wondering what would happen if I tried to declare a field as a short
rather than an int.

I compiled my code with GCC 3.3.6 with all warning turned on and I got
no warnings returned. Have things changed since K&R or is there
something bad about my code?

On my implementation, the sizeof() each bit-field is identical to the
type I used to declare the fields.

<code>
#include <stdio.h>

struct {
unsigned short admin : 1;
unsigned short manager : 1;
unsigned short supervisor : 1;
unsigned short user : 1;
unsigned short guest : 1;
} roles;

struct {
unsigned int admin : 1;
unsigned int manager : 1;
unsigned int supervisor : 1;
unsigned int user : 1;
unsigned int guest : 1;
} roles2;

int main(void)
{
printf("sizeof(short): %d sizeof(roles): %d\n",
sizeof(short), sizeof(roles));
printf("sizeof(int): %d sizeof(roles2): %d\n",
sizeof(int), sizeof(int));


return 0;
}
</code>

Just curious...

Thank you in advance,

--
Sean

Keith Thompson
Guest
 
Posts: n/a
#2: Jan 13 '06

re: Bit-field Question


"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> writes:[color=blue]
> I was just thumbing through K&R 2nd Edition and thought I'd read up on
> bit-fields, which I [personally] haven't had much use for in my
> career. Anyhow, K&R says that, "Fields may be declared only as ints;
> for portability, specify signed or unsigned explicitly". So it got me
> wondering what would happen if I tried to declare a field as a short
> rather than an int.[/color]

What will happen depends on the implementation. The standard says
(C99 6.7.2.1p4):

A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type.

An implementation may support other types for bit fields, but it's not
required to; any program that uses such an extension is non-portable.
[color=blue]
> I compiled my code with GCC 3.3.6 with all warning turned on and I got
> no warnings returned. Have things changed since K&R or is there
> something bad about my code?
>
> On my implementation, the sizeof() each bit-field is identical to the
> type I used to declare the fields.[/color]

C99 6.5.3.4p1:

The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of
such a type, or to an expression that designates a bit-field
member.

This is a constraint, so if gcc doesn't give you a diagnostic, it's
non-conforming. (gcc can generally be persuaded to be conforming, or
nearly so, with command-line options.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jack Klein
Guest
 
Posts: n/a
#3: Jan 13 '06

re: Bit-field Question


On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-u@mib.org> wrote
in comp.lang.c:

[snip]
[color=blue][color=green]
> > On my implementation, the sizeof() each bit-field is identical to the
> > type I used to declare the fields.[/color]
>
> C99 6.5.3.4p1:
>
> The sizeof operator shall not be applied to an expression that has
> function type or an incomplete type, to the parenthesized name of
> such a type, or to an expression that designates a bit-field
> member.
>
> This is a constraint, so if gcc doesn't give you a diagnostic, it's
> non-conforming. (gcc can generally be persuaded to be conforming, or
> nearly so, with command-line options.)[/color]

You missed on this last one, Keith. His remark, which you are
commenting on here, is inaccurate and does not reflect what his code
actually does. I have replaced his code below, which you snipped:
[color=blue]
> #include <stdio.h>
>
> struct {
> unsigned short admin : 1;
> unsigned short manager : 1;
> unsigned short supervisor : 1;
> unsigned short user : 1;
> unsigned short guest : 1;
> } roles;
>
> struct {
> unsigned int admin : 1;
> unsigned int manager : 1;
> unsigned int supervisor : 1;
> unsigned int user : 1;
> unsigned int guest : 1;
> } roles2;
>
> int main(void)
> {
> printf("sizeof(short): %d sizeof(roles): %d\n",
> sizeof(short), sizeof(roles));
> printf("sizeof(int): %d sizeof(roles2): %d\n",
> sizeof(int), sizeof(int));
>
>
> return 0;
> }[/color]

Clearly he is not applying the sizeof operator to the bit-field
members, but to the entire struct type containing the bit-field
members, and this of course is perfectly valid.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Keith Thompson
Guest
 
Posts: n/a
#4: Jan 13 '06

re: Bit-field Question


Jack Klein <jackklein@spamcop.net> writes:[color=blue]
> On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-u@mib.org> wrote
> in comp.lang.c:
>
> [snip]
>[color=green][color=darkred]
>> > On my implementation, the sizeof() each bit-field is identical to the
>> > type I used to declare the fields.[/color]
>>
>> C99 6.5.3.4p1:
>>
>> The sizeof operator shall not be applied to an expression that has
>> function type or an incomplete type, to the parenthesized name of
>> such a type, or to an expression that designates a bit-field
>> member.
>>
>> This is a constraint, so if gcc doesn't give you a diagnostic, it's
>> non-conforming. (gcc can generally be persuaded to be conforming, or
>> nearly so, with command-line options.)[/color]
>
> You missed on this last one, Keith. His remark, which you are
> commenting on here, is inaccurate and does not reflect what his code
> actually does. I have replaced his code below, which you snipped:[/color]

[snip again]
[color=blue]
> Clearly he is not applying the sizeof operator to the bit-field
> members, but to the entire struct type containing the bit-field
> members, and this of course is perfectly valid.[/color]

Whoops.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Roberto Waltman
Guest
 
Posts: n/a
#5: Jan 14 '06

re: Bit-field Question


<enceladus311@yahoo.comI-WANT-NO-SPAM> wrote:[color=blue]
>...
>...
>int main(void)
>{
> printf("sizeof(short): %d sizeof(roles): %d\n",
> sizeof(short), sizeof(roles));
> printf("sizeof(int): %d sizeof(roles2): %d\n",
> sizeof(int), sizeof(int));[/color]
-------------------------------^^^

You probably meant "roles2" here.


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Keith Thompson
Guest
 
Posts: n/a
#6: Jan 14 '06

re: Bit-field Question


Roberto Waltman <usenet@rwaltman.net> writes:[color=blue]
> <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote:[color=green]
>>...
>>...
>>int main(void)
>>{
>> printf("sizeof(short): %d sizeof(roles): %d\n",
>> sizeof(short), sizeof(roles));
>> printf("sizeof(int): %d sizeof(roles2): %d\n",
>> sizeof(int), sizeof(int));[/color]
> -------------------------------^^^
>
> You probably meant "roles2" here.[/color]

Apart from that, "%d" isn't a correct format for size_t, which is the
type that the sizeof operator yields. Either use "%ld" and cast the
value to unsigned long, or use the C99-specific "%zd" format.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Closed Thread


Similar C / C++ bytes