Fount this statement A zero-width bit field can cause the next field to be aligned on the next container boundary where the container is the same size as the underlying type of the bit field
To put it into practice assuming int is 2 bytes (16 bits) and that short is 1 byte (8 bits) to save typing. Also let's say we are using the gcc compiler (would be nice to explain the differences to clang).
struct foo {
unsigned int a:5;
unsigned int :0;
unsigned int b:3;
}
In memory this looks like
struct address
|
|
v
aaaaa000 00000000 bbb00000 00000000
Question 1: In my understanding it can not look like aaaaa000 00000000 0..00bbb00000...
, So bbb
has to align with the container directly following the current container. Is this actually true?
Moving on, if I specify
struct bar {
unsigned short x:5;
unsigned int :0;
unsigned short y:7;
}
Will it be like so?
struct address
| short stops here short starts
| | |
v v | this is uint | v
xxxxx000 00000000 00000000 yyyyyyy0
Edit 1
It was pointed out that short can not be less than 16 bytes. That is slightly beside the point in this question. But if its important to you you can replace short
with char
and int
with short