dimanche 21 juin 2020

C++: Are local automatic variables initialized or not? Stroustrup example

The following example is from Stroustrup C++ 4th Ed. Page 519. It was my understanding from K&R C and earlier in the Stroustrup book that local automatic variables of primitive types are undefined or not known to be initialized. Static variables of primitive types or automatic variables of a class with a default constructor are initialized.

That would mean int b should be undefined and not known to be initialized. Yet, Stroustrup adds the following code comment saying b = 0. Is this Errata? Or perhaps some behavior of calling with the default constructor?

Thank you

struct S { 
   string a;
   int b;
};

void f(void) {
   S s0 {}; // default construction: {"",0}
   // ...
}

Aucun commentaire:

Enregistrer un commentaire