Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Question about brace-initialization of data member array in constructor?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

  • const member 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading