Bit-field Question

F

Fao, Sean

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,
 
K

Keith Thompson

Fao said:
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.

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

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

Jack Klein

in comp.lang.c:

[snip]
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.)

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:
#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;
}

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

Keith Thompson

Jack Klein said:
in comp.lang.c:

[snip]
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.)

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:

[snip again]
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.

Whoops.
 
R

Roberto Waltman

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

You probably meant "roles2" here.


Roberto Waltman

[ Please reply to the group,
return address is invalid ]
 
K

Keith Thompson

Roberto Waltman said:
-------------------------------^^^

You probably meant "roles2" here.

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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top