I use __attribute__((packed));
to make items of a struct
being stored in memory after another as this is critical for some low-level development.
As __attribute__((packed));
is GCC specific I wonder if there is a similar solution that works on ALL ANSI/C89/C99/C11 compilers or at least some of them.

- 322
- 4
- 9
2 Answers
There is no standard approach to accomplish what __attribute__((packed))
does. The typical solution is to use #ifdef
's to handle different compilers. You can find a few solutions to this approach at this SO post which also contains the details on the Visual C++ equivalent of __attribute__((packed))
. Alternatively, GCC supports the Windows struct packing pragmas, so if you are just concerned with Windows and GCC you could just use the Windows approach.
There is no support for features to control struct layout specified by the standard. The standard simply states that this aspect is implementation defined.
Therefore, if you do need to control layout, you will need to use compiler specific functionality. If you can find a way to avoid needing to do this at all, that would be preferable.

- 730,956
- 141
- 904
- 1,278

- 601,492
- 42
- 1,072
- 1,490
– pitastic Jul 22 '15 at 07:52