给定数据类型的字节对齐要求是否保证为 2 的幂?

发布于 2024-10-19 17:57:48 字数 143 浏览 5 评论 0原文

给定数据类型的字节对齐要求是否保证为 2 的幂?

除了“否则没有意义”之外,是否有其他东西可以提供这种保证,因为它与系统页面大小不一致?

(背景:C/C++,因此请随意假设数据类型是 C 或 C++ 类型,并给出 C/C++ 具体答案。)

Is the byte alignment requirement of a given data type guaranteed to be a power of 2?

Is there something that provides this guarantee other than it "not making sense otherwise" because it wouldn't line up with system page sizes?

(background: C/C++, so feel free to assume data type is a C or C++ type and give C/C++ specific answers.)

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

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

发布评论

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

评论(6

请远离我 2024-10-26 17:57:48

对齐要求基于硬件。大多数(如果不是全部)“现代”芯片的地址都可以被 8 整除,而不仅仅是 2 的幂。过去有不可被 8 整除的芯片(我知道有 36 位架构)。

Alignment requirement are based on the hardware. Most, if not all, "modern" chips have addresses that are divisible by 8, not just a power of 2. In the past there were non-divisible by 8 chips (I know of a 36 bit architecture).

左秋 2024-10-26 17:57:48

根据 C 标准,您可以假设有关对齐的事项:

  • 任何类型的对齐要求都会除以该类型的大小(由 sizeof 确定)。
  • 字符类型 charsigned charunsigned char 没有对齐要求。 (这实际上只是第一点的特例。)

在现代现实世界中,整数和指针类型的大小是 2 的幂,并且它们的对齐要求通常等于它们的大小(唯一的例外是 long long(在 32 位机器上)。浮点有点不太统一。在 32 位机器上,所有浮点类型的对齐方式通常为 4,而在 64 位机器上,浮点类型的对齐要求通常等于类型的大小(4、8 或 16)。

结构体的对齐要求应该是其成员对齐要求的最小公倍数,但允许编译器施加更严格的对齐。然而,通常每个CPU架构都有一个包含对齐规则的ABI标准,不遵守该标准的编译器将生成无法与遵循ABI标准的编译器构建的代码链接的代码,因此对于一个CPU来说这是非常不寻常的。编译器会违反标准,除非非常特殊的用途。

顺便说一下,一个可以在任何正常编译器上运行的有用宏是:

#define alignof(T) ((char *)&((struct { char x; T t; } *)0)->t - (char *)0)

Things you can assume about alignment, per the C standard:

  • The alignment requirement of any type divides the size of that type (as determined by sizeof).
  • The character types char, signed char, and unsigned char have no alignment requirement. (This is actually just a special case of the first point.)

In the modern real world, integer and pointer types have sizes that are powers of two, and their alignment requirements are usually equal to their sizes (the only exception being long long on 32-bit machines). Floating point is a bit less uniform. On 32-bit machines, all floating point types typically have an alignment of 4, whereas on 64-bit machines, the alignment requirement of floating point types is typically equal to the size of the type (4, 8, or 16).

The alignment requirement of a struct should be the least common multiple of the alignment requirements of its members, but a compiler is allowed to impose stricter alignment. However, normally each cpu architecture has an ABI standard that includes alignment rules, and compilers which do not adhere to the standard will generate code that cannot be linked with code built by compilers which follow the ABI standard, so it would be very unusual for a compiler to break from the standard except for very special-purpose use.

By the way, a useful macro that will work on any sane compiler is:

#define alignof(T) ((char *)&((struct { char x; T t; } *)0)->t - (char *)0)
找个人就嫁了吧 2024-10-26 17:57:48

针对大小进行优化的“结构”内字段的对齐很可能处于奇怪的边界上。除此之外,您的“这没有意义”可能会适用,但我认为没有保证,特别是如果程序是小型模型,并且针对尺寸进行了优化。 - 乔

The alignment of a field inside a "struct", optimized for size could very well be on a odd boundary. other then that your "It wouldn't make sense" would probably apply, but I think there is NO guarantee, especially if the program was small model, optimized for size. - Joe

笑,眼淚并存 2024-10-26 17:57:48

该标准不需要对齐,但允许结构/联合/位字段默默地添加填充字节以获得正确的对齐。如果需要,编译器还可以自由地将所有数据类型对齐到偶数地址。

话虽如此,这取决于 CPU,并且我不相信存在对奇数地址有对齐要求的 CPU。然而,有很多 CPU 没有对齐要求,因此编译器可以将变量放置在任何地址。

The standard doesn't require alignment, but allows struct/unions/bit fields to silently add padding bytes to get a correct alignment. The compiler is also free to align all your data types on even addresses should it desire.

That being said, this is CPU dependent, and I don't believe there exists a CPU that has an alignment requirement on odd addresses. There are plenty of CPUs with no alignment requirements however, and the compiler may then place variables at any address.

零度° 2024-10-26 17:57:48

简而言之,不。这取决于硬件。

然而,大多数现代CPU要么进行字节对齐(例如,Intel x86 CPU),要么进行字对齐(例如,Motorola、IBM/390、RISC等)。

即使使用单词对齐,它也可能很复杂。例如,16 位字将在 2 字节(偶数)地址上对齐,32 位字将在 4 字节边界上对齐,但 64 位值可能只需要 4 字节对齐,而不是 8 字节对齐。 -字节对齐地址。

对于字节对齐的 CPU,它也是编译器选项的一个函数。通常可以指定结构成员的默认对齐方式(通常还可以使用特定于编译器的#pragma)。

In short, no. It depends on the hardware.

However, most modern CPUs either do byte alignment (e.g., Intel x86 CPUs), or word alignment (e.g., Motorola, IBM/390, RISC, etc.).

Even with word alignment, it can be complicated. For example, a 16-bit word would be aligned on a 2-byte (even) address, a 32-bit word on a 4-byte boundary, but a 64-bit value may only require 4-byte alignment instead of an 8-byte aligned address.

For byte-aligned CPUs, it's also a function of the compiler options. The default alignmen for struct members can usually be specified (usually also with a compiler-specific #pragma).

只是一片海 2024-10-26 17:57:48

对于基本数据类型(整数、浮点数、双精度),对齐方式通常与类型的大小相匹配。对于类/结构,对齐方式至少是其所有成员对齐方式的最低公倍数(这是标准)

  • 在 Visual Studio 中,您可以为类型设置自己的对齐方式,但它必须是2,在 1 到 8192 之间。

  • 在 GCC 中也有类似的机制,但它没有这样的要求(至少在理论上)

For basic data types (ints, floats, doubles) usually the alignment matches the size of the type. For classes/structs, the alignment is at least the lowest common multiple of the alignment of all its members (that's the standard)

  • In Visual Studio you can set your own alignment for a type, but it has to be a power of 2, between 1 and 8192.

  • In GCC there is a similar mechanism, but it has no such requirement (at least in theory)

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