包含双字段的结构的大小 [英] Size of struct containing double field

查看:33
本文介绍了包含双字段的结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我了解结构中的字节填充.但是我还有一个小测试在结构中包含一个双字段,我不知道如何解释:

Firstly, I understand byte padding in structs. But I have still a small test contain a double field in struct and I don't know how to explain this :

typedef struct {
    char a;
    double b;
}data;

typedef struct{
    char a;
    int b;
}single;

int main(){
    printf("%d\n",sizeof(double));
    printf("%d\n",sizeof(single));
    printf("%d\n",sizeof(data));
}

通过这个测试,答案是:8 816.

Through out this test, the answer is : 8 8 and 16.

为什么这个结果让我思考?

Why this result make me thinking ?

通过第二次测试,我们可以看到我机器上的单词大小为 4 个字节.

By second test, we can see size of word on my machine is 4 bytes.

通过第一次测试,我们可以看到 double 的大小为 8 个字节.

By first test, we can see size of double is 8 bytes.

因此,在结构 data 处:结果应该是 12 个字节:char 4 个字节,double 8 个字节.

So, at the struct data : the result should be 12 bytes : 4 bytes for char and 8 bytes for double.

但是,我不知道为什么结果是 16 个字节.(对我来说很奇怪)

But, I don't know why the result is 16 bytes. (So strange with me)

请帮我解释一下,谢谢:)

Please explain it for me, thanks :)

推荐答案

通常用于在结构中布置数据的过程本质上是这样的:

The procedure typically used for laying out data in a struct is essentially this:

  • 设置偏移量 = 0.
  • 对于结构中的每个成员:设 A 为其对齐要求(例如,1、2、4 或 8 个字节,可能更多).向 Offset 添加使其成为 A 的倍数所需的字节数.(假设 A 是 2 的幂,这可以通过 Offset += -Offset & A-1 来完成,假设否定的二进制补码.)将 Offset 的当前值分配为该成员的偏移量.将成员的大小添加到 Offset.
  • 处理完所有成员后:让A成为任何成员的最大对齐要求.向 Offset 添加使其成为 A 的倍数所需的字节数.Offset 的最终值是结构的大小.
  • Set Offset = 0.
  • For each member in the struct: Let A be its alignment requirement (e.g., 1, 2, 4, or 8 bytes, possibly more). Add to Offset the number of bytes needed to make it a multiple of A. (Given that A is a power of two, this can be done with Offset += -Offset & A-1, assuming two’s complement for the negation.) Assign the current value of Offset to be the offset of this member. Add the size of the member to Offset.
  • After processing all members: Let A be the greatest alignment requirement of any member. Add to Offset the number of bytes needed to make it a multiple of A. This final value of Offset is the size of the struct.

正如 Earnest Friedman-Hill 所说,最后一步在结构的末尾添加填充,以便在它们的数组中,每个结构都以所需的对齐方式开始.

As Earnest Friedman-Hill stated, the last step adds padding to the end of the struct so that, in an array of them, each struct begins at the required alignment.

所以,对于像 struct { char c; 这样的结构体.双d;int32_t i;},在一个典型的实现中,你有:

So, for a struct such as struct { char c; double d; int32_t i; }, on a typical implementation, you have:

  • 将偏移量设置为 0.
  • char 要求对齐为 1,因此 Offset 已经是 1 的倍数(0•1 为 0).将 c 置于该偏移量 0 处.将 c 的大小 1 与 Offset 相加,使其为 1.
  • double 要求对齐为 8,因此将 7 添加到 Offset,使其成为 8.将 d 放在此偏移处,8.将 d 的大小,8,添加到 Offset,使其成为 16.
  • int 需要 4 对齐,因此 Offset 已经是 4 的倍数(4•4 是 16).将 i 放在这个偏移量 16 处.将 i 的大小 4 添加到偏移量,使其为 20.
  • 最后,需要的最大对齐是 8,所以在 Offset 上加上 4,使其成为 24.这个结构体的大小是 24 字节.

请注意,以上与机器的任何字长无关.它只使用每个成员的对齐要求.每个类型的对齐要求可以不同,也可以与类型的大小不同,以上仍然有效.

Observe that the above has nothing to do with any word size of the machine. It only uses the alignment requirement of each member. The alignment requirement can be different for each type, and it can be different from the size of the type, and the above will still work.

(如果对齐要求不是 2 的幂,则算法中断.可以通过使最后一步将偏移量增加为所有对齐的最小公倍数的倍数来解决这个问题.)

(The algorithm breaks if alignment requirements are not powers of two. That could be fixed by making the last step increase the offset to be a multiple of the least common multiple of all the alignments.)

这篇关于包含双字段的结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆