c struct 对齐
What we know is that size of a struct is the sum of all the data members. Like for the following struct,
我们知道的是, 结构的大小是所有数据成员的总和 。 对于以下结构,
struct A{
int a;
int* b;
char c;
char *d;
};
Size of the struct should be sum of all the data member, which is: Size of int a+ size of int* b +size of char c+ size of char* d
结构的大小应为所有数据成员的总和,即:int a的大小+ int * b的大小+ char c的大小+ char * d的大小
Now considering the 64-bit system,
Size of int is 4 Bytes
Size of character is 1 Byte
Size of any pointer type is 8 Bytes
(Pointer size doesn't depend on what kind of data type they are pointing too)So the size of the struct should be: (4+8+1+8)=21 Bytes
Let's see what compiler is giving using the sizeof() operator.
让我们看看使用sizeof()运算符给出的编译器内容。
#include <stdio.h>
struct A {
int a;
int* b;
char c;
char* d;
};
int main()
{
struct A a;
printf("Size of struct A: %lu\n", sizeof(struct A));
printf("Size of object a: %lu\n", sizeof(a));
return 0;
}
Output:
输出:
Size of struct A: 32
Size of object a: 32
Oops!! The output is 32 Bytes. How is that possible?
糟糕! 输出为32个字节。 那怎么可能?
It seems like the compiler took maximum size out of the data type and assigned the same memory to all data types. Is it so?
似乎编译器从数据类型中取出了最大大小,并为所有数据类型分配了相同的内存。 是这样吗?
Okay, it's quite like that, but not the same. Of course, the compiler adds padding and tries to align the data members. So for the above structure, the data alignment looks like below,
好的,就像那样,但是不一样。 当然,编译器会添加填充并尝试对齐数据成员。 因此,对于上述结构,数据对齐如下所示,
Above is the alignment of the structure A, and that's why the size of the struct is 32 Bytes. Also, the object a of type struct A is 32 Bytes.
上面是结构A的对齐方式,这就是为什么该结构的大小为32 Bytes的原因 。 同样,类型为struct A的对象a是32个字节。
编译器如何添加填充? (How compiler adds padding?)
Now the question is how compiler adds padding and align? The method is compiler dependent and kind of greedy. It aligns till the boundary of maximum memory allocated. Here we find that max memory allocated is 8 Bytes, thus all the data members acquire 8 Bytes and the total size is 32 Bytes. Now the question is will it happen every time similarly?
现在的问题是编译器如何添加填充和对齐? 该方法取决于编译器并且有点贪婪。 对齐直到分配的最大内存边界。 在这里,我们发现分配的最大内存为8字节,因此所有数据成员都获取8字节,总大小为32字节。 现在的问题是,是否每次都会同样发生?
Is it like the number of data members * max datatype size?
就像数据成员数*最大数据类型大小一样吗?
The answer is no. Check the following structure which has the same members but the ordering is different.
答案是不。 检查以下具有相同成员但顺序不同的结构。
struct B{
int* b;
char c;
int a;
char *d;
};
#include <stdio.h>
struct B {
int* b;
char c;
int a;
char* d;
};
int main()
{
struct B b;
printf("Size of struct B: %lu\n", sizeof(struct B));
printf("Size of object b: %lu\n", sizeof(b));
return 0;
}
Output:
输出:
Size of struct B: 24
Size of object b: 24
In the above structure, we find that the size is 24 Bytes though the same data members have been used. This is due to the change in the order of the member declaration. In this case, the alignment and padding would be like below:
在上面的结构中,我们发现尽管使用了相同的数据成员,但大小为24字节。 这是由于成员声明顺序的更改。 在这种情况下,对齐方式和填充将如下所示:
Above is the alignment for structure B and that's why size is 24 Bytes, instead of 32. We saw that compiler keeps aligning greedily and that's why it aligned char c & int a in the same row. When it tried to align char* d, it could not as only 3 bytes were left. But instead of char*, if it was char only then it would have aligned in the same line.
上面是结构B的对齐方式,这就是为什么大小为24字节而不是32字节的原因。我们看到编译器不断贪婪地对齐,这就是为什么它在同一行中对齐char c和int a的原因。 当它尝试对齐char * d时 ,它不能,因为只剩下3个字节。 但是,如果不是char * ,则如果只是char,则它将在同一行中对齐。
So, I hope it's clear how compiler aligns a structure. A point to be noted is that compiler can't reorder the data members though it may have reduced size. Thus, struct A will have size 32 Bytes, not 24 Bytes.
因此,我希望很清楚编译器如何对齐结构。 需要注意的一点是,尽管编译器的大小可能减小,但它不能对其重新排序。 因此, 结构A的大小为32字节,而不是24字节。
翻译自: https://www.includehelp.com/c/size-of-struct-in-c-padding-alignment-in-struct.aspx
c struct 对齐