gcc中定义了两个修改数据结构对齐方式的语句
1. #pragma pack()
2. __attribute__((packed))
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// calculate the offset of t in S
#define offsetof(S,t) (size_t)&(((S *)0)->t)
typedef struct _S1{
char a;
char b;
double c;
}S1;
//__attribute__((packed)) means no alignment optimization
typedef struct _S2{
char a;
char b;
double c;
}__attribute__((packed)) S2;
typedef struct _Y
{
int a;
int b;
char c;
char content[0];
} Y;
#pragma pack(push, 4)
struct a_4
{
short v1;
int v2;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct a_1
{
short v1;
int v2;
};