Different types for zero length bit fields in c? -
Different types for zero length bit fields in c? -
fount statement a zero-width bit field can cause next field aligned on next container boundary container same size underlying type of bit field
to set practice assuming int 2 bytes (16 bits) , short 1 byte (8 bits) save typing. let's using gcc compiler (would nice explain differences clang).
struct foo { unsigned int a:5; unsigned int :0; unsigned int b:3; }
in memory looks
struct address | | v aaaaa000 00000000 bbb00000 00000000
question 1: in understanding can not aaaaa000 00000000 0..00bbb00000...
, bbb
has align container directly next current container. true?
moving on, if specify
struct bar { unsigned short x:5; unsigned int :0; unsigned short y:7; }
will so?
struct address | short stops here short starts | | | v v | uint | v xxxxx000 00000000 00000000 yyyyyyy0
edit 1 pointed out short can not less 16 bytes. beside point in question. if of import you can replace short
char
, int
short
update, after reading text in context:
the result of illustration (corrected utilize char
):
struct bar { unsigned char x:5; unsigned int :0; unsigned char y:7; }
would (assuming 16-bit int
):
char pad pad int boundary | | | | v v v v xxxxx000 00000000 yyyyyyy0
(i'm ignoring endian).
the zero-length bitfield causes position move next int
boundary. defined int
16-bit, 16 minus 5 gives 11 bits of padding.
it not insert entire blank int
. illustration on page link demonstrates (but using 32-bit integers).
c bit-fields
Comments
Post a Comment