52

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 everything is packed tight. This makes it difficult to read the data using C++ structs.

Is there a way to force a struct to be packed tight?

Example:

struct {
    short a;
    int b;
}

The above structure is 8 bytes: 2 for short a, 2 for padding, 4 for int b. However, on disk, the data is only 6 bytes (not having the 2 bytes of padding for alignment)

Please be aware the actual data structures are thousands of bytes and many fields, including a couple arrays, so I would prefer not to read each field individually.

steveo225
  • 11,394
  • 16
  • 62
  • 114

2 Answers2

70

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.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Thank you. I am currently using GCC and that worked. I left a comment about the pragma in the event we change compilers though. – steveo225 Jan 13 '14 at 18:07
  • What happens to structure inheritance? – ar2015 Mar 15 '18 at 02:06
  • 1
    IIRC only this structure will be tightly packed; the inheriting structure has to use it again. If this is inheriting from some structure that will be considered as one type and it's layout will be kept as it's defined in its declaration. All these are compiler-specific so you should read the compiler documentation. – legends2k Mar 15 '18 at 02:17
  • Neither of these things worked. I am writing a system that must read COFF binary files that have all of their fields packed together. The Windows compiler can read this data into fields that seem to be packed by default. The g++ compiler cannot do this no matter how you try to force it. – polcott Sep 11 '20 at 15:55
  • I'm sure it works; [live example](http://coliru.stacked-crooked.com/a/38dc961da49f2a8b). It's most probably an environmental issue. Check your compiler version, the flags you pass, etc. Try to dump your settings and see if there're any off values. – legends2k Sep 11 '20 at 16:26
  • struct __attribute__ ((packed)) worked for me. Thanks! – gkhanacer Apr 20 '22 at 13:02
  • The `#pragma` seems to work both in Visual Studio and GCC. – daparic May 16 '23 at 19:40
  • Yeah, as the answer called out. VC++ is the compiler inside Visual Studio (IDE). – legends2k May 22 '23 at 05:00
17

You need to use a compiler-specific, non-Standard directive to specify 1-byte packing. Such as under Windows:

#pragma pack (push, 1)

The problem is that the creator of the file took no time to properly byte align the data structures and everything is packed tight.

Actually, the designer did the right thing. Padding is something that the Standard says can be applied, but it doesn't say how much padding should be applied in what cases. The Standard doesn't even say how many bits are in a byte. Even though you might assume that even though these things aren't specified they should still be the same reasonable value on modern machines, that's simply not true. On a 32-bit Windows machine for example the padding might be one thing whereas on the 64-bit version of Windows is might be something else. Maybe it will be the same -- that's not the point. The point is you don't know what the padding will be on different systems.

So by "packing it tight" the developer did the only thing they could -- use some packing that he can be reasonably sure that every system will be able to understand. In that case that commonly-understood packing is to use no padding in structures saved to disk or sent down a wire.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 6
    What I meant by that statement is that the order of the struct's elements could have been arranged better such that alignment would have been good under many different circumstances. Not that they simply packed the bytes tight. – steveo225 Jan 13 '14 at 18:06