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

user10607
  • 3,011
  • 4
  • 24
  • 31
  • Which compiler? Bitfields have a lot of implementation-specific details. – Carl Norum Nov 03 '14 at 17:53
  • Even bitfields get padding for alignment during type switches, so your sample isn't right. – Mooing Duck Nov 03 '14 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. – ouah Nov 03 '14 at 18:01
  • @ouah do you think I should edit the question? It doesn't seem to really alter the meaning. – user10607 Nov 03 '14 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. – ouah Nov 03 '14 at 18:44
  • @user10607 "A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type." and _Bool may not be the same as `unsigned char`. – edmz Nov 03 '14 at 18:49
  • @black well other types are definitely allowed as shown in this answer http://stackoverflow.com/a/4298148/1073672 – user10607 Nov 03 '14 at 18:52
  • @user10607 yes but it's implementation defined. Using `:0` is ok, if you referred to that. – edmz Nov 03 '14 at 19:07

2 Answers2

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).

ams
  • 24,923
  • 4
  • 54
  • 75
  • Here is the link for context http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc03defbitf.htm – user10607 Nov 03 '14 at 23:57
  • I did some more reading and your answer fits the big picture nicely. Thanks! – user10607 Nov 04 '14 at 12:58
-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
SirDarius
  • 41,440
  • 8
  • 86
  • 100
saikumarm
  • 1,565
  • 1
  • 15
  • 30
  • 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. – Keith Thompson Nov 03 '14 at 18:20