方法一:
使用 pragma pack(byte-alignment)
#define L1_CACHE_LINE 2
#pragma pack(L1_CACHE_LINE)
struct A
{
u_int32_t a;
u_int32_t b;
u_int16_t c;
u_int16_t d;
u_int32_t e;
};
#pragma pack()
方法二:
使用 __attribute__((aligned(byte-alignment)))
#define L1_CACHE_LINE 2
struct A
{
u_int32_t a __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int32_t b __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int16_t c __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int16_t d __attribute__ ( (aligned(L1_CACHE_LINE)) );
u_int32_t e __attribute__ ( (aligned(L1_CACHE_LINE)) );
};
// or
struct A
{
u_int32_t a ;
u_int32_t b ;
u_int16_t c ;
u_int16_t d ;
u_int32_t e ;
} __attribute__((aligned(L1_CACHE_LINE)));
//or
struct A
{
u_int32_t a ;
u_int32_t b ;
u_int16_t c ;
u_int16_t d ;
u_int32_t e ;
} __attribute__((packed));
参考 gcc manaul