Data structure padding

前端 未结 4 446
感动是毒
感动是毒 2020-12-10 20:10

What is data structure padding in c++ and how do i check the number of bytes padded bytes?

class a { public: int x; int y; int z; };
4条回答
  •  醉话见心
    2020-12-10 20:38

    Processors require that certain types of data have particular alignments. For example, a processor might require that an int be on a 4-byte boundary. So, for example, an int could start at memory location 0x4000 but it could not start at 0x4001. So if you defined a class:

    class a
    {
    public:
        char c;
        int i;
    };
    

    the compiler would have to insert padding between c and i so that i could start on a 4-byte boundary.

提交回复
热议问题