C 中的结构填充

发布于 2024-11-16 14:48:40 字数 182 浏览 2 评论 0原文

如果我在 C 中有一个结构定义,

typedef struct example 
{
  char c;
  int ii;
  int iii;
};

当我声明上述结构类型的变量时,应该分配什么内存。 例如 ee;

以及什么是结构填充以及结构填充是否存在任何风险?

If I have a structure definition in C of the following

typedef struct example 
{
  char c;
  int ii;
  int iii;
};

What should be the memory allocated when I declare a variable of the above structure type.
example ee;

and also what is structure padding and if there is any risk associated with structure padding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

亽野灬性zι浪 2024-11-23 14:48:40

尝试一下。在不同的系统上可能会有所不同。

#include <stdio.h>
#include <stddef.h> /* for offsetof */
struct example { char c; int ii; int iii; };
int main(int argc, char *argv[])
{
    printf("offsetof(struct example, c) == %zd\n", offsetof(struct example, c));
    printf("offsetof(struct example, ii) == %zd\n", offsetof(struct example, ii));
    printf("offsetof(struct example, iii) == %zd\n", offsetof(struct example, iii));
    return 0;
}

输出:

offsetof(struct example, c) == 0
offsetof(struct example, ii) == 4
offsetof(struct example, iii) == 8

请注意,sizeof(char) == 1,但在ii 字段之前有四个字节。额外的三个字节是填充。填充的存在是为了确保数据类型在处理器的正确边界上排列。

如果处理器进行未对齐的访问,可能会发生各种情况:

  • 访问速度较慢(大多数 x86)
  • 访问必须由内核处理并且速度非常慢(各种 PowerPC)(这导致某些计算机游戏在快速 PPC 上运行非常慢603 处理器,当它们在较慢的 PPC 601 处理器上运行得很好时。)
  • 程序崩溃(各种 SPARC)

我知道填充没有已知的风险。真正发生的唯一问题是使用不同编译器编译的两个程序(已知 GCC 与 MSVC 会导致此问题)使用不同的填充并且无法共享结构。如果来自不同编译器的代码链接在一起,这也可能导致崩溃。由于填充通常由平台 ABI 指定,因此这种情况很少见。

Try it. It may be different on different systems.

#include <stdio.h>
#include <stddef.h> /* for offsetof */
struct example { char c; int ii; int iii; };
int main(int argc, char *argv[])
{
    printf("offsetof(struct example, c) == %zd\n", offsetof(struct example, c));
    printf("offsetof(struct example, ii) == %zd\n", offsetof(struct example, ii));
    printf("offsetof(struct example, iii) == %zd\n", offsetof(struct example, iii));
    return 0;
}

Output:

offsetof(struct example, c) == 0
offsetof(struct example, ii) == 4
offsetof(struct example, iii) == 8

Note that sizeof(char) == 1, but there are four bytes before the ii field. The extra three bytes are padding. Padding exists to make sure that data types are lined up on the correct boundaries for your processor.

If a processor makes an unaligned access, various things can happen:

  • The access is slower (most x86)
  • The access must be handled by the kernel and is INCREDIBLY slow (various PowerPC) (This caused some computer games to run very slow on fast PPC 603 processors when they run just fine on slower PPC 601 processors.)
  • The program crashes (various SPARC)

I know of no known risks with padding. The only problem that really happens is that two programs compiled with different compilers (GCC vs MSVC has been known to cause this) use different padding and cannot share structures. This can also cause crashes if code from different compilers is linked together. Since padding is often specified by the platform ABI, this is rare.

夜吻♂芭芘 2024-11-23 14:48:40

在正常的 32 位下,它应该占用 12 个字节 - c 字段将被填充。然而,这取决于体系结构和编译器。

您始终可以使用编译器的 pragma 来声明结构的对齐方式(对于某些编译器,可以更改默认对齐方式)。

Under normal 32 bit it should take 12 bytes - the c field will be padded. This is architecture and compiler depended, however.

You can always use a pragma for the compiler to declare the alignment for the structure (and for some compilers, change the default alignment).

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