How is the size of a struct with Bit Fields determined/measured?

前端 未结 4 1988
再見小時候
再見小時候 2020-11-28 04:41
#include 

typedef struct size
{
        unsigned int a:1;
        unsigned int b:31;
        unsigned int c:1;
} mystruct;

int main()
{
        myst         


        
4条回答
  •  清酒与你
    2020-11-28 05:08

    Alignment

    The compiler is rounding the size of the structure to 32 bits, the size of each object it may try to reference to 32 bits, and at the same time it is preserving the order of your bit fields.

    So if you have a 32-bit item in the middle and 1-bit items on each side, that's 3 32-bit words to allocate and so: 12 bytes.

    For the other two cases, it's just a question of how few 32-bit objects your bitfield sequence can be packed into, while still preserving field order.

提交回复
热议问题