Packed bit fields in c structures - GCC

前端 未结 2 424
Happy的楠姐
Happy的楠姐 2021-02-04 02:47

I am working with structs in c on linux. I started using bit fields and the \"packed\" attribute and I came across a wierd behavior:

struct t1
{
    int a:12;
           


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 03:27

    Your structures are not "exactly the same". Your first one has three consecutive bit-fields, the second has one bit-field, an (non bit-field) int, and then a second bit-field.

    This is significant: consecutive (non-zero width) bit-fields are merged into a single memory location, while a bit-field followed by a non-bit-field are distinct memory locations.

    Your first structure has a single memory location, your second has three. You can take the address of the b member in your second struct, not in your first. Accesses to the b member don't race with accesses the a or c in your second struct, but they do in your first.

    Having a non-bit-field (or a zero-length bit-field) right after a bit-field member "closes" it in a sens, what follow will be a different/independent memory location/object. The compiler cannot "pack" your b member inside the bit-field like it does in the first struct.

提交回复
热议问题