Question

If we have a struct with bit fields, then how are the subsequent members aligned in the struct? Consider the following code:

struct A{
    int a:1;
    char b;     // at offset 1
};

struct B{
    int a:16;
    int b: 17;
    char c;     // at offset 7
};

printf("Size of A: %d\n", (int)sizeof(struct A));
printf("Offset of b in A: %d\n", (int)offsetof(struct A, b));

printf("Size of B: %d\n", (int)sizeof(struct B));
printf("Offset of c in B: %d\n", (int)offsetof(struct B, c));

Output:

Size of A: 4
Offset of b in A: 1
Size of B: 8
Offset of c in B: 7

Here, in the first case, b is allocated just in the 2nd byte of the struct without any padding. But, in the 2nd case, when bit fields overflow 4 bytes, c is allocated in the last (8th) byte.

What is happening in the 2nd case? What is the rule for padding in structs involving bit fields in general?

Was it helpful?

Solution

how are the subsequent members aligned in the struct?

Nobody knows. This is implementation-defined behavior and thus compiler-specific.

What is happening in the 2nd case?

The compiler may have added padding bytes or padding bits. Or the bit order of the struct might be different than you expect. The first item of the struct is not necessarily containing the MSB.

What is the rule for padding in structs involving bit fields in general?

The compiler is free to add any kind of padding bytes (and padding bits in a bit field), anywhere in the struct, as long as it isn't done at the very beginning of the struct.

Bit-fields are very poorly defined by the standard. They are essentially useless for anything else but chunks of boolean flags allocated at random places in memory. I would advise you to use bit-wise operators on plain integers instead. Then you get 100% deterministic, portable code.

OTHER TIPS

I would take a small example. Hope this will make clear :: Consider two structures :

struct {
    char a;
    int b;
    char c;
} X;

Versus.

struct {
    char a;
    char b;
    int c;
} Y;

A little more explanation regarding comments below:

All the below is not a 100%, but the common way the structs will be constructed in 32 bits system where int is 32 bits:

Struct X:

|     |     |     |     |     |     |     |     |     |     |     |     |
 char  pad    pad   pad   ---------int---------- char   pad   pad   pad   = 12 bytes

struct Y:

|     |     |     |     |     |     |     |     |
 char  char  pad   pad   ---------int----------        = 8 bytes

Thank you

Some reference ::

Data structure Alignment-wikipedia

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top