Fount this statement A zero-width bit field may align the next field at the next container boundary, where the container is the same size as the base type of the bit field
Suppose int is 2 bytes (16 bits) and short is 1 byte (8 bits) to save input. Also suppose we use the gcc compiler (it would be nice to explain the differences in clang).
struct foo { unsigned int a:5; unsigned int :0; unsigned int b:3; }
In memory, it looks like
struct address | | v aaaaa000 00000000 bbb00000 00000000
Question 1: In my opinion, it cannot look like aaaaa000 00000000 0..00bbb00000... , so bbb should align with the container immediately after the current container. Is this really so?
Moving if i point
struct bar { unsigned short x:5; unsigned int :0; unsigned short y:7; }
Would that be so?
struct address | short stops here short starts | | | vv | this is uint | v xxxxx000 00000000 00000000 yyyyyyy0
Edit 1 It was pointed out that the short cannot be less than 16 bytes. This is a little alongside the question in this matter. But if this is important to you, you can replace short with char and int with short
source share