'Dynamically allocated structures are well initialised?
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;
};.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |