pragma pack(1) nor __attribute__ ((aligned (1))) works

后端 未结 1 1836
醉话见心
醉话见心 2020-12-08 21:58

My code used to work in the past, but now the struct size suddenly is 16 bytes. It used to be 13 bytes. I recently upgraded from Xcode 4.2 to Xcode 4.3.1 (4E1019).



        
相关标签:
1条回答
  • 2020-12-08 22:33

    Xcode uses the gcc and clang compilers which both use __attribute__((packed)) to designate struct packing.

    struct foo {
      uint8_t bar;
      uint8_t baz;
    } __attribute__((packed));
    

    Using __attribute__((aligned(1))) tells the compiler to begin each struct element on the next byte boundary but doesn't tell it how much space it can put at the end. This means that the compiler is allowed to round the struct up to a multiple of the machine word size for better use in arrays and similar. __attribute__((packed)) tells the compiler to not use any padding at all, even at the end of the struct.

    0 讨论(0)
提交回复
热议问题