Bools and bitfields
Ian Lance Taylor
iant@google.com
Sat Dec 12 11:08:00 GMT 2009
"Paulo J. Matos" <pocmatos@gmail.com> writes:
> Regarding the size of possible bitfields I would like to know why:
> #include <stdbool.h>
> struct s {
> bool f : 2;
> };
>
> fails to compile in gcc 4.3 and gcc 4.4 but not in gcc 4.2.
>
> sizeof(bool) is 1, and C99 6.7.2.1 paragraph 3 seems to imply that the
> bitfield size shouldn't be bigger than the width of the object.
> "The expression that specifies the width of a bit-field shall be an
> integer constant
> expression with a nonnegative value that does not exceed the width of
> an object of the
> type that would be specified were the colon and expression omitted."
>
> Therefore I would expect to have 1 to 8 as possible values for an 8
> bit/byte architecture.
sizeof(bool) == 1 but the type is nevertheless just 1 bit wide. The
only valid values to store in a bool are 0 and 1. It doesn't make
sense to have a bool field that is 2 bits wide, as you can still only
store 0 and 1 in it.
Ian
More information about the Gcc-help
mailing list