Force C++ structure to pack tightly

前端 未结 2 1080
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 06:23

I am attempting to read in a binary file. The problem is that the creator of the file took no time to properly align data structures to their natural boundaries and everythi

2条回答
  •  误落风尘
    2020-11-28 07:03

    If you're using GCC, you can do struct __attribute__ ((packed)) { short a; int b; }

    On VC++ you can do #pragma pack(1). This option is also supported by GCC.

    #pragma pack(push, 1)
    struct { short a; int b; }
    #pragma pack(pop)
    

    Other compilers may have options to do a tight packing of the structure with no padding.

提交回复
热议问题