The initial value of primitive data types

#include <iostream> class MyClass { public: MyClass() : mFirst() { } int mFirst; int mSecond; }; int main() { MyClass mc; std::cout << "mc.mFirst: " << mc.mFirst << std::endl; std::cout << "mc.mSecond: " << mc.mSecond << std::endl; int a; std::cout << "a: " << a << std::endl; return 0; } 

What is the expected result of this program?

I would think that only MyClass.mFirst would be initialized to zero. However, GCC initializes them all to zero, even when optimization is turned on:

 $ g++ -o test -O3 main.cpp $ ./test mc.mFirst: 0 mc.mSecond: 0 a: 0 

I'd like to know:

  • How is each value initialized according to the C ++ standard?
  • Why does the GCC initialize them all to zero?

Update

According to Erik, the values ​​are zero because my stack contains zeros. I tried to make the stack non-zero using this construct:

 int main() { // Fill the stack with non-zeroes { int a[100]; memset(a, !0, sizeof(a)); } MyClass mc; std::cout << "mc.mFirst: " << mc.mFirst << std::endl; std::cout << "mc.mSecond: " << mc.mSecond << std::endl; int a; std::cout << "a: " << a << std::endl; return 0; } 

However, the output remains unchanged:

 mc.mFirst: 0 mc.mSecond: 0 a: 0 

Can someone explain why?

Update 2

Ok, I figured it out. GCC probably optimized unused variables.

This app shows expected behavior:

 #include <iostream> struct MyClass { MyClass() : mFirst() { } MyClass(int inFirst, int inSecond) : mFirst(inFirst), mSecond(inSecond) { } int mFirst; int mSecond; }; int main() { // Fill the stack with non-zeroes // Use volatile to prevent GCC optimizations. { volatile MyClass mc(1, 2); volatile int a = 3; } { volatile MyClass mc; volatile int a; std::cout << "mc.mFirst: " << mc.mFirst << std::endl; std::cout << "mc.mSecond: " << mc.mSecond << std::endl; std::cout << "a: " << a << std::endl; } return 0; } 

Output:

 $ g++ -o test main.cpp $ ./test mc.mFirst: 0 mc.mSecond: 2 a: 3 
+4
source share
2 answers

They (EDIT: β€œThey” refer to mSecond and a, which are not explicitly initialized) are not initialized. Your stack should be 0, so you get the values.

8.5 / 9:

If both the object and the object have a (possibly cv-qualified) non-POD class type (or array), the object should be initialized by default; if the object is of type const, the type of the base class must have a default constructor declared by the user. Otherwise, if the initializer is not set for a non-static object, the object and its subobjects, if any, have an undefined initial cost; if an object or any of its subobjects has a const-qualification type, the program is poorly formed.

+4
source

They are classified as "undefined", which means what the compiler or something else in memory in that place at the time of creation decides. In principle, leaving them uninitialized, they leave it to the mercy of fate, and you must initialize them. The only reason for not initializing members of the structure that I can think of is if you have a very large array and you do not want to call the constructor many times.

+1
source

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


All Articles