I cannot understand the behavior of gcc 4.8.1 or Visual Studio 2015 with respect to default initialization versus value initialization.
It doesn't help that I'm trying to understand the differences between these myself and possibly running into compiler bugs?
My question is: Can someone explain this behavior? And ideally tell me what should be happening.
I have two classes:
class Foo{
int _bar;
public:
void printBar(){ cout << _bar << endl; }
};
class bar{
int ent;
public:
int getEnt(){return ent;}
};
I'm using the following code to test:
int main()
{
Foo foo;
foo.printBar();
Foo().printBar();
bar b;
cout << b.getEnt() << endl;
return 0;
}
On gcc and Visual Studio I get:
134514795
0
0
Now if I change the test code to:
int main()
{
Foo foo;
foo.printBar();
bar b;
cout << b.getEnt() << endl;
return 0;
}
gcc gives me:
0
0
And Visual Studio gives me:
50790236
51005888
n3376 quotes
8.5/11
8.5/6
8.5/10
8.5/7
8.5/5
So, in your case, there are nor static storage duration variables, nor thread-local variables, so objects
foo
andb
will be default-initialized, that means, that constructor will be called. Default-constructor (not user-defined) will not initialize members and in members will be arbitrary garbage and this arbitrary garbage may be 0 (thanks to Jarod42 for point this in comment). AndFoo().printBar();
should print 0, since object is zero-initialized.Default initialisation, of classes like this without user-defined constructors, does nothing, leaving each trivial member with an indeterminate value.
Value initialisation will zero-initialise each member.
In the first case, you're printing:
Foo foo;
Foo()
bar b;
The third one happens to be zero; perhaps because it reuses the storage of the temporary value-initialised
Foo
.In the second case, you're printing the indeterminate values of two default-initialised objects. Coincidentally, they have zero values in one case but not the other.
Both programs have undefined behaviour, since they use uninitialised values.
The logic is quite simple:
This default-initializes
foo
, and sinceFoo
's default constructor is trivial, it effectively doesn't initialize it at all, sofoo._bar
can hold any value (including 0).This value-initializes the temporary object, which in case of trivial default constructor means zero-initialization, so
Foo()._bar
is equal to 0.