Bill
Bill

Reputation: 305

How to set struct alignment in Xcode

I need to set the struct alignment in XCode to 1-byte packing, and adding the pragma "#pragma pack(1)" doesn't seem to have any effect. Is there another way to do this? I have to read legacy data, so hand-packing is not an option. Thanks,

Bill

Upvotes: 1

Views: 3691

Answers (1)

Jaffa
Jaffa

Reputation: 12719

XCode is using GCC, so you must use GCC specific instructions : http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Type-Attributes.html

For example :

  struct S { char f[3]; } __attribute__ ((aligned (1)));

This struct will thus be aligned with a minimum of 1 byte.

You also have the packed attribute, which specify that no padding should be added.

EDIT

I forget to mention but as @Stephen Chu mentionned it, MSVC style pragma are also supported by GCC : http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

Upvotes: 2

Related Questions