gpt4 book ai didi

c++ - 为什么 char 数组的 alignof 总是 1?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:40 25 4
gpt4 key购买 nike

我正在阅读一些关于 C++ 中对齐的内容,但我不确定为什么仅包含 char 数组成员的类的对齐不是数组的大小,但事实证明始终为 1。例如

#include <iostream>

struct Foo{char m_[16];}; // shouldn't this have a 16 byte alignment?!

int main()
{
std::cout << sizeof(Foo) << " " << alignof(Foo);
}

Live on Coliru

在上面的代码中很明显sizeof(Foo)是16,但是它的对齐是1,看代码的输出。

为什么 alignof(Foo) 在这种情况下是 1?请注意,如果我将 char m_[16]; 替换为 int m_; 之类的基本类型,那么 alignof(Foo) 就会变成我想要的已经预料到了,即 sizeof(int)(在我的机器上是 4)。

如果我简单地声明一个数组 char arr[16];,也会发生同样的情况,然后 alignof(arr) 将为 1。

最佳答案

注意:数据对齐在this article中有详细解释.如果您想了解该术语的一般含义以及为什么它是一个重要问题,请阅读这篇文章。

对齐在 C++ 中被定义为一个实现定义的整数值,表示可以分配给定对象的连续地址之间的字节数 [6.11.1] Alignment .

此外对齐必须是 2 的非负整数次幂 [6.11.4] Alignment .

当我们计算结构的对齐方式时,我们必须考虑另一个规则 [6.11.5] Alignment :

Alignments have an order from weaker to stronger or stricteralignments. Stricter alignments have larger alignment values. Anaddress that satisfies an alignment requirement also satisfies anyweaker valid alignment requirement.

虽然没有直接说明,但这些规则暗示结构对齐必须至少与其最严格对齐的成员的对齐一样严格。它可以更大,但不一定是,通常也不是。

因此,当确定 OP 示例中结构的对齐方式时,结构的对齐方式必须不低于其唯一成员类型 char[16] 的对齐方式。然后通过8.3.6 [expr.alignof] :

When alignof is applied to a reference type, the result is thealignment of the referenced type. When alignof is applied to an arraytype, the result is the alignment of the element type.

alignof(char[16]) 等于 alignof(char) 由于 [6.11.6] Alignment 而通常为 1 :

(...) narrow character types shall have the weakest alignment requirement.

在这个例子中:

struct Foo
{
char c[16];
double d;
};

doublechar 具有更严格的对齐方式,因此 alignof(Foo) 等于 alignof(double)

关于c++ - 为什么 char 数组的 alignof 总是 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42523833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com