I don't understand what happens regarding the zero initialization of structs that has default values for its members.
If I have these structs:
struct A {
int *a;
int b;
};
struct B {
int *a;
int b;
B() : b(3) {}
};
struct C {
int *a;
int b = 3;
};
What we can say without a doubt is:
A a;
leaves all fields uninitializedA a{};
is {nullptr, 0}B b;
andB b{};
both are {garbage, 3} (the constructor is called)
Now it's unclear what happens when I do the following, here are the results using gcc:
C c; // {garbage, 3}
C c{}; // {nullptr, 3}
The question is: does C c{};
guarantees that C::a
is initialized to nullptr
, in other words, does having default members like in C
still zero initialize the other members if I explicitly construct the object like C c{};
?
Because it's not what happens if I have a constructor that does the same thing than C
(like in B
), the other members are not zero initialized, but why? What is the difference between B
and C
?