动态分配的结构是良好初始化的吗?

发布于 2025-01-26 18:30:16 字数 719 浏览 1 评论 0 原文

对于POD结构,可以新的demo_class [const_number]()确保动态分配的结构是很好的初始化(即不是垃圾) in C ++ 11,然后 < < < /em>?

值得赞赏的是,如果某人对详细规则进行了有关初始化吊舱结构阵列的详细规则

有关于基本类型的帖子,例如 new Int {} 等。但是关于POD结构没有直接答案。

更重要的是,大多数帖子都没有提及C ++ 11和之后。

更新: 感谢Eerorika的回答。 根据所述答案,该答案说[强调我的]:

to-initialize t type t type的对象表示:

否则, 对象为零

如何完全理解这一点?结构\类可能具有许多具有不同类型的成员变量。这是否意味着每个成员变量都将是对象零始于零?例如:

struct Point
{
    int x;inty;
}; 

struct Demo
{
    Point pt; 
    double* ptr; 
    std::string str;
};.

For POD structure, could new Demo_Class[CONST_NUMBER]() guarantee the dynamically allocated structures are well initialised(i.e. not garbage) in C++11 and afterwards?

It would be appreciated that if somebody shed the light on the detailed rules about the initialization the POD structure array.

There are posts about base types, say new int{} and etc. But there is no direct answer about POD structures.

What more, most posts does not mention C++11 and afterwards at all.

UPDATE:
Thanks to eerorika's for the repid answer.
As per the said answer, which says that[emphasise mine]:

To value-initialize an object of type T means:

otherwise, the object is zero-initialized.

How to fully understand that? The struct\class may has many member variables with different types. Does it mean every member variable would be the object is zero-initialized? For example:

struct Point
{
    int x;inty;
}; 

struct Demo
{
    Point pt; 
    double* ptr; 
    std::string str;
};.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2025-02-02 18:30:16

对于POD结构, new Demo_class [const_number]()保证在C ++ 11中,动态分配的结构是很好的初始化(即不是垃圾)?

至少从C ++ 03引入值初始化(可能还通过较旧的默认初始化规则之前),至少已保证它。在C ++ 11或之后,保证尚未删除。

最新草案中的标准报价:

[dcl.init.general]

  • 如果初始化器为(),则该对象为价值。

to-initialize t type t type的对象表示:

  • 如果t是(可能是CV qualified)类类型([class]),则
    • 如果t没有默认构造函数([class.default.ctor])或用户提供或删除的默认构造函数,则该对象是默认的initialized;
    • 否则,该对象是零启动化的,并且检查了默认限制的语义约束,如果t具有非平凡的默认构造函数,则该对象是默认的initialial;
  • 如果t是数组类型,则每个元素都是值initialized;
  • 否则,该对象为零。

零启动对象或T型的引用表示:

  • 如果t是标量类型([basic.types.general]),则将对象初始化为通过将整数文字0(零)转换为t; 84
  • 而获得的值。

  • 如果t是(可能是CV符合的)非工会类类型,则将其填充位([basic.types.general])初始化为零位,每个非静态数据成员,每个非静态数据成员,每个非虚拟级基类subObject,如果对象不是基类子对象,则每个虚拟基类subobject均为零命中;
  • 如果t是(可能是CV合并的)联合类型,则将其填充位([basic.types.general])初始化为零位,并且该对象的第一个非静态数据命名成员为零initialial;
  • 如果t是数组类型,则每个元素均被零置化;
  • 如果T是参考类型,则不会执行初始化。

PS避免使用分配。考虑使用 std :: vector

For POD structure, could new Demo_Class[CONST_NUMBER]() guarantee the dynamically allocated structures are well initialised(i.e. not garbage) in C++11 and afterwards?

It has been guaranteed at least since C++03 when value initialisation was introduced (probably also before through older default initialisation rules). The guarantee hasn't been removed in C++11 nor afterwards.

Standard quotes from latest draft:

[dcl.init.general]

  • If the initializer is (), the object is value-initialized.

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type ([class]), then
    • if T has either no default constructor ([class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
    • otherwise, the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

To zero-initialize an object or reference of type T means:

  • if T is a scalar type ([basic.types.general]), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;84
  • if T is a (possibly cv-qualified) non-union class type, its padding bits ([basic.types.general]) are initialized to zero bits and each non-static data member, each non-virtual base class subobject, and, if the object is not a base class subobject, each virtual base class subobject is zero-initialized;
  • if T is a (possibly cv-qualified) union type, its padding bits ([basic.types.general]) are initialized to zero bits and the object's first non-static named data member is zero-initialized;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

P.S. Avoid using allocating new. Consider using std::vector instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文