20

It's possible to tell GCC it should not use padding for the struct. This is done using __attribute__((packed)).

typedef struct {

  uint8_t startSymbol;
  uint8_t packetType;
  uint32_t deviceId;
  uint16_t packetCRC;

} PacketData __attribute__((packed));

However, newest Xcode uses LLVM and does not recognize the attribute. How to define packed struct for LLVM?

The full description of the problem might be found here

UPDATE I'm using Xcode 4.5.1 for iOS which uses Apple LLVM 4.1 compiler. I'm getting "'packed' attribute ignored" warning in Xcode in the code example above.

Community
  • 1
  • 1
Centurion
  • 14,106
  • 31
  • 105
  • 197
  • Am I missing something or this is a very new feature removal? The last time I checked, clang supported `__attribute__((packed))`... –  Dec 03 '12 at 17:35
  • clang supports the exact same `__attribute__((packed))` pragma... – wkl Dec 03 '12 at 17:35
  • Have no idea :) I'm using Xcode 4.5.1 for iOS which uses Apple LLVM 4.1 compiler. – Centurion Dec 03 '12 at 17:37
  • @Centurion I found the answer, see the edit. –  Dec 03 '12 at 17:49

3 Answers3

34

Did you actually try it? I just tested it on my machine, and __attribute__((packed)) compiled fine using clang.

Edit: I got the same warning ("Warning: packed attribute unused") for

typedef struct {
    int a;
    char c;
} mystruct __attribute__((packed));

and in this case sizeof(mystruct) was 8.

However,

typedef struct __attribute__((packed)) {
    int a;
    char c;
} mystruct;

worked just fine, and sizeof(mystruct) was 5.

Conclusion: it seems that the attribute needs to preceed the struct label in order to get this working.

  • 1
    Yes, I did it just now. I'm using Xcode 4.5.1 for iOS which uses Apple LLVM 4.1 compiler. I'm getting "'packed' attribute ignored" warning in Xcode – Centurion Dec 03 '12 at 17:39
  • @Centurion Then check `sizeof(PacketData)` to see if the warning is moot. Clang on Linux handles the pragma just fine. – chrisaycock Dec 03 '12 at 17:43
  • @Centurion Doesn't it mean that it's recognized/supported just not used? Are you actually instantiating the structure anywhere in the code? –  Dec 03 '12 at 17:44
  • @H2CO3, You just saved my life :) Thanks a lot, your suggestion have worked and now getting correct struct member value. Thanks again :) – Centurion Dec 03 '12 at 17:55
  • 1
    Putting it on the last line works too: "} __attribute__((packed)) mystruct;" – David H Mar 18 '13 at 16:04
  • Putting the attribute at the end applies it to the _typedef_, not the _struct_ itself; that's why it doesn't work. – Jens Alfke Sep 09 '21 at 20:20
9

You can use preprocessor directive to specify byte alignment for the struct so no padding will be done by the compiler:

#pragma pack(1)

typedef struct {
char        t1;
long long   t2;
char        t3;
} struct_size_test;

#pragma options align=reset

See the answer to this question on stackoverflow.

gog
  • 1,220
  • 11
  • 30
us_david
  • 4,431
  • 35
  • 29
0

clang 3.5 on Linux -

typedef struct  __attribute__((packed)) thing1 { int blah; } THING_ONE;

worked.