gpt4 book ai didi

c++ - 当数组对象的元素存储被重用时,它的生命周期是否结束?

转载 作者:行者123 更新时间:2023-12-01 14:08:08 26 4
gpt4 key购买 nike

在随后的引述中,我指的是 ISO 标准草案 N4713。

§ 6.6.3, 第 1 段说:

...The lifetime of an object o of type T ends when:
...
— the storage which the object occupies is released, or is reused by an object that is not nested within o (6.6.2).



请回答代码注释中的问题:
#include <new>

int main() {
int x[2] = {0, 1};
char* p = new (x + 1) char{0}; // Has x ended its' lifetime?
int z = x[0]; // Is this UB?
}

如果我使用 unsigned char作为数组元素类型,数组对象 x将为 *p 提供存储空间,根据第 6.6.2 节第 3 段:

If a complete object is created (8.5.2.4) in storage associated with another object e of type “array of N unsigned char” or of type “array of N std::byte” (21.2.1), that array provides storage for the created object if:
— the lifetime of e has begun and not ended, and
— the storage for the new object fits entirely within e, and
— there is no smaller array object that satisfies these constraints.



请验证我在代码注释中的陈述:
#include <new>

int main() {
unsigned char x[2] = {0, 1};
char* p = new (x + 1) char{0}; // Only x[1] have ended its' lifetime.
int z = x[0]; // This is OK.
}

虽然我不明白前面引用的最后一条规则,请举个例子?

there is no smaller array object that satisfies these constraints.

最佳答案

根据以下对标准的阅读,数组生命周期不会因放置 new 表达式而结束:

[基本生活]/1.5:

The lifetime of an object o of type T ends when:

  • [...]

  • the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).



根据这个规则,如果在数组中创建的新对象没有嵌套在数组中,则数组生命周期结束。否则它不会结束。

[介绍对象]/2

If an object is created in storage associated with a member subobject or array element e (which may or may not be within its lifetime), the created object is a subobject of e's containing object if:

  • the lifetime of e's containing object has begun and not ended, and

  • the storage for the new object exactly overlays the storage location associated with e, and

  • the new object is of the same type as e (ignoring cv-qualification).



最后两个项目符号没有填满,所以很明显,新的 char 对象不是数组的子对象。

然后 [intro.object]/3:

If a complete object is created ([expr.new]) in storage associated with another object e of type “array of N unsigned char” or of type “array of N std​::​byte” ([cstddef.syn]), that array provides storage for the created object if:

  • the lifetime of e has begun and not ended, and

  • the storage for the new object fits entirely within e, and

  • there is no smaller array object that satisfies these constraints.



所以 unsigned char 数组为新的 char 对象提供了存储空间。

[介绍对象]/4:

An object a is nested within another object b if:

  • a is a subobject of b, or

  • b provides storage for a, or

  • there exists an object c where a is nested within c, and c is nested within b.



正如我们上面提到的,第一个项目符号没有填满。第二个满载而归。所以根据[basic.life]/1.5,数组的生命周期并没有结束,因为新的char对象嵌套在数组中:
int main() {
unsigned char x[2] = {0, 1};
char* p = new (x + 1) char{0}; // x provides storage for the char object
int z = x[0]; // OK The first element is within its lifetime.
}

如果不是这种情况,可能大多数低级代码将不符合标准!

关于c++ - 当数组对象的元素存储被重用时,它的生命周期是否结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62122668/

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