Why padding are added, if char comes after int?

后端 未结 4 627
醉酒成梦
醉酒成梦 2020-12-03 19:11

For example, there is a structure

struct A
{
char a;
int i;
};

In this case, we have a[1 byte] + padding[3 byte] + int[4 byte] = 8.

4条回答
  •  隐瞒了意图╮
    2020-12-03 19:36

    Not only each member of the struct has to be data aligned, but the struct itself has to aligned to the size of the largest member in the struct. So, padding is added to struct A such that its size should be a multiple of the larger of sizeof i and sizeof a.

    Have a look at C FAQ here

提交回复
热议问题