Bit field's memory management in C

后端 未结 3 1294
一整个雨季
一整个雨季 2021-01-18 10:50

To understand the bit field memory storage, I have created the test program below.

#include 

int main()
{
    int a;
    typedef struct {
            


        
3条回答
  •  天命终不由人
    2021-01-18 11:23

    For linux on x86 the relevant ABI document can be found here(pdf).

    Specifically related to bitfields:

    ‘‘Plain’’ bit-fields (that is, those neither signed nor unsigned) always have nonnegative values. Although they may have type char, short, int, or long (which can have negative values), these bit-fields have the same range as a bit-field of the same size with the corresponding unsigned type.

    Bit-fields obey the same size and alignment rules as other structure and union members, with the following additions:

    Bit-fields are allocated from right to left (least to most significant). A bit-field must entirely reside in a storage unit appropriate for its declared type. Thus a bit-field never crosses its unit boundary.

    Bit-fields may share a storage unit with other struct / union members, including members that are not bit-fields. Of course, struct members occupy different parts of the storage unit. Unnamed bit-fields’ types do not affect the alignment of a structure or union, although individual bit-fields’ member offsets obey the alignment constraints.

    The

提交回复
热议问题