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

前端 未结 6 1475
执念已碎
执念已碎 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:03

    We use this technique frequently to convert messages between a byte array and a structure, and have never encountered problems with it. You may have to perform endianness conversion yourself, but field order isn't a problem. If you have any concerns about data type sizes, you can always specify field size like so:

    struct foo
    {
      short someField : 16 __attribute__ ((packed));
    };
    

    This guarantees that someField will be stored as 16 bits and will not be rearranged or altered to fit byte boundaries.

提交回复
热议问题