gpt4 book ai didi

c++ - 放置新的和未初始化的 POD 成员

转载 作者:行者123 更新时间:2023-12-02 16:59:14 25 4
gpt4 key购买 nike

C++ 标准是否保证未初始化的 POD 成员在新放置后保留其先前的值?

或者更准确地说,根据 C++11,以下断言是否始终得到满足?

#include <cstdlib>
#include <cassert>

struct Foo {
int alpha; // NOTE: Uninitialized
int beta = 0;
};

int main()
{
void* p = std::malloc(sizeof (Foo));
int i = some_random_integer();
static_cast<Foo*>(p)->alpha = i;
new (p) Foo;
assert(static_cast<Foo*>(p)->alpha == i);
}

C++03 的答案是否相同?

最佳答案

Does the C++ standard guarantee that uninitialized POD members retain their previous value after a placement new?

Will the following assert always be satisfied according to C++11?

没有。

未初始化的数据成员具有不确定值,这与底层内存保持不变完全不同。

[C++11: 5.3.4/15]: A new-expression that creates an object of type T initializes that object as follows:

  • If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.
  • Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.

[C++11: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

[C++11: 12.1/6]: A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used (3.2) to create an object of its class type (1.8) or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement.

[C++11: 12.6.2/8]: In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then

  • if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;
  • otherwise, if the entity is a variant member (9.5), no initialization is performed;
  • otherwise, the entity is default-initialized (8.5).

(注意。12.6.2/8 中的第一个选项是如何处理您的成员(member)beta)

[C++11: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.

[C++11: 8.5/11]: If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

编译器可以选择在分配期间将底层内存清零(或以其他方式更改)。例如,众所周知, Debug模式下的 Visual Studio 会将可识别的值(例如 0xDEADBEEF)写入内存以帮助调试;在这种情况下,您可能会看到 0xCDCCDCD,它们用来表示“干净的内存”( reference )。

吗,在这种情况下?我不知道。我认为我们无法知道。

我们确实知道 C++ 并不禁止它,我相信这使我们得出了这个答案的结论。 :)

<小时/>

Is the answer the same for C++03?

是的,尽管逻辑略有不同:

[C++03: 5.3.4/15]: A new-expression that creates an object of type T initializes that object as follows:

  • If the new-initializer is omitted:
    • If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized (8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.
    • Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;
  • If the new-initializer is of the form (), the item is value-initialized (8.5);
  • If the new-initializer is of the form (expression-list) and T is a class type, the appropriate constructor is called, using expression-list as the arguments (8.5);
  • If the new-initializer is of the form (expression-list) and T is an arithmetic, enumeration, pointer, or pointer-to-member type and expression-list comprises exactly one expression, then the object is initialized to the (possibly converted) value of the expression (8.5);
  • Otherwise the new-expression is ill-formed.
<小时/>

现在,所有这些都是我对初始化规则的严格解释。

实际上,我认为您认为与放置 operator new 语法的定义存在潜在冲突的观点可能是正确的:

[C++11: 18.6.1/3]: Remarks: Intentionally performs no other action.

下面的示例解释了放置 new “对于在已知地址构造对象非常有用”。

然而,它实际上并没有讨论在已知地址构造对象的常见用法而不混合已经存在的值,但是短语“不执行其他操作”确实表明目的是让你的“不确定值”是之前内存中的任何值。

或者,它可以简单地禁止操作符本身采取任何操作,而让分配器自由地执行。在我看来,标准试图提出的重点是不分配新的内存。

无论如何,访问此数据会调用未定义的行为:

[C++11: 4.1/1]: A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior. If T is a non-class type, the type of the prvalue is the cv-unqualified version of T. Otherwise, the type of the prvalue is T.

所以这并不重要:无论如何你都无法遵守原始值

关于c++ - 放置新的和未初始化的 POD 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14659752/

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