简体   繁体   中英

Are unpacked struct in packed struct automatically packed?

Are unpacked struct in packed struct automatically packed by GCC?

In other words, do __packed__ attribute automatically propagates to nested structures?

That is to say:

struct unpackedStruct{
    int16_t field1;
    int32_t field2;
    // etc...
}

struct packedStruct{
    int16_t field1;
    struct unpackedStruct struct1; // <-- Is this struct packed?
    // etc...
} __attribute__((__packed__));

No, the inner structure is not packed. In this Godbolt example , we can see that struct foo is not packed inside struct bar , which has the packed attribute; the struct bar object created contains three bytes of padding (visible as .zero 3 ) inside its struct foo member, between the struct foo members c and i .

Current documentation for GCC 10.2 explicitly says the internal layout of a member of a packed structure is not packed (because of the attribute on the outer structure; it could be packed due to its own definition, of course).

(In older documentation that said that applying packed to a structure is equivalent to applying it to its members, it meant the effect of applying packed to the “variable” that is the member, described in the documentation for variable attributes . When packed is applied to a structure member, it causes the member's alignment requirement to be one byte. That is, it eliminates padding between previous members and that member, because no padding is needed to make it aligned. It does not alter the representation of the member itself. If that member is an unpacked structure, it remains, internally, an unpacked structure.)

In practice it does not: see https://godbolt.org/z/4YMaz8 . Note the .zero 2 of padding between the two members of the unpackedStruct member.

This situation is explicitly mentioned in the manual , with an example almost identical to yours:

In the following example struct my_packed_struct 's members are packed closely together, but the internal layout of its s member is not packed —to do that, struct my_unpacked_struct needs to be packed too.

struct my_unpacked_struct
 {
    char c;
    int i;
 };

struct __attribute__ ((__packed__)) my_packed_struct
  {
     char c;
     int  i;
     struct my_unpacked_struct s;
  };

The basic idea is that every object of a given type should have the same layout, so that code operating on that type will work on every object of that type. So packed has to apply to a type , and you can't have some objects of that type packed and others not.

No - packing is not recursive so every member needs to be packed itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM