gpt4 book ai didi

c++ - 普通旧数据 - 对齐要求

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:21:55 26 4
gpt4 key购买 nike

假设我有两个具有相同初始成员序列的 POD 结构 AB,但后来有些不同(我知道,这可以通过继承轻松解决) .

struct A {
int x;
uint64_t y;
int z;
};
struct B {
int x;
uint64_t y;
int8_t z;
};

指向结构的指针需要指向初始成员(§9.2.20 [class.mem])。

现在还有两个问题:

  • 我发现标准中的哪些地方不能对成员重新排序。我很确定情况就是这样,因为结构需要与 C 完全兼容,并且对于 C 规定内存地址必须按照声明的顺序递增。
  • 两个结构中公共(public)成员的对齐方式必须相同吗?即,以下是否始终为真:

    A a;
    B b;
    assert(offsetof(A, y) == offsetof(B, y));

    如果不是:至少如果我将结构放入 union 中,那么这应该成立,因为标准(§9.2.19 [class.mem])说

    If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them.


我在这里添加一个额外的例子。

struct A {
int x;
uint64_t y;
int z;
};
struct B {
int x;
uint64_t y;
int8_t z;
};
B convertToB(A& a);
void g() {
A a;
// at this point, I cannot rely on offset(A, y) == offset(B, y)
B b = convertToB(a);
// since I do the copy over the union, offset(A, y) == offset(B, y) holds
}
union U {
A asA;
B asB;
};
B convertToB(A& a) {
U u;
// at this point, offset(A, y) == offset(B, y) holds
u.asA = a;
return u.asB;
}

因为我没有机会知道是否在某个时候, union 发生了复制,并且结构必须在任何地方都相同,所以我声称,offset(A, y) == offset(B, y) 必须持有。

最佳答案

您可以在此处找到对象成员的排序要求:

9.2/13: Nonstatic data members of a (non-union) class with the same access control are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified. 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 and virtual base classes.

在您的 struct 中,所有成员都具有相同的访问类,因此您在这里可以保证顺序。

但请注意引用的最后一句话:不保证两个连续成员之间的空间相同(尽管可能性不大)。这就是为什么我建议采用 union 方法。您不仅可以从标准的保证中受益,而且还可以在源代码中清楚地传达您的意图。


编辑

我们还可以考虑另一个方面:POD 类型是一种普通类型,具有标准布局。

9.2/16: Two standard-layout struct types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types

3.9/11: If two types T1 and T2 are the same type, then T1 and T2 are layout-compatible types.

再一次,你发现你没有任何保证,只要你的结构在某个点发散。但是我知道这可以保证 u.asB 的布局与它返回的 B 对象兼容。

最后,我希望能找到一个更具体的可复制对象的保证:

3.9/2: For any object (other than a base-class subobject) of trivially copyable type T, whether or not the object holds a valid value of type T, the underlying bytes making up the object can be copied into an array of char or unsigned char. If the content of the array of char or unsigned char is copied back into the object, the object shall subsequently hold its original value.

我认为可以从中推断出,在您的情况下,将一个值存储在 union 中但读取另一个值是可行的。但是请注意,根据此(非规范)注释,这不是一般事实:

9.5/4: [Note: In general, one must use explicit destructor calls and placement new operators to change the active member of a union. —end note]

关于c++ - 普通旧数据 - 对齐要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373006/

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