-5

What is this declaration doing in C++? C++14

unsigned char Mem[0x1000] {0};
Paul S.
  • 41
  • 2
  • 3
    Any other languages you may be interested in? – StoryTeller - Unslander Monica Nov 26 '17 at 17:06
  • Which is the part you are having trouble with? – AntonH Nov 26 '17 at 17:08
  • So i understand that an array of 4096 unsigned chars was declared, but what is the {0} doing there? – Paul S. Nov 26 '17 at 17:11
  • If every element is being filled with a zero, why do that? Do i need to "clean" every variable I declare? – Paul S. Nov 26 '17 at 17:12
  • Also, what if the declaration went like this: unsigned char Mem[0x1000] {0x12, 0x00}; – Paul S. Nov 26 '17 at 17:14
  • Look here: https://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value As to why? Cleanliness and portability of code. – AntonH Nov 26 '17 at 17:17
  • You can give as many values as you like, and all the rest will be zeroed. A single `{}` would also work, but some compilers will issue a warning that you might have forgotten the values. That's why `{0}` seems popular. – Bo Persson Nov 26 '17 at 17:19
  • It's just that the code i posted doesn't have an equal sign = Every post in the link you gave me has an equal sign. That's my doubt. Is the equal sign then meaningless? – Paul S. Nov 26 '17 at 17:20
  • It's just that the code i posted doesn't have an equal sign = Every post in the link you gave me has an equal sign. That's my doubt. Is the equal sign then meaningless? – Paul S. Nov 26 '17 at 17:20
  • 1
    The equal sign is optional here. – Bo Persson Nov 26 '17 at 17:21
  • 1
    If your question is about the equals sign, why didn't you say that in the question, not ten comments later? – Jonathan Wakely Nov 26 '17 at 17:22
  • Thanks. First answer wasn't that helpful I guess. – Paul S. Nov 26 '17 at 17:22
  • @JonathanWakely My apologies, to be honest I did not even know what the declaration was doing even with the equal sign. Know I learnt 2 things I guess. – Paul S. Nov 26 '17 at 17:24

1 Answers1

2
unsigned char Mem[0x1000] {0};

It creates an array of 0x1000 elements, and initializes it using the initializer {0}. That sets the first element to 0 and then value-initializes the remaining elements, which sets them to 0 as well.

i.e. it's identical to this, which just value-initializes every element:

unsigned char Mem[0x1000] {};

That's also identical to saying ={}, except that before C++11 it was only possible to use the ={} form, now you can also use {} without the =

For this case:

unsigned char Mem[0x1000] {0x12, 0x00};

it sets the first element to 0x12, then the second element to 0x00, then sets the remainder to 0. i.e. it's the same as:

unsigned char Mem[0x1000] {0x12};

If every element is being filled with a zero, why do that?

In C you can't have an empty initializer, so you can't say {} and have to do {0}. That's not true in C++, so there's no reason to prefer {0} to {} when they do the same thing.

Do i need to "clean" every variable I declare?

If you don't use any initializer then the elements will be uninitialized, so have indeterminate values, and reading an element before you've assigned a value to it would be undefined behaviour.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521