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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Landscaper3345
  • 365
  • 2
  • 11
  • 1
    I guess this could be a duplicate: [c++ array initialization](https://stackoverflow.com/questions/5291008/c-array-initialization), the [accepted answer](https://stackoverflow.com/a/5291024/7976805) has a quote from standard. But a good answer here could also be welcome. – Yksisarvinen May 13 '22 at 21:40
  • The bit that always bugged me is `int myArray[10] = { 1 };` doesn't give an array full of 1s. I understand why it doesn't, but it still bugs me. – user4581301 May 13 '22 at 21:49
  • 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 https://en.cppreference.com/w/cpp/language/aggregate_initialization. – user17732522 May 13 '22 at 21:54
  • 1
    The defect reports listed on the linked page all seem irrelevant to the example. – user17732522 May 13 '22 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. – enhzflep May 13 '22 at 21:58
  • @user4581301 C array initialization fills any missing elements with 0 and that is what happens in C++98 too because value-initialization of int means 0. – Goswin von Brederlow May 13 '22 at 22:02
  • 1
    @user17732522 The defect is that in C++98, value initialization was not a thing. See: https://stackoverflow.com/questions/27349679/is-value-initialization-part-of-the-c98-standard-if-not-why-was-it-added-in – NathanOliver May 13 '22 at 22:24
  • A side note: [gcc](https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-std-1) and [clang](https://stackoverflow.com/questions/21870283/how-to-get-list-of-supported-standards-in-clang) don't support C++98 specifically, only C++03. [msvc](https://stackoverflow.com/questions/59449881/how-to-enable-c98-compiling-in-visual-studio-2019) doesn't even let you choose anything before C++11, so asking about C++98 specifically may be not very useful. – Yksisarvinen May 13 '22 at 22:59
  • In C++98 (due to backward compatibility with C89/90) `int myArray[10] = { 0 };` initialises all elements to zero. Essentially, if the array is initialised at all at the point of definition, any elements that aren't explicitly initialised are zero-initialised. So `int myArray[10] = { 0 };` explicitly initialises `myArray[0]` to `0` and implicitly initialises the rest of the elements to zero, while `int myArray[10] = { 1 };` explicitly initialises `myArray[0]` to `1` and implicitly initialises the rest of the elements to zero. There was no concept of value initialisation in C++ back then. – Peter May 14 '22 at 01:20
  • @Yksisarvinen Older versions of compilers did support C++98 (e.g. before 2003) and there are still real-world projects (e.g. with regulatory constraints that make updating to a modern compiler a hugely expensive exercise) that are constrained in ways that require maintaining older compilers. Not all developers have the luxury of updating to recent compilers/standards. In any event, in context of this question, C++03 and C++98 are the same. – Peter May 14 '22 at 01:28

1 Answers1

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326