Are inline type elements initialized by default?

I recently ran into a problem with one of my classes because I did not set a pointer to NULL in my constructor initialization list, and therefore it contained garbage when I ran the program.

However, although I know that instances of a built-in type declared on the stack but not initialized will contain random values, I was sure I would read somewhere that, since class members not explicitly placed on the constructor initialization list have your default constructors, for built-in types this will also happen by inserting code similar to the pseudo-constructor that will be on most platforms, set them to zero. I also thought that I read somewhere in “Thinking in C ++” that under certain circumstances, before an object is built, its memory will be reset, however, I seem to be wrong in both cases.

Please, someone can confirm me,
a) Initialization of elements of a built-in type has something to do with whether a user-defined constructor is defined or not, b) it is always necessary to initialize elements of a built-in type manually, and
c) do any circumstances in which the storage of an object is reset before calling the constructor?

Also, examining this, I saw that the terms "default-initialised" and "zero-initialised" are used - is there a difference between the statement:

T a;

and

T a();

? I thought the first form was just used to prevent ambiguity when the second could be accepted by the compiler as a function declaration.

Thanks so much for your time,

stellarpower

+4
3

.

T a1;            // default initialization
T a2{};          // value initialization
T();             // also value initialization
new T;           // default initialization
new T();         // value initialization
new T{};         // also value initialization
class C1 {
    C1() {}
    T x;
};               // no initializer for C1::x; default-initialized
class C2 {
    T x;
};               // implicit default constructor default-initializes C2::x
class C3 {
    C3() : x() {}
    T x;
};               // C3::x will be value-initialized.
class C4 {
    C4() : x{} {}
    T x;
};               // C4::x will also be value-initialized.
// DANGER
T a();           // declares a function; not value initialization (quirk of C++)

, , , , () {}, . , , .

0. null. . , , .

a) -, ,

default - . , mem-. C1 C2 . , , , , . . "" .

C1 y1;   // y1 is default-initialized; y1.x is indeterminate
C1 y2{}; // y2 is value-initialized;   y2.x is indeterminate
C2 y3;   // y3 is default-initialized; y3.x is indeterminate
C2 y4{}; // y4 is value-initialized;   y4.x is set to 0
C3 y5;   // y5 is default-initialized; y5.x is set to 0
C3 y6{}; // y6 is value-initialized;   y6.x is set to 0

b) , c) - , ?

, " ". , 0: , ( ). , . , , .

+12

, , . , . , , , . - ( , ). , , . , , 0.

,

T a;

a T.

T a();

a(), T .

+1

- - POD. . POD

class Foo {
    int num1;
    double num2;
    bool b;
    int *pnum;
};

Foo foo = {};

(b false, pnum NULL ..), ( ).

,

a) POD ,

b) POD,

c) POD, .

Another way to create an instance of the class and zero initialization without calling the constructor is to allocate the necessary memory using calloc and direct the pointer to the required type. You should avoid this if absolutely necessary!

+1
source

Source: https://habr.com/ru/post/1605607/


All Articles