Is a member variable of a built-in type inside a global object zero initialized?

To use an uninitialized object of a built-in type with automatic storage time, undefined behavior is used. Of course, I highly recommend always initializing member variables of the built-in type inside the class. Despite this, I assume that a member of the built-in type without an initializer is always initialized to zero if the corresponding object of the class type has a static storage duration (i.e., a Global object). My assumption is that the full memory of an object of type class with a static storage duration is reset to zero.

Example:

#include <iostream> using namespace std; class Foo { public: int bar; }; Foo a; int main() { Foo b; cout << "a.bar " << a.bar << "\n"; cout << "b.bar " << b.bar << "\n"; return 0; } 

Compile:

 $ g++ -o init init.cpp -Wall -pedantic # gcc 7.2.1 init.cpp: In function 'int main()': init.cpp:14:31: warning: 'b.Foo::bar' may be used uninitialized in this function [-Wmaybe-uninitialized] cout << "b.bar " << b.bar << "\n"; ^~~~ 

GCC only complains about a member, an object of type class with a duration of automatic storage of b.bar, not a.bar. So, am I right?

Feel free to change the name of this question.

thanks

+5
source share
2 answers

As the comment says, it is initialized to zero, [basic.start.init] / 3 :

Variables with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) must be initialized to zero ([dcl.init]) before any other initialization begins. [...]

And zero initializing the object, zero initializing all its non-static data elements and padding bits, [dlc.init] /6.2 :

For zero initialization of an object or reference of type T means: [...]

  • if T is a (possibly cv-qualified) non-unit class, each non-static data element and each subobject of the base class is initialized to zero, and padding is initialized to zero bits, [...]

So, as you said, the total memory of the object is zeroed out (a bit belonging to its representation of the value and its fill bits).

+1
source

Do not initialize an object of a built-in type with automatic storage time undefined.

No, that is not right. See [dcl.init] / 12 :

If no initializer is specified for the object, the object is initialized by default. When storage for an object with automatic or dynamic storage duration is obtained, the object has an undefined value, and if the object is not initialized, this object retains an undefined value until this value is replaced ([expr.ass]). [Note. Objects with a static or storage duration of threads are zero-initialized, see [Basic.start.static]. - end note] If an undefined value is created by an estimate, the behavior is undefined, except in the following cases:

Foo b; behavior is defined, cout << "b.bar " << b.bar << "\n"; (before b.bar has a given value) - this is undefined behavior.

GCC only complains about a member, an object of type class with a duration of automatic storage of b.bar, not a.bar.

The GCC family of alerts related to uninitialized variables is provided for convenience and often contains false alarms. In the general case, it is impossible to get accurate warnings for each scenario (it is also difficult to solve the stop problem). It just lets you know that you are trying to use a value that you have not explicitly initialized, that is, an undefined value that is probably not the way you wanted.

+1
source

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


All Articles