Does GCC's __attribute__((__packed__)) retain the original ordering?

前端 未结 6 1460
执念已碎
执念已碎 2020-12-01 10:46

Purpose

I am writing a network program in C (specifically gnu89) and I would like to simplify things by reinterpreting a certain

6条回答
  •  北海茫月
    2020-12-01 11:23

    Assuming that you are asking whether the struct members will retain the order specified in their definition, the answer is yes. The Standard requires that successive members have increasing addresses:

    Section §6.7.2.1p13:

    Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

    and the documentation for the packed attribute clearly states that only padding/alignment is affected:

    The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

提交回复
热议问题