C++结构大小:2+4+2+2+4 = 16

发布于 2024-08-13 21:41:57 字数 512 浏览 6 评论 0原文

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

为什么这个结构体的 sizeof(); 是 16 个字节?我正在用 g++ 编译。

struct bitmapfileheader {       
     unsigned short bfType;
     unsigned int bfSize;
     unsigned short bfReserved1;
     unsigned short bfReserved2;
     unsigned int bfOffBits;   
   };

Possible Duplicate:
Why isn’t sizeof for a struct equal to the sum of sizeof of each member?

Why is the sizeof(); of this structure 16 bytes? I'm compiling in g++.

struct bitmapfileheader {       
     unsigned short bfType;
     unsigned int bfSize;
     unsigned short bfReserved1;
     unsigned short bfReserved2;
     unsigned int bfOffBits;   
   };

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

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

发布评论

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

评论(9

半衾梦 2024-08-20 21:41:57

这是因为 4 字节整数与 4 字节边界对齐,因此 bfType 之后有 2 字节填充。

It's because the 4 byte ints are aligned to a 4 byte boundry, so there are 2 bytes of padding after bfType.

那些过往 2024-08-20 21:41:57

对齐。
可能在您的平台上,int 必须是 4 字节对齐,short 必须是 2 字节对齐。

+0 -1 : bfType
+2 -3 : <padding>
+4 -7: bfSize
+8 -9: bfReserve1
+10 -11: bfReserve2
+12 -15: bfOffBits
-------------
16 bytes

对齐很好,因为对于许多架构来说,未对齐的结构需要额外的工作。

Alignment.
Likely on your platform ints have to be 4byte aligned and shorts are 2byte aligned.

+0 -1 : bfType
+2 -3 : <padding>
+4 -7: bfSize
+8 -9: bfReserve1
+10 -11: bfReserve2
+12 -15: bfOffBits
-------------
16 bytes

Alignment is good because unaligned structures require extra work for many architectures.

以歌曲疗慰 2024-08-20 21:41:57

结构中的各个字段需要适当对齐。编译器将在结构中填充额外的空间以满足对齐要求。

如果您不想这样做,可以使用 UNALIGNED 宏。

The individual fields in a structure need to be aligned appropriately. The compiler will pad additional space in the structure in order to satisfy alignment requirements.

If you don't want this, you can use the UNALIGNED macro.

悟红尘 2024-08-20 21:41:57

我认为您的编译器对字段使用 4 字节对齐。

I think your compiler uses 4-byte allignment for the fields.

柠檬 2024-08-20 21:41:57

这个问题是由一个称为“对齐”的概念引起的。在许多情况下,希望将数字放置在该数字大小(以字节为单位)的倍数(最多可达某个最大值,通常是平台的指针大小)的地址处。如此放置的变量被称为与 n 字节边界对齐,其中 n 是数字。其具体效果取决于处理器。如果数据正确对齐,许多处理器执行数学运算的速度会更快。有些甚至无法对不适当对齐的数据执行操作(有时甚至加载操作) - 为了处理此类数据,必须将其加载到两个寄存器中,然后需要执行一系列位移和掩码以获得一个可用的价值,然后需要放回去。可以将其想象为将一半 int 存储在两个存储桶中,并且需要将它们放在一起才能使用,而不是简单地将整个 int 存储在一个存储桶中。

在您的情况下,初始 bfType 可能需要与 2 字节边界对齐,而 bfSize 可能需要与 4 字节边界对齐。编译器必须通过将整个结构对齐到 4 个字节来适应这一点,并在 bfTypebfSize 之间保留 2 个未使用的字节。

然而,当在同一系统上进行编译时,填充可能会保持一致,这可能取决于编译器选项和使用的特定 ABI(通常,在同一平台上是安全的,除非您试图使事物不兼容)。您可以自由地创建另一个具有相同前 5 个成员的结构,它们将在完全相同的位置占用另一个结构的 16 个字节。

如果您确实需要避免这种行为,则必须检查编译器文档。大多数编译器提供一个属性或关键字来将变量声明为没有对齐,并提供另一个属性或关键字来指示结构不应有填充。但在一般情况下,这些很少是必要的。

This issue comes because of a concept known as alignment. In many cases, it is desirable to have a number placed at an address that is a multiple of the size of the number in bytes (up to some maximum, often the pointer size of the platform). A variable so placed is said to be aligned to a n-byte boundary, where n is the number. The exact effects of this depend on the processor. Many processors perform math faster if the data is properly aligned. Some are even incapable of performing operations (sometimes even load operations) on unsuitably-aligned data - in order to work on such data, it has to be loaded into two registers and then a series of bit shifts and masks need to be performed to get a usable value, and then it needs to be put back. Think of it like storing half of the int in each of two buckets and needing to put them together to use it, rather than simply storing the whole int in one bucket.

In your case, the initial bfType likely needs to be aligned to a 2-byte boundary, while bfSize likely needs to be aligned to a 4-byte boundary. The compiler has to accomodate this by aligning the entire struct to 4 bytes, and leaving 2 unused bytes between bfType and bfSize.

When compiling on the same system, however, the padding is probably going to be consistent, possibly depending on compiler options and the specific ABI used (generally, you're safe on the same platform unless you are trying to make things incompatible). You can freely make another struct with the same first 5 members, and they will take up 16 bytes of the other struct, in the exact same positions.

If you really need to avoid this behavior, you will have to check your compiler documentation. Most compilers offer an attribute or keyword to declare a variable as having no alignment, and another one to indicate that a struct should have no padding. But these are rarely necessary in the general course of things.

谈下烟灰 2024-08-20 21:41:57

您可以通过编译指示打包结构以避免填充

U can pragma pack the structure to avoid padding

死开点丶别碍眼 2024-08-20 21:41:57

ISO C++03, 9.2[class.mem]/12:

在没有介入访问说明符的情况下声明的(非联合)类的非静态数据成员将被分配,以便后面的成员在类对象中具有更高的地址。由访问说明符分隔的非静态数据成员的分配顺序未指定 (11.1)。 实现对齐要求可能会导致两个相邻成员不能立即分配;管理虚拟函数 (10.3) 和虚拟基类 (10.1) 的空间要求也可能如此。

ISO C++03, 9.2[class.mem]/12:

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

〆一缕阳光ご 2024-08-20 21:41:57

由于内存分配方式的原因,短暂后会出现填充

because of the way memory is allocated, there will be padding after a short

许一世地老天荒 2024-08-20 21:41:57

这是由于对齐所致 - 编译器必须进行一些填充。

This is due to alignment - the compiler has to do some padding.

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