是否存在“int”的环境会导致结构填充? [英] Is there any environment where "int" would cause struct padding?

查看:216
本文介绍了是否存在“int”的环境会导致结构填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,这在讨论中出现了:

Specifically, this came up in a discussion:


内存消耗明智,是否有可能使用 struct 两个 int s占用的内存多于两个 int s?

Memory consuption wise, is there a possibility that using a struct of two ints take more memory than just two ints?

或者,用语言来说:

#include <iostream>

struct S { int a, b; };

int main() {
    std::cout << (sizeof(S) > sizeof(int) * 2 ? "bigger" : "the same") << std::endl;
}

是否有任何合理的 1 (不一定是常见的)或当前)这个小程序打印的环境更大

Is there any reasonable1 (not necessarily common or current) environment where this small program would print bigger?

1 澄清,我在这里的意思是系统(和编译器)以一些有意义的数量开发和生产,特别是不是为了证明这一点而构建的理论示例,或一次性原型或业余爱好者创作。

1To clarify, what I meant here is systems (and compilers) developed and produced in some meaningful quantity, and specifically not theoretical examples constructed just to prove the point, or one-off prototypes or hobbyist creations.

推荐答案


这个小程序会打印出更大的合理(不一定是普通或当前)环境吗?

Is there any reasonable (not necessarily common or current) environment where this small program would print bigger?

我不知道。我知道并不完全让人放心,但我有理由相信没有这样的环境。 C ++标准规定的要求。

Not that I know of. I know that's not completely reassuring, but I have reason to believe there is no such environment due to the requirements imposed by the C++ standard.

符合标准的匕首;编译器保存如下:

In a standard-compliant† compiler the following hold:


  • (1)数组之间不能有任何填充,因为它们可以用指针 ref ;

  • (2)标准布局结构在每个成员之后可能有也可能没有填充,但在开头,因为它们与更短但是相等的标准布局结构布局兼容 ref ;

  • (3)数组元素和结构成员正确对齐 ref ;

  • (1) arrays cannot have any padding between elements, due to the way they can be accessed with pointersref;
  • (2) standard layout structs may or may not have padding after each member, but not at the beginning, because they are layout-compatible with "shorter"-but-equal standard layout structsref;
  • (3) array elements and struct members are properly alignedref;

从(1)和(3)开始,一个类型的对齐小于或等于它的大小。如果它更大,则数组需要添加填充以使其所有元素对齐。出于同样的原因,类型的大小始终是其对齐的整数倍。

From (1) and (3), it follows that the alignment of a type is less than or equal to its size. Were it greater, an array would need to add padding to have all its elements aligned. For the same reason, the size of a type is always a whole multiple of its alignment.

这意味着在给定的结构中,第二个成员将始终正确对齐—无论int的大小和对齐方式—如果放置在第一个成员之后,即不需要插页填充。在这种布局下,结构的大小也已经是其对齐的倍数,因此也不需要尾随填充。

This means that in a struct as the one given, the second member will always be properly aligned—whatever the size and alignment of ints—if placed right after the first member, i.e., no interstitial padding is required. Under this layout, the size of the struct is also already a multiple of its alignment, so no trailing padding is required either.

没有符合标准的我们可以选择的一组(大小,对齐)值使得这个结构需要任何形式的填充。

任何这样的填充都需要不同的目的。但是,这样的目的似乎难以捉摸。假设由于某种原因需要这个填充的环境。无论填充的原因是什么,它都可能会变得很糟糕;也适用于数组,但是从(1)我们知道它不能。

Any such padding would then need a different purpose. However, such a purpose seems elusive. Suppose there is an environment that needs this padding for some reason. Whatever the reason for the padding is, it would likely‡ also apply in the case of arrays, but from (1) we know that it cannot.

但是假设这样的环境确实存在,我们想要一个C ++编译器。它可以通过简单地使int更大,即通过将填充放在内部来支持数组中额外需要的填充。这将再次允许结构与两个整数相同的大小,并让我们没有理由添加填充。

But suppose such an environment truly exists and we want a C++ compiler for it. It could support this extra required padding in arrays by simply making ints larger that much, i.e. by putting the padding inside the ints. This would in turn once more allow the struct to be the same size as two ints and leave us without a reason to add padding.

&匕首;一个编译器—甚至一个不符合标准的—得到任何这些错误都可以说是错误的,所以我会忽略它们。

† A compiler—even one otherwise not-standard-compliant—that gets any of these wrong is arguably buggy, so I'll ignore those.

‡我想在数组和结构都是原语的环境中,可能存在一些潜在的区别,它们允许我们使用无填充数组和填充结构,但同样,我不知道有任何此类事情正在使用中。

‡ I guess that in an environment where arrays and structures are primitives there might be some underlying distinction that allows us to have unpadded arrays and padded structs, but again, I don't know of any such thing in use.

这篇关于是否存在“int”的环境会导致结构填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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