3

In C++11 or higher regardless of compiler int myArray[10] = { 0 }; would initialize to all elements to zero. The question is would this also work in C++98 and could the compiler not decide to initialize all elements to zero? In other words could C++98 with a given compiler ignore the assigning zero to all elements?

I found this page: https://en.cppreference.com/w/cpp/language/zero_initialization which lists C++98 defects about zero initialization.

10
  • 1
    I guess this could be a duplicate: c++ array initialization, the accepted answer has a quote from standard. But a good answer here could also be welcome. Commented May 13, 2022 at 21:40
  • 1
    I don't see where you get the supposed different C++98 behavior from. Cppreference also clearly states that pre-C++11 the remaining elements are value-initialized, meaning for int zero-initialized. See en.cppreference.com/w/cpp/language/aggregate_initialization. Commented May 13, 2022 at 21:54
  • 1
    The defect reports listed on the linked page all seem irrelevant to the example. Commented May 13, 2022 at 21:58
  • 2
    @user4581301 - I enjoy the implementation, since it most closely matches how we do things without computers. string employees[10] = {"me", "son"}. Cool - there's room for 10 employees and we know the names of 2 of them. Should we initialize the other 8 positions to a copy of either of the first two, or something else. Why? That's my justification for why such behaviour can be the only reasonable one. Commented May 13, 2022 at 21:58
  • 1
    @user17732522 The defect is that in C++98, value initialization was not a thing. See: stackoverflow.com/questions/27349679/… Commented May 13, 2022 at 22:24

1 Answer 1

2

All elements would be zero. Quotes from C++98:

[dcl.init.aggr]

If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be default-initialized (8.5)


[dcl.init]

To default-initialize an object of type T means:

  • if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, the storage for the object is zero-initialized.

The meaning of default initialisation was radically different in C++98 compared to its meaning starting from C++03 where the old default initialisation essentially was renamed value initialisation and the new default initialisation became to mean "no initialisation" for trivial types.

Note that int myArray[10] = {}; would achieve the same goal. It's unnecessary to explicitly provide a value for the first element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.