6

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

8
  • Which compiler? Bitfields have a lot of implementation-specific details. Commented Nov 3, 2014 at 17:53
  • Even bitfields get padding for alignment during type switches, so your sample isn't right. Commented Nov 3, 2014 at 17:58
  • 2
    assuming [...] short is 1 byte (8 bits) short cannot be 8 bits, the Standard requires it to be at least 16-bit. Commented Nov 3, 2014 at 18:01
  • @ouah do you think I should edit the question? It doesn't seem to really alter the meaning. Commented Nov 3, 2014 at 18:41
  • @user10607 you may edit it and replace your unsigned short with unsigned char and add an edit note at the end of the question. Commented Nov 3, 2014 at 18:44

2 Answers 2

1

Update, after reading the text in context:

The result of your example (corrected to use char):

struct bar {
    unsigned char x:5; 
    unsigned int :0;
    unsigned char y:7;
}

would look like this (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 the position to move to next int boundary. You defined int to be 16-bit, so 16 minus 5 gives 11 bits of padding.

It does not insert an entire blank int. The example on the page you link demonstrates this (but using 32-bit integers).

2
-2

First, whenever writing bit-fields, it is always recommended that you declare variables either in ascending or descending sizes of datatypes used. This way compiler always chooses the highest datatype size and it makes chunks of the same size.

This is what i think will be.

struct address
| short stops here         short starts  
|    |                   |   
v   v| this is unit      | v              
xxxxx000 00000000 00000000 yyyyyyy0
1
  • 5
    First, I don't believe that's a valid recommendation unless your only goal is to minimize space. Sometimes bit fields are used to match an externally imposed layout. Second, I don't believe this answers the question. Commented Nov 3, 2014 at 18:20

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.