Search code examples
cgccstructcompiler-specific

A __attribute__((packed)) like attribute not GCC Specific


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.


Solution

  • 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.