In the following class:
struct S {
S() : B{} {}
const uint8_t B[32];
};
Are all 32 bytes of the B array guaranteed to be initialized to zero by the default constructor?
Is there any way to create an object of type S such that any element of the B array is not zero? (without const casting or reinterpretting memory). Do all forms of initialization of S lead to a zeroed B array?
>Solution :
Are all 32 bytes of the B array guaranteed to be initialized to zero by the default constructor?
Yes, B is value-initialized which for an array means each member is value-initialized – primitive types are value-initialized to 0.
Is there any way to create an object of type S such that any element of the B array is not zero?
Not as far as I know, although S still has the default copy constructor so if somehow you got an S with non-zero B, you can clone those objects.
constmember guarantees the values cannot be changed throughout the lifetime, so any non-zero value must be set at initialization which leads to the third question…
Do all forms of initialization of S lead to a zeroed B array?
Yes, S is not an aggregate (due to user-provided ctor) so there is no way how to initialize the members directly.