解决警告方法:
#pragma warning(push)
#pragma warning(disable:4200)
#pragma pack(push)
#pragma pack(1)
typedef struct ProtocolPkt
{
unsigned char head[2]; // command: 0xff, 0xff;
unsigned char size; // bytes of the entire frame. if 0x00, one byte id code is following
unsigned char token; // command
unsigned char data[0];
} sProtocolPkt;
#pragma pack(pop)
#pragma warning(pop)
在结构体或联合中定义长度为0的数组有诸多好处:
1、指针本身需要占用内存,而长度为0的数组不需要
2、长度为0的数组定义出的缓冲区可以和结构体处在同一片连续地址中,只要一次malloc操作和free操作.如果用指针,需要分别申请和释放结构体内存和指针指向的内存块,至少需要两次以上的内存操作.