I was reading through the documentation of GNU/GCC and I stopped at the description of __attribute__ ((packed))
.
It says:
packed
The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.
Here is a structure in which the field x is packed, so that it immediately follows a:
struct foo { char a; int x[2] __attribute__ ((packed)); };
I wonder how this attribute works exactly and whether this code:
struct bar
{
char a:1 __attribute__ ((packed));
};
would create a 1-bit structure instead of 1-byte.
A byte is the smallest addressable unit. As such, structs can only occupy a whole number of bytes, not fractional bytes.
Any unused bits in bitfields (or more accurately, the storage units containing bitfields) become padding.
So in your example of struct bar
, the size of the struct will still be 1, with 1 bit used and 7 bits unused.