C中结构体的大小
考虑以下 C 代码:
#include <stdio.h>
struct employee
{
int id;
char name[30];
};
int main()
{
struct employee e1;
printf("%d %d %d", sizeof(e1.id), sizeof(e1.name), sizeof(e1));
return(0);
}
输出为:
4 30 36
为什么结构体的大小不等于其成员大小的总和单个组件变量?
Possible Duplicate:
Why isn’t sizeof for a struct equal to the sum of sizeof of each member?
Consider the following C code:
#include <stdio.h>
struct employee
{
int id;
char name[30];
};
int main()
{
struct employee e1;
printf("%d %d %d", sizeof(e1.id), sizeof(e1.name), sizeof(e1));
return(0);
}
The output is:
4 30 36
Why is the size of the structure not equal to the sum of the sizes of its individual component variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译器可能会添加填充以满足对齐要求。请注意,这不仅适用于结构体字段之间的填充,还可能适用于结构体的末尾(以便结构体类型的数组将使每个元素正确对齐)。
例如:
即使
c
字段不需要填充,该结构通常会有一个sizeof(struct foo_t) == 8
(在 32 位系统上 -而具有 32 位int
类型的系统),因为在c
字段之后需要有 3 个字节的填充。请注意,系统(例如 x86 或 Cortex M3)可能不需要填充,但出于性能原因,编译器可能仍会添加它。
The compiler may add padding for alignment requirements. Note that this applies not only to padding between the fields of a struct, but also may apply to the end of the struct (so that arrays of the structure type will have each element properly aligned).
For example:
Even though the
c
field doesn't need padding, the struct will generally have asizeof(struct foo_t) == 8
(on a 32-bit system - rather a system with a 32-bitint
type) because there will need to be 3 bytes of padding after thec
field.Note that the padding might not be required by the system (like x86 or Cortex M3) but compilers might still add it for performance reasons.
对齐到 6 个字节并不奇怪,因为它对齐到 4 倍数的地址。
所以基本上你的结构中有 34 个字节,下一个结构应该放在该地址上,即 4 的倍数。34 之后最接近的值是36. 这个填充区域计入结构的大小。
Aligning to 6 bytes is not weird, because it is aligning to addresses multiple to 4.
So basically you have 34 bytes in your structure and the next structure should be placed on the address, that is multiple to 4. The closest value after 34 is 36. And this padding area counts into the size of the structure.
如前所述,C 编译器将添加填充以满足对齐要求。这些要求通常与内存子系统有关。某些类型的计算机只能访问符合某个“好”值的内存,例如 4 个字节。这通常与字长相同。因此,C 编译器可能会将结构中的字段与该值对齐,以使它们更易于访问(例如,4 字节值应为 4 字节对齐)此外,它可能会填充结构的底部以排列结构后面的数据。我相信还有其他原因。更多信息可以在此维基百科页面找到。
As mentioned, the C compiler will add padding for alignment requirements. These requirements often have to do with the memory subsystem. Some types of computers can only access memory lined up to some 'nice' value, like 4 bytes. This is often the same as the word length. Thus, the C compiler may align fields in your structure to this value to make them easier to access (e.g., 4 byte values should be 4 byte aligned) Further, it may pad the bottom of the structure to line up data which follows the structure. I believe there are other reasons as well. More info can be found at this wikipedia page.
您的默认对齐方式可能是 4 个字节。要么 30 字节元素变为 32,要么整个结构向上舍入到下一个 4 字节间隔。
Your default alignment is probably 4 bytes. Either the 30 byte element got 32, or the structure as a whole was rounded up to the next 4 byte interval.