gpt4 book ai didi

c++ - 保留指向已销毁对象的指针然后使用它来访问重新创建的对象(可能指向第一个子对象)是否是 UB?

转载 作者:行者123 更新时间:2023-12-04 14:48:13 25 4
gpt4 key购买 nike

考虑

struct full
{
struct basic
{
int a = 1;
} base;
int b = 2;
};

void example()
{
alignas(full) std::byte storage[/* plenty of storage */];
full * pf = new (storage) full;
basic * pb = &pf->base;

new (storage) basic; // supposedly ends ​lifetime of *pf (right?)
// if doesn't, suppose we did pf->~full(); before this line

pb->a; // is this legal?

new (storage) full; // supposedly ends ​lifetime of *pb (right?)
// if doesn't, suppose we did pb->~basic(); before this line

pb->a; // is this still legal?
pf->b; // is this legal?
pf->base.a; // is this legal?
}

我想知道以上是否合法,包括了解是否需要在每一步之前调用析构函数。

最佳答案

按照它的编写方式,您的代码具有未定义的行为,因为 pfpb 一旦对象被销毁(即在新的(存储)基础;)。实际上,编译器可以自由推测可通过这些指针在 new (storage) basic; 表达式中访问的值。例如,读取这些指针可能会产生编译器根据先前通过这些指针写入而推测的值,但不一定是通过指向新构造的对象的指针。

标准有std::launder功能来减轻这种情况。该函数有效地充当了编译器基于指针及其指向的对象进行推测的障碍。基本上,它“删除”编译器可能拥有的有关指向对象的任何知识,并返回一个好像重新获得的指针。更正后的代码如下所示:

void example()
{
alignas(full) std::byte storage[/* plenty of storage */];
full * pf = new (storage) full;
basic * pb = &pf->base;

new (storage) basic;

pb = std::launder(pb); // prevent speculation about the object pb points to
pb->a; // ok now

new (storage) full;

pf = std::launder(pf); // prevent speculation about pf and pb
pb = std::launder(pb);

// These are ok now
pb->a;
pf->b;
pf->base.a;
}

关于c++ - 保留指向已销毁对象的指针然后使用它来访问重新创建的对象(可能指向第一个子对象)是否是 UB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69577830/

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