C中结构体的大小

发布于 2024-08-13 13:17:13 字数 546 浏览 10 评论 0原文

可能的重复:
为什么不是结构体的 sizeof 不等于每个成员的 sizeof 之和吗?

考虑以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

原来分手还会想你 2024-08-20 13:17:13

编译器可能会添加填充以满足对齐要求。请注意,这不仅适用于结构体字段之间的填充,还可能适用于结构体的末尾(以便结构体类型的数组将使每个元素正确对齐)。

例如:

struct foo_t {
    int x;
    char c;
};

即使 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:

struct foo_t {
    int x;
    char c;
};

Even though the c field doesn't need padding, the struct will generally have a sizeof(struct foo_t) == 8 (on a 32-bit system - rather a system with a 32-bit int type) because there will need to be 3 bytes of padding after the c 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.

街道布景 2024-08-20 13:17:13

对齐到 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.

ι不睡觉的鱼゛ 2024-08-20 13:17:13

如前所述,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.

一抹微笑 2024-08-20 13:17:13

您的默认对齐方式可能是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文