C语言
C语言结构体
#pragma pack(push)
#pragma pack(1)
struct _cmd_base
{
UCHAR type;
UCHAR chk;
UINT16 len;
UCHAR *data;
};
#pragma pack(pop)
typedef struct _cmd_base CMD_BASE, * P_CMD_BASE;
int size = sizeof(CMD_BASE);
上述结构体大小size为8
#pragma pack(push)
#pragma pack(1)
struct _cmd_base
{
UCHAR type;
UCHAR chk;
UINT16 len;
UCHAR data[];
};
#pragma pack(pop)
typedef struct _cmd_base CMD_BASE, * P_CMD_BASE;
int size = sizeof(CMD_BASE);
上述结构体大小size为4
不难发现第二种结构体中data居然不占用内存,这就方便了内存直接映射结构体的操作了。