C 结构中的松弛字节

发布于 2024-12-03 01:21:31 字数 559 浏览 0 评论 0原文

我知道字边界的概念,计算机使用字边界存储结构。我正在使用 64 位 CPU 和 64 位操作系统。 limit.h 中 __WORDSIZE 宏的值为 64。所以我的字边界是 8 个字节,对吗?

我有两个结构:

struct a
{
int a;
char b;
float c;
};

sizeof(struct a) 给出 12 作为答案。 sizeof(int) is 4. sizeof(float) is 4.

For struct b{
char a;
char b;
char c;
char d;
};

sizeof(struct b) is 4.

这些输出表明字边界是 4 个字节。如何找到单词边界。那么它真的等于 sizeof(int) 吗?

奇怪的是:

struct c{
char a;
char b;
char c;
char d;
char e;
}

sizeof(struct c) 是 5 个字节。谁能解释一下吗?

I know the concept of word boundary whereby computer stores structures using word boundary. I am working on a 64 bit CPU with a 64 bit OS. The value of __WORDSIZE macro in limits.h is 64. So my word boundary is 8 bytes right?

I have two structures:

struct a
{
int a;
char b;
float c;
};

sizeof(struct a) gives 12 as answer. sizeof(int) is 4. sizeof(float) is 4.

For struct b{
char a;
char b;
char c;
char d;
};

sizeof(struct b) is 4.

These outputs suggest the word boundary is 4 bytes. How do I find the word boundary. Is it really equal to sizeof(int) then?

Strangely:

struct c{
char a;
char b;
char c;
char d;
char e;
}

sizeof(struct c) is 5 bytes. Can anyone explain this please.

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

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

发布评论

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

评论(2

公布 2024-12-10 01:21:31

首先,__WORDSIZE 不在那里供您检查。切勿使用以 ___ 开头且大写字母的宏(或其他名称),除非专门记录了供您使用的宏(或其他名称)。它是标头使用的实现的内部部分,并且该实现可以在更高版本中自由删除或重命名它。其他系统甚至可能根本没有相应的宏,或者可能有不同的名称。

话虽如此,结构对齐与本机“单词”大小无关。它与结构中各个元素的对齐有关。

请参阅我对此问题的回答:

如何gcc 是否计算结构所需的空间?

First of all, __WORDSIZE is not there for you to inspect. You should never use macros (or other names) beginning with __ or _ and a capital letter unless they're specifically documented for your use. It's an internal part of the implementation used by headers, and the implementation is free to remove or rename it in a later version. Other systems may not even have a corresponding macro at all, or may have it by a different name.

With that said, structure alignment has nothing to do with the native "word" size. It has to do with the alignments of the individual elements of the structure.

See my answer to this question:

How does gcc calculate the required space for a structure?

剧终人散尽 2024-12-10 01:21:31

在 struct a 中,您有一个 4 字节整数、1 字节字符、3 个字节填充(用于将 float 与 4 字节边界对齐)以及 4 个字节用于 <代码>浮动。在某些机器上和某些结构上,char 和后面的 double 之间可能有多达 7 个字节的填充,如 struct f 中所示:

struct f
{
    char   a;
    double b;
};

在我能想到的任何情况下,struct b 都没有任何替代大小。

使用struct c,编译器可以放置连续的元素而无需任何填充,因为结构中的任何元素都不需要更严格的对齐。如果您有:

struct d
{
    short a;
    char  b;
    char  c;
    char  d;
};

预计大小为 6(如果不是 8)是合理的,因为 short 需要在 2 字节边界上对齐。

struct e
{
    int   a;
    char  b;
};

在许多平台上,sizeof(struct e) 将为 8,因为结构必须按 4 字节对齐。

__WORDSIZE 与此几乎没有关系。它不是标准化的宏。

In struct a, you have a 4-byte integer, 1 byte char, 3 bytes padding to align the float to a 4-byte boundary, and 4 bytes for the float. On some machines and for some structures, you might have as many as 7 bytes padding between a char and a following double, as in struct f:

struct f
{
    char   a;
    double b;
};

There aren't any alternative sizes for struct b under any circumstances I can think of.

With struct c, the compiler can place successive elements without any padding because none of the elements in the structure need more stringent alignment. If you had:

struct d
{
    short a;
    char  b;
    char  c;
    char  d;
};

it would be reasonable to expect the size to be 6 (if not 8), because the short needs to be aligned on a 2-byte boundary.

struct e
{
    int   a;
    char  b;
};

On many platforms, sizeof(struct e) will be 8 because the structure must be 4-byte aligned.

The macro __WORDSIZE has very little to do with any of this. It isn't a standardized macro.

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