25

I'm studying the basics of the C language. I arrived at the chapter of structures with bit fields. The book shows an example of a struct with two different types of data: various bools and various unsigned ints.

The book declares that the structure has a size of 16 bits and that without using padding the structure would measure 10 bits.

This is the structure that the book uses in the example:

#include <stdio.h>
#include <stdbool.h>

struct test{

       bool opaque                 : 1;
       unsigned int fill_color     : 3;
       unsigned int                : 4;
       bool show_border            : 1;
       unsigned int border_color   : 3;
       unsigned int border_style   : 2;
       unsigned int                : 2;
};

int main(void)
{
       struct test Test;

       printf("%zu\n", sizeof(Test));

       return 0;
}

Why on my compiler instead does the exact same structure measure 16 bytes (rather than bits) with padding and 16 bytes without padding?

I'm using

GCC (tdm-1) 4.9.2 compiler;
Code::Blocks as IDE.
Windows 7 64 Bit
Intel CPU 64 bit

This is result I'm getting:

enter image description here

Here is a picture of the page where the example is:

enter image description here

3
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. Commented Apr 26, 2018 at 13:34
  • 4
    From what I remember there's some known bugs in GCC here. That being said, don't use bit-fields. Don't read a book teaching you to use bit-fields.
    – Lundin
    Commented Apr 26, 2018 at 14:47
  • 3
    The book seems like garbage. C doesn't "use unsigned int as the basic layout unit for structures with bit fields". Per 6.7.2.1 Structure and union specifiers, paragraph 5 of the C standard, a bit field can be any type: "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. It is implementation-defined whether atomic types are permitted." Commented Apr 26, 2018 at 17:24

7 Answers 7

30

The Microsoft ABI lays out bitfields in a different way than GCC normally does it on other platforms. You can choose to use the Microsoft-compatible layout with the -mms-bitfields option, or disable it with -mno-ms-bitfields. It's likely that your GCC version uses -mms-bitfields by default.

According to the documentation, When -mms-bitfields is enabled:

  • Every data object has an alignment requirement. The alignment requirement for all data except structures, unions, and arrays is either the size of the object or the current packing size (specified with either the aligned attribute or the pack pragma), whichever is less. For structures, unions, and arrays, the alignment requirement is the largest alignment requirement of its members. Every object is allocated an offset so that: offset % alignment_requirement == 0
  • Adjacent bit-fields are packed into the same 1-, 2-, or 4-byte allocation unit if the integral types are the same size and if the next bit-field fits into the current allocation unit without crossing the boundary imposed by the common alignment requirements of the bit-fields.

Since bool and unsigned int have different sizes, they are packed and aligned separately, which increases the struct size substantially. The alignment of unsigned int is 4 bytes, and having to realign three times in the middle of the struct leads to a 16 byte total size.

You can get the same behavior of the book by changing bool to unsigned int, or by specifying -mno-ms-bitfields (though this will mean you can't inter-operate with code compiled on Microsoft compilers).

Note that the C standard does not specify how bitfields are laid out. So what your book says may be true for some platforms but not for all of them.

3
  • 22
    This is a great answer overall but that final paragraph is the real key point; far too many books relate implementation specifics as broad fact without qualification. It drives me nuts... Commented Apr 26, 2018 at 14:35
  • In the last sentence, you might change "the standard" to "the C language standard," since, as you note, there are standards that do.
    – Owen
    Commented Apr 26, 2018 at 17:53
  • 2
    Although the C standard does not specify all the details of how bitfields are laid out, it does place some requirements (see C11 6.7.2.1/11). There is a lot of wiggle room there, but as it applies to the OP's structure, the MS ABI requires a layout that cannot be interpreted as conforming to the C standard. Microsoft non-conformance strikes again! Commented Apr 27, 2018 at 16:25
8

Taken as describing the provisions of the C language standard, your text makes unjustified claims. Specifically, not only does the standard not say that unsigned int is the basic layout unit of structures of any kind, it explicitly disclaims any requirement on the size of the storage units in which bitfield representations are stored:

An implementation may allocate any addressable storage unit large enough to hold a bit- field.

(C2011, 6.7.2.1/11)

The text also makes assumptions about padding that are not supported by the standard. A C implementation is free to include an arbitrary amount of padding after any or all elements and bitfield storage units of a struct, including the last. Implementations typically use this freedom to address data alignment considerations, but C does not limit padding to that use. This is altogether separate from the unnamed bitfields that your text refers to as "padding".

I guess the book should be commended, however, for avoiding the distressingly common misconception that C requires the declared data type of a bit field to have anything to do with the size of the storage unit(s) in which its representation resides. The standard makes no such association.

Why on my compiler instead does the exact same structure measure 16 bytes (rather than bits) with padding and 16 bytes without padding?

To cut the text as much slack as possible, it does distinguish between the number of bits of data occupied by members (16 bits total, 6 belonging to unnamed bitfields) and the overall size of instances of the struct. It seems to be asserting that the overall structure will be the size of an unsigned int, which apparently is 32 bits on the system it is describing, and that would be the same for both versions of the struct.

In principle, your observed sizes could be explained by your implementation using a 128-bit storage unit for the bitfields. In practice, it likely uses one or more smaller storage units, so that some of the extra space in each struct is attributable to implementation-provided padding, such as I touch on above.

It is very common for C implementations to impose a minimum size on all structure types, and therefore to pad representations to that size when necessary. Often this size matches the strictest alignment requirement of any data type supported by the system, though that's, again, an implementation consideration, not a requirement of the language.

Bottom line: only by relying on implementation details and / or extensions can you predict the exact size of a struct, regardless of the presence or absence of bitfield members.

6

The C standard doesn't described all details about how variables shall be placed in memory. This leaves room for optimization that depends on the platform used.

To give your self an idea about how things are located in memory, you can do like this:

#include <stdio.h>
#include <stdbool.h>

struct test{

       bool opaque                 : 1;
       unsigned int fill_color     : 3;
       unsigned int                : 4;
       bool show_border            : 1;
       unsigned int border_color   : 3;
       unsigned int border_style   : 2;
       unsigned int                : 2;
};


int main(void)
{
  struct test Test = {0};
  int i;
  printf("%zu\n", sizeof(Test));

  unsigned char* p;
  p = (unsigned char*)&Test;
  for(i=0; i<sizeof(Test); ++i)
  {
    printf("%02x", *p);
    ++p;
  }
  printf("\n");

  Test.opaque = true;

  p = (unsigned char*)&Test;
  for(i=0; i<sizeof(Test); ++i)
  {
    printf("%02x", *p);
    ++p;
  }
  printf("\n");

  Test.fill_color = 3;

  p = (unsigned char*)&Test;
  for(i=0; i<sizeof(Test); ++i)
  {
    printf("%02x", *p);
    ++p;
  }
  printf("\n");

  return 0;
}

Running this on ideone (https://ideone.com/wbR5tI) I get:

4
00000000
01000000
07000000

So I can see that opaque and fill_color are both in the first byte. Running exactly the same code on a Windows machine (using gcc) gives:

16
00000000000000000000000000000000
01000000000000000000000000000000
01000000030000000000000000000000

So here I can see that opaque and fill_color are not both in the first byte. It seems that opaque takes up 4 bytes.

This explains that you get 16 bytes in total, i.e. the bool takes 4 bytes each and then 4 bytes for the fields in between and after.

2
  • To avoid undefined behavior, your format needs to match your argument types. In this case you need to match unsigned char with a directive of form %02hhx (note the "hh"). It looks like the observed behavior is probably what you intended anyway, but don't rely on luck. Commented Apr 26, 2018 at 16:06
  • Note also that although C does not require the opaque and fill_color members to appear in the same byte, it does require them to appear in the same "addressible storage unit", however the implementation chooses that, if there is enough space. The observed 16-byte layout conforms only if the storage unit to which opaque is assigned is larger than four bytes or smaller than 4 bits. Commented Apr 26, 2018 at 16:13
6

To my surprise there seems to be a difference between some GCC 4.9.2 online compilers. First, this is my code:

#include <stdio.h>
#include <stdbool.h>

struct test {
       bool opaque                 : 1;
       unsigned int fill_color     : 3;
       unsigned int                : 4;
       bool show_border            : 1;
       unsigned int border_color   : 3;
       unsigned int border_style   : 2;
       unsigned int                : 2;
};

struct test_packed {
       bool opaque                 : 1;
       unsigned int fill_color     : 3;
       unsigned int                : 4;
       bool show_border            : 1;
       unsigned int border_color   : 3;
       unsigned int border_style   : 2;
       unsigned int                : 2;
} __attribute__((packed));

int main(void)
{
       struct test padding;
       struct test_packed no_padding;

       printf("with padding: %zu bytes = %zu bits\n", sizeof(padding), sizeof(padding) * 8);
       printf("without padding: %zu bytes = %zu bits\n", sizeof(no_padding), sizeof(no_padding) * 8);

       return 0;
}

And now, results from different compilers.

GCC 4.9.2 from WandBox:

with padding: 4 bytes = 32 bits
without padding: 2 bytes = 16 bits

GCC 4.9.2 from http://cpp.sh/:

with padding: 4 bytes = 32 bits
without padding: 2 bytes = 16 bits

BUT

GCC 4.9.2 from theonlinecompiler.com:

with padding: 16 bytes = 128 bits
without padding: 16 bytes = 128 bits

(to properly compile you need to chagne %zu to %u)

EDIT

@interjay's answer might explain this. When I added -mms-bitfields to GCC 4.9.2 from WandBox, I got this:

with padding: 16 bytes = 128 bits
without padding: 16 bytes = 128 bits
4
  • 2
    Nice answer - UV. Detail: "seems to be a problem" is not so much a problem, but a surprising difference. A result of 16 is a compliant answer - and potentially more speed efficient on the target machine. Commented Apr 26, 2018 at 11:41
  • @chux, thanks, you're right, I've corrected my answer. Commented Apr 26, 2018 at 11:47
  • 2
    It looks like theonlinecompiler.com runs GCC on Windows, so that's the difference.
    – interjay
    Commented Apr 26, 2018 at 12:00
  • @interjay, I believe you're right. I edited my answer to add reference to yours. Commented Apr 26, 2018 at 13:18
5

Before the author defines the struct he says he wants to divide the bit fields into two bytes so there will be one byte containing the bitfields for the fill-related information and one byte for the border related information.

To achieve that, he adds (inserts) a few unused bits (bitfield):

unsigned int       4;  // padding of the first byte

he also padds the second byte, but there is no need for that.

So before the padding there would be 10 bits in use and afer the padding there are 16 bits defined (but not al of them in use).


Note: the author uses bool to indicate a 1/0 field. The author next assumes the _Bool C99 type is aliased to bool. But it seems compilers get a bit confused here. Replacing bool with unsigned int would solve it. From C99:

6.3.2: The following may be used in an expression wherever an int or unsigned int may be used:

  • A bit-field of type _Bool, int, signed int, or unsigned int.
19
  • 1
    @interjay, indeed. Seems to me he mixes bits and bytes. Commented Apr 26, 2018 at 11:20
  • 1
    @interjay If bool and unsigned cause the compiler to align those fields, a result of 16 bytes is not that strange. Commented Apr 26, 2018 at 11:24
  • 1
    @chux I guess it's possible, though it's very strange behavior to align fields in the middle of a bitfield. Especially when it isn't reproduced on Linux. Maybe it's something that GCC does on Windows in order to adhere to the platform ABI?
    – interjay
    Commented Apr 26, 2018 at 11:31
  • 1
    @joop bool is fine. Expecting nice packing with a bit-field using mixed types is the source of the difficulties. Bit-fields are higher mainatence, not bool. Commented Apr 26, 2018 at 11:45
  • 1
    @joop: The semantics of a Bool bitfield are different from those of a single-bit unsigned char bitfield. Storing the value 2 into a single-bit field of any non-Bool unsigned type will clear that bit, while storing 2 into a single-bit Bool will set that bit. On the other hand, I think any code which tries to write anything other than 0 or 1 to a single-bit field should be regarded as highly suspect. If x might have a value other than 0 or 1, code wanting to store it to a bitfield should either be written as thing.field = !!(x) or thing.field = (x & 1); to make clear...
    – supercat
    Commented Apr 26, 2018 at 20:36
2

You are completely misinterpreting what the book says.

There are 16 bits worth of bit fields declared. 6 bits are unnamed fields that cannot be used for anything - that's the padding mentioned. 16 bits minus 6 bits equals 10 bits. Not counting the padding fields, the struct has 10 useful bits.

How many bytes the struct has, depends on the quality of the compiler. Apparently you ran into a compiler that doesn't pack bool bitfields in a struct, and it uses 4 bytes for a bool, some memory for bitfields, plus struct padding, total 4 bytes, another 4 bytes for a bool, more memory for bitfields, plus struct padding, total 4 bytes, adding up to 16 bytes. It's rather sad really. This struct could quite reasonably be two bytes.

1

There have historically been two common ways of interpreting the types of bitfield elements:

  1. Examine whether the type is signed or unsigned, but ignore distinctions between "char", "short", "int", etc. in deciding where an element should be placed.

  2. Unless a bitfield is preceded by another with the same type, or the corresponding signed/unsigned type, allocate an object of that type and place the bitfield within it. Place following bitfields with the same type in that object if they fit.

I think the motivation behind #2 was that on a platform where 16-bit values need to be word-aligned, a compiler given something like:

struct foo {
  char x;  // Not a bitfield
  someType f1 : 1;
  someType f2 : 7;
};

might be able to allocate a two-byte structure, with both fields being placed in the second byte, but if the structure had been:

struct foo {
  char x;  // Not a bitfield
  someType f1 : 1;
  someType f2 : 15;
};

it would be necessary that all of f2 fit within a single 16-bit word, which would thus necessitate a padding byte before f1. Because of the Common Initial Sequence rule, f1 must be placed identically in those two structures, which would imply that if f1 could satisfy the Common Initial Sequence rule, it would need padding before it even in the first structure.

As it is, code which wants to allow the denser layout in the first case can say:

struct foo {
  char x;  // Not a bitfield
  unsigned char f1 : 1;
  unsigned char f2 : 7;
};

and invite the compiler to put both bitfields into a byte immediately following x. Since the type is specified as unsigned char, the compiler need not worry about the possibility of a 15-bit field. If the layout were:

struct foo {
  char x;  // Not a bitfield
  unsigned short f1 : 1;
  unsigned short f2 : 7;
};

and the intention was that f1 and f2 would sit in the same storage, then the compiler would need to place f1 in a way that could support a word-aligned access for its "bunkmate" f2. If the code were:

struct foo {
  char x;  // Not a bitfield
  unsigned char f1 : 1;
  unsigned short f2 : 15;
};

then f1 would be placed following x, and f2 in a word by itself.

Note that the C89 Standard added a syntax to force the layout that prevent f1 from being placed in a byte before the storage used f2:

struct foo {
  char x;  // Not a bitfield
  unsigned short : 0;  // Forces next bitfield to start at a "short" boundary
  unsigned short f1 : 1;
  unsigned short f2 : 15;
};

The addition of the :0 syntax in C89 largely eliminates the need to have compilers regard changing types as forcing alignment, except when processing old code.

19
  • Your approach (2) does not conform to any version of the C standard, so in what sense is it common? Are you referring to pre-standard C implementations (you do say "historically")? Or are you asserting that it is a characteristic common to many now-relevant C implementations to be non-conforming in this way? Or perhaps are you referring to a small number of implementations with a large installed base? Commented Apr 26, 2018 at 17:13
  • @JohnBollinger: I don't have a K&R1 or K&R2 handy, but I think that's how it was defined there. It's also matches the behavior of some pre-standard compilers I've used; while nothing in the Standard would require such behavior, nothing would prohibit it either.
    – supercat
    Commented Apr 26, 2018 at 17:14
  • 1
    C89, C99, and C11 all say "If enough space remains [in the same addressable storage unit], a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit." I don't see any reasonable interpretation of "addressable storage unit" that affords approach (2) conforming to that. Commented Apr 26, 2018 at 17:20
  • K&R1 does not have this language (it happens I do have a copy handy), but it couches the discussion of bit fields in terms of minimizing the amount of space used, which is contrary to (2). Commented Apr 26, 2018 at 17:25
  • @JohnBollinger: The Standard is rather mushy here as to what the "same addressable storage unit" means in cases where a field's type doesn't match the previous one. Given struct b2 { char x; unsigned m1:4,m2:8,m3:8,m4:4;};, clang and gcc both yield a 4-byte structure. Into what type of addressable storage unit are m1..m4 placed? If one changes the declared type of m3 and m4 to unsigned char, how should that affect things?
    – supercat
    Commented Apr 26, 2018 at 18:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.