That structure may legitimately be as small as 2 bytes or 3 octets. That
is:
sizeof (struct S_Test) >= 2 /* 1 */
sizeof (struct S_Test) * CHAR_BIT >= 24 /* 2 */
Property 1 occurs when a short occupies 1 byte. Property 2 occurs when a
short is 16 bits and a byte is 8 bits.
--
Clive D.W. Feather | Internet Expert | Work: <cl...@demon.net>
Tel: +44 181 371 1138 | Demon Internet Ltd. | Home: <cl...@davros.org>
Fax: +44 181 371 1037 | | Web: <http://www.davros.org>
Written on my laptop; please observe the Reply-To address
You're right in the general case. However, he said that the compilers he
checked all had alignment "set to 4 bytes", whatever that means.
Therefore, the value of CHAR_BIT isn't relevant. Assuming that he means
that both short and bitfields must be aligned on multiples of 4 bytes,
the size can't be less than 8 bytes, hence his confusion. The obvious
explanation for sizeof(S_test)==4 is that "alignment set to 4 bytes" is
either not true, or means something else.
>You're right in the general case. However, he said that the compilers he
>checked all had alignment "set to 4 bytes", whatever that means.
>Therefore, the value of CHAR_BIT isn't relevant. Assuming that he means
>that both short and bitfields must be aligned on multiples of 4 bytes,
>the size can't be less than 8 bytes, hence his confusion. The obvious
>explanation for sizeof(S_test)==4 is that "alignment set to 4 bytes" is
>either not true, or means something else.
Um, I don't see the problem.
"Alignment set to 4 bytes" means - I presume - that structures all have
an alignment of 4 bytes. So the above structure consists of:
2 bytes for Id
1 byte holding both Willi and Otto
1 byte padding to get the alignment.
What's the problem ?
> >the size can't be less than 8 bytes, hence his confusion. The obvious
> >explanation for sizeof(S_test)==4 is that "alignment set to 4 bytes" is
> >either not true, or means something else.
>
> Um, I don't see the problem.
>
> "Alignment set to 4 bytes" means - I presume - that structures all have
> an alignment of 4 bytes. So the above structure consists of:
> 2 bytes for Id
> 1 byte holding both Willi and Otto
> 1 byte padding to get the alignment.
>
> What's the problem ?
That's an example of "means something else". It's what I would expect,
and it's apparantly what two of the compilers he listed were doing. It's
not the same as the interpretation I suggested, which was that Willi and
Otto must be seperated by a multiple of four bytes from the start of the
structure. Since this interpretation would explain his confusion, it's
probably close to the one he was using.
:>You're right in the general case. However, he said that the compilers he
:>checked all had alignment "set to 4 bytes", whatever that means.
:>Therefore, the value of CHAR_BIT isn't relevant. Assuming that he means
:>that both short and bitfields must be aligned on multiples of 4 bytes,
:>the size can't be less than 8 bytes, hence his confusion. The obvious
:>explanation for sizeof(S_test)==4 is that "alignment set to 4 bytes" is
:>either not true, or means something else.
: Um, I don't see the problem.
: "Alignment set to 4 bytes" means - I presume - that structures all have
: an alignment of 4 bytes. So the above structure consists of:
: 2 bytes for Id
: 1 byte holding both Willi and Otto
: 1 byte padding to get the alignment.
Don't Willi and Otto have to be in the same byte? (You'd get the
same answer, but with two bytes padding). I'm just not certain
about the bitfield padding rules.
>Don't Willi and Otto have to be in the same byte? (You'd get the
>same answer, but with two bytes padding). I'm just not certain
>about the bitfield padding rules.
They don't have to be. They are allowed to be, but there's no
requirement for it.
:>Don't Willi and Otto have to be in the same byte? (You'd get the
:>same answer, but with two bytes padding). I'm just not certain
:>about the bitfield padding rules.
: They don't have to be. They are allowed to be, but there's no
: requirement for it.
Thanks - I had read ANSI 3.5.2.1 line 35 as compulsory, if an implementation
could pack stuff into bytes, rather than ints, at all.
Has this changed in C9x? `If enough space remains, a bit field that
immediately follows another bit-field in a structure shall be packed
into adjacent bits of the same unit': C89.
---
>> They don't have to be. They are allowed to be, but there's no
>> requirement for it.
>
>Has this changed in C9x? `If enough space remains, a bit field that
>immediately follows another bit-field in a structure shall be packed
>into adjacent bits of the same unit': C89.
Oops, no, I'd just forgotten about that wording. Yes, they have to be in
the same byte.