Re: C question
- From: Anton Erasmus <nobody@xxxxxxxxxxxxxxxx>
- Date: Wed, 27 Jul 2005 21:31:48 +0200
On Wed, 27 Jul 2005 15:34:34 -0000, Grant Edwards <grante@xxxxxxxx>
wrote:
>On 2005-07-27, Colin Paul Gloster <Colin_Paul_Gloster@xxxxxxx> wrote:
>
>> As already answered by someone else, you have found an example
>> of bit fields. The reason no field name exists on line 3 is
>> because it is just padding.
>>
>> Do not use bit fields in C. From page 150 of the second
>> edition of Kernighan and Ritchie, "The C Programming
>> Language", ISBN 0131103628:
>
>If you're program is supposed to be portable, I would agree.
>Much of what one writes in an embedded program has no hope
>whatsoever of being portable, so bitfields have their place.
>
[Snipped]
Ones code should at least be portable between compilers, and between
different versions of the same compiler. It also should not break
when the optimisation level is changed. I have worked with compilers
where with low optimization, bitfields were byte aligned. When upping
the optimization to maximum, the bitfields were suddenly word aligned.
In my opinion bitfields should not be used where the underlying
structure is important. Use masks and shifts instead. If one wants to
do such things as calculations with a 12bit sized integer, then
bitfields are nice because the underlying implimentation does not
matter.
Regards
Anton Erasmus
.
- References:
- C question
- From: Xarion
- Re: C question
- From: Colin Paul Gloster
- Re: C question
- From: Grant Edwards
- C question
- Prev by Date: Re: Learning embedded coding, which uC?
- Next by Date: Re: Learning embedded coding, which uC?
- Previous by thread: Re: C question
- Next by thread: Re: C question
- Index(es):
Relevant Pages
|