Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Date | 2011-09-01 14:19 -0400 |
|---|---|
| From | Kenneth Brody <kenbrody@spamcop.net> |
| Newsgroups | comp.lang.c |
| Subject | Re: Bit-field union bug |
| References | <j3m8np$8gv$1@speranza.aioe.org> <1bbov5xnp5.fsf@snowball.wb.pfeifferfamily.net> <slrnj5tbae.sup.usenet-nospam@guild.seebs.net> <4E5EB3D5.9090402@verizon.net> |
| Message-ID | <XZudnYcMB5FbUcLTnZ2dnUVZ_v2dnZ2d@bestweb.net> (permalink) |
On 8/31/2011 6:21 PM, James Kuyper wrote:
> On 08/31/2011 05:57 PM, Seebs wrote:
>> On 2011-08-31, Joe Pfeiffer<pfeiffer@cs.nmsu.edu> wrote:
>>> While I would agree with you, the standard disagrees with both of us.
>>> I'm not quite sure what the point of a bitfield is when the compiler is
>>> free to insert arbitrary padding between elements, but that is indeed
>>> the case.
>>
>> The point is that the compiler is *permitted* to coalesce bitfields. But
>> not required to, because some systems really don't provide meaningful support
>> for them.
>
> Actually, it is required to do so, when adjacent bit-fields in the same
> struct fit in a single storage unit (6.7.2.1p10). And since CHAR_BIT is
> required to be>=8, these bit-fields, had they been in the same struct,
> would have been required to be stored in adjacent bits of the same
> storage unit.
But, this is not two bit-fields. Here's the original code:
>> > struct s_skip_ind {
>> > unsigned skip_ind:4;
>> > };
>> >
>> > struct s_trans_id {
>> > unsigned trans_val:3;
>> > unsigned trans_id:1;
>> > };
>> >
>> > struct foo {
>> > unsigned proto:4;
>> > union {
>> > s_skip_ind skip_ind;
>> > s_trans_id trans_id;
>> > };
>> > };
The struct "foo" consists of a bit-field and a union. The fact that the
union consists of two structs which, in turn, consist of bit-fields, is
irrelevant to 6.7.2.1p10 (as I read it):
> 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.
What follows the bit-field "proto" is an unnamed union, not another bit-field.
[...]
> An implementation has a lot of freedom to allocate bit-fields; far too
> much to make any portable use of them for communications outside of a
> single program. However, there's not quite as much freedom as you
> thought there was.
As I found out years ago when a bit-mapped flags value stored on disk was
not portable to another system whose compiler allocated bit-fields the other
way.
struct foo {
...
struct {
int SomeFlag:1;
int AnotherFlag:1;
}
Flags;
...
};
It's now just an "unsigned short" with an enum for bit-mask values.
--
Kenneth Brody
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Bit-field union bug Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-08-31 21:23 +0000
Re: Bit-field union bug Keith Thompson <kst-u@mib.org> - 2011-08-31 14:34 -0700
Re: Bit-field union bug Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-09-03 19:06 +0000
Re: Bit-field union bug Ian Collins <ian-news@hotmail.com> - 2011-09-04 08:19 +1200
Re: Bit-field union bug Quentin Pope <qp19433@hotmail.NOSPAM.com> - 2011-09-03 21:12 +0000
Re: Bit-field union bug Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-03 22:30 +0100
Re: Bit-field union bug Ian Collins <ian-news@hotmail.com> - 2011-09-04 11:01 +1200
Re: Bit-field union bug Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-04 00:37 +0100
Re: Bit-field union bug Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-06 00:36 +0300
Re: Bit-field union bug Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 12:23 -0800
Re: Bit-field union bug Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-24 21:15 +0000
Re: Bit-field union bug Ian Collins <ian-news@hotmail.com> - 2011-09-01 09:36 +1200
Re: Bit-field union bug jacob navia <jacob@spamsink.net> - 2011-08-31 23:38 +0200
Re: Bit-field union bug Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-08-31 15:42 -0600
Re: Bit-field union bug Seebs <usenet-nospam@seebs.net> - 2011-08-31 21:57 +0000
Re: Bit-field union bug James Kuyper <jameskuyper@verizon.net> - 2011-08-31 18:21 -0400
Re: Bit-field union bug Seebs <usenet-nospam@seebs.net> - 2011-09-01 02:45 +0000
Re: Bit-field union bug James Kuyper <jameskuyper@verizon.net> - 2011-09-01 06:37 -0400
Re: Bit-field union bug Kenneth Brody <kenbrody@spamcop.net> - 2011-09-01 14:19 -0400
Re: Bit-field union bug James Kuyper <jameskuyper@verizon.net> - 2011-09-01 14:46 -0400
Re: Bit-field union bug Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-01 15:34 +0300
Re: Bit-field union bug Keith Thompson <kst-u@mib.org> - 2011-09-01 07:24 -0700
Re: Bit-field union bug James Kuyper <jameskuyper@verizon.net> - 2011-08-31 17:55 -0400
Re: Bit-field union bug "Scott Fluhrer" <sfluhrer@ix.netcom.com> - 2011-08-31 19:01 -0400
Re: Bit-field union bug Peter Nilsson <airia@acay.com.au> - 2011-08-31 16:03 -0700
Re: Bit-field union bug David Thompson <dave.thompson2@verizon.net> - 2011-09-06 01:13 -0400
Re: Bit-field union bug Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-24 12:17 -0800
Re: Bit-field union bug ec429 <ec429@cam.ac.uk> - 2012-01-25 00:13 +0000
Re: Bit-field union bug Kaz Kylheku <kaz@kylheku.com> - 2012-01-25 00:36 +0000
Re: Bit-field union bug Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 14:54 -0800
Re: Bit-field union bug Andrey Tarasevich <andreytarasevich@hotmail.com> - 2011-09-01 16:27 -0700
csiph-web