-1

In C++, when an array is declared like that

int myArray[8] = {0,};

what does it mean?

billi90
  • 35
  • 6

2 Answers2

7

This effectively initializes all elements of an array to 0. You give the first element explicitly (0), and all that you omit are default-initialized value-initialized, which is also 0 for your type. The comma after the 0 is optional.

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
  • 7
    All the remaining elements are in fact value-initialized. If they were default-initialized, all hell would break loose. – Kerrek SB Nov 21 '14 at 15:53
  • Hmm... I'm not a language lawyer, but - for example - accepted answer for the linked question states that default initialization is used in this case - both for C and C++. – Freddie Chopin Nov 21 '14 at 16:00
  • 2
    I can't see the linked question, but it sounds like the answer is wrong, at least in C++. Default initialization of ints means no initialization (I know, it sounds silly but that is the way it is.) EDIT: I see it has just been "fixed". – juanchopanza Nov 21 '14 at 16:02
  • Here is a proof that default initialization is used: http://ideone.com/VBXyMR You can try yourself with ints - you can give only first value (for example 42), and the rest will be initialized with 0. ints have "default value" - it's just that initializing PODs on stack doesn't call their "default constructor". In C++11 you can even use this default value: `int a {};` will initialize this variable to default value of int, which is 0. – Freddie Chopin Nov 21 '14 at 16:05
  • 1
    @FreddieChopin: The standard defines `default-initialization`, `zero-initialization`, `value-initialization` and `aggregate-initialization`. You don't get to make up their definitions. `8.5 Initializers [dcl.init]`: §6 zero-initialization, §7 default-initialization, §8 value-initialize and `8.5.1 Aggregates [dcl.init.aggr]` for aggregates. – Deduplicator Nov 21 '14 at 16:06
  • 1
    That is proof of *value initialization*. It is a matter of terminology. Default initialization of an int does not initialize it. – juanchopanza Nov 21 '14 at 16:16
  • @Deduplicator - ok, here's the final (?) draft for C++11 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf on page 204 in item 7 doesn't mention "value-initialization" - `int()` given as example is an example of default initialization. Item 11 is similar an mentions default initialization of whole row with expression `float()`. – Freddie Chopin Nov 21 '14 at 16:16
  • `float()` is a *value initialization*. If you find a quote that says it is a default initialization, you should include it in your answer. – juanchopanza Nov 21 '14 at 16:20
  • @juanchopanza - personally I would say that having "int a;" on stack is not "default initialized", but "uninitialized". – Freddie Chopin Nov 21 '14 at 16:20
  • My cites stay *substantially* the same (just re-numbered slightly). And neither of those paragraphs of `[dcl.init.aggr]` you name mention default-initialization. Just because you don't like the standard terminology does not give you licence to ignore it, especially as there is good reason for it. – Deduplicator Nov 21 '14 at 16:20
  • 2
    @FreddieChopin I don't really care what you say personally. I care about what the standard says. That terminology allows me to communicate efficiently with other people who understand it without being subject to personal preferences. – juanchopanza Nov 21 '14 at 16:26
  • OK, you are right - "value" is T(), I mistakenly considered that to be "default". My confusion comes from the fact, that for a class type "default" is the same as "value" (; – Freddie Chopin Nov 21 '14 at 16:28
4

int myArray[8] = {0,};

It's the same as int myArray[8] = {0}; will populate the whole array with 0;

This is allowed in case that someone would want to add more elements to that array. For example :

int myArray[] = {

        1,
        2,
        3,
};

Can easily be populated later.

bitcell
  • 921
  • 8
  • 16