5

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?

2
  • The rule in general is: the compiler can pad and align things in any way it wants. All compilers offer implementation-defined extensions to control how fields get packed, and you should use them if you care about it. Commented Oct 15, 2013 at 6:33
  • Possible duplicate of Does the type of bitfield affect structure alignement and also read this. Commented Oct 15, 2013 at 6:36

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Bit-wise operators on unsigned integers.
@ElchononEdelson Just so :)
-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.