为什么在c ++结构中添加填充程序?

Bry*_*Fok 2 c c++

填充程序在c ++结构中有什么影响?我经常在一些c ++ api中看到它们.例如:

struct example
{
    unsigned short a;
    unsigned short b;
    char c[3];
    char filler1;
    unsigned short e;
    char filler2;
    unsigned int g;
};
Run Code Online (Sandbox Code Playgroud)

此结构旨在通过网络传输

per*_*ain 6

struct example
{
    unsigned short a; //2 bytes
    unsigned short b;//2 bytes
            //4 bytes consumed
    char c[3];//3 bytes
    char filler1;//1 bytes
            //4 bytes consumed
    unsigned short e;//2 bytes
    char filler2;//1 bytes
            //3 bytes consumed ,should be filler[2]
    unsigned int g;//4 bytes
};
Run Code Online (Sandbox Code Playgroud)