programming

C언어 - 구조체 멤버 얼라인먼트

programmer j 2019. 9. 10. 20:58

1. GNUC사용시 구조체 멤버 얼라인먼트

- 구글 검색 키워드 : arm gcc pragma pack

ARM Compiler toolchain Compiler Reference - #pragma pack(n)

Is gcc's __attribute__((packed)) / #pragma pack unsafe?

6.60.8 Structure-Packing Pragmas


- 구글 검색 키워드 : gnuc pragma push

6.62.13 Diagnostic Pragmas


사용예

#if defined (__GNUC__)
#define MY_LIB_BEGIN_PACK1            _Pragma("pack(push,1)")
#define MY_LIB_END_PACK               _Pragma("pack(pop)")
#endif


MY_LIB_BEGIN_PACK1
typedef struct _my_struct1
{
  unsigned char         field1;
  short                 field2;
  int                   field3;
} my_struct1_t;

typedef struct _my_struct2
{
  unsigned char         field1;
  int                   field2;
  int                   field3;
} my_struct2_t;

MY_LIB_END_PACK