0

For this question, the size of the data types used is:

  • char: 1 byte
  • unsigned: 4 bytes
  • long: 8 bytes

I found this answer which explains the size of structs containing bit fields by going top to bottom and allocating more bytes if necessary. As far as I understand, whether packing the members together or not is implementation defined but if packing is done, this may be a way to do it. So far so good.

How does this work when mixing data types? The size of the struct is apparently 4 bytes.

struct bitfield1
{
    char a : 1;       // char needs 1 byte, so 1 byte allocated
    char b : 1;       // this fits inside the first byte allocated
    char c : 1;       // this fits inside the first byte allocated
    unsigned d : 1;   // this would fit inside the first byte allocated, but it allocates 3 bytes more?!
};

I would have said that it either just fits inside the first byte or allocates 4 more bytes because it is an unsigned integer.

Similarly, when mixing more data types...

struct bitfield2
{
    char a : 1;         // char needs 1 byte, so 1 byte allocated
    unsigned b : 1;     // this fits inside the first byte allocated
    char c : 1;         // this fits inside the first byte allocated
    long d : 1;         // this would fit inside the first byte allocated, but it allocates 7 bytes more?!
};

...it allocates 8 bytes in total, the size of long. How does this work in detail?

Rob
  • 14,746
  • 28
  • 47
  • 65
Agiltohr
  • 115
  • 5
  • 3
    The representation of bit-fields is very implementation-specific. Both the order and the underlying type size. – Eugene Sh. Dec 15 '21 at 15:11
  • 2
    Bit-fields are underspecified by the standard so there's no telling what will happen. If you want deterministic, portable code, then don't use bit-fields. Generally the compiler is only required to allocate different fields in the same "storage unit" if they are of the very same type. – Lundin Dec 15 '21 at 15:12
  • 3
    This is a much better answer: https://stackoverflow.com/a/14008523/4756299 "An implementation may allocate **any addressable storage unit** large enough to hold a bit- field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit ... **is implementation-defined**. The order of allocation of bit-fields ... **is implementation-defined**. The alignment of the addressable storage unit **is unspecified**." – Andrew Henle Dec 15 '21 at 15:12
  • 2
    Does this answer your question? [How is the size of a struct with Bit Fields determined/measured?](https://stackoverflow.com/questions/4129961/how-is-the-size-of-a-struct-with-bit-fields-determined-measured) – the busybee Dec 15 '21 at 15:13
  • 2
    *I found [this answer](https://stackoverflow.com/questions/4129961/how-is-the-size-of-a-struct-with-bit-fields-determined-measured/16129290#16129290) which explains ...* That answer is wrong. – Andrew Henle Dec 15 '21 at 15:19
  • Thanks, I think this answers my question. – Agiltohr Dec 15 '21 at 15:24
  • 1
    You figure out the size of a struct, whether it contains bit fields or not, by using the `sizeof` operator. In general, this result may vary across different C implementations, and perhaps for other reasons as well. If you know enough about how your particular implementation lays out structures then you can use that knowledge to compute structure sizes manually, but this is a poor idea under most circumstances. – John Bollinger Dec 15 '21 at 15:25
  • 1
    According to [GCC documentation](https://gcc.gnu.org/onlinedocs/gcc/Structures-unions-enumerations-and-bit-fields-implementation.html), GCC follows the target ABI (Application Binary Interface) (and other compilers must also follow the ABI if they wish to interoperate correctly). So the answer is going to depend on the ABI for your target platform. – Eric Postpischil Dec 15 '21 at 15:27
  • @EricPostpischil Cool stuff! Thanks for the link, this makes it clearer! – Agiltohr Dec 15 '21 at 15:30

0 Answers0