1

I was doing one programming problem, there I initialized the bool array like following:-

bool hash[n] = {0};

I submitted the code and I got the wrong answer. I tried to figure out what is the problem. Then I changed the above statement to following:-

bool hash[n];
fill(hash, hash + n, 0);

This gave correct answer. I didn't understand why the bool array initialization is not working.

After that just out of curiosity, I tried following:-

bool hash[n] = {0};
fill(hash, hash + n, 0);

I submitted the code and I got wrong answer. This really blew my mind. Any inputs?

prat
  • 133
  • 1
  • 7
  • is `n` compile time value or do you use VLA? – Jarod42 Apr 03 '20 at 07:20
  • no it is given at runtime – prat Apr 03 '20 at 07:24
  • 4
    @prat then `bool hash[n];` is not legal C++. In C++ array dimensions must be *compile time constants*.You are just unlucky enough to have a compiler which accepts illegal C++. – john Apr 03 '20 at 07:25
  • Of course would need to see a complete program to explain what is going on. None of your three options look wrong to me (apart from the array). – john Apr 03 '20 at 07:27
  • But obviously *not* lucky enough for it to initialise it properly :-) – paxdiablo Apr 03 '20 at 07:27
  • @john, I think C++ now supports runtime declaration of arrays, I have been using for a while, didn't get any issue. I am trying to refer to " runtime declaration of dimensions of array" – prat Apr 03 '20 at 07:33
  • @prat *I think C++ now supports runtime declaration of arrays* -- No, C++ does not support this. You are another victim of the bogus syntax. If you want a rude awakening, try your code with the Visual C++ compiler, latest and greatest version. You will see the code will not compile. – PaulMcKenzie Apr 03 '20 at 07:43
  • @prat C++ hasn't included VLAs yet, with length/size declared at runtime. However some compilers do accept such syntax, following C99. –  Apr 03 '20 at 07:45
  • 1
    most compilers need to be passed flags to be fully standard conformant, read here on vlas: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard the thing is: you don't need them, because you can use `std::vector` – 463035818_is_not_an_ai Apr 03 '20 at 07:48
  • @prat What your compiler accepts and what the C++ standard accepts are not the same thing. Every compiler is different of course, but this issue, *variable length arrays* (VLA), is a particularly notorious deviation from the C++ standard which is accepted by the g++ compiler (in it's default mode). Just think of it as a trap to catch the unwary beginner. – john Apr 03 '20 at 07:49
  • @idclev463035818: but `std::vector` is criticized a lot ^_^ – Jarod42 Apr 03 '20 at 07:51
  • @Jarod42 I stopped critizing at some point. Workarounds are simple `std::vector`, `std::vector` or `std::vector` are all not perfect but do the job. `std::vector` is not a vector of bools, but I also dont blame a stone for not being a flower :P But yeah, sorry I missed the `bool` here – 463035818_is_not_an_ai Apr 03 '20 at 07:58
  • [Visual C++ online compiler](https://rextester.com/IQIJ30200). Doesn't compile. – PaulMcKenzie Apr 03 '20 at 08:04
  • @idclev463035818: No need to worry about "missing bool". For typical programming problems, `vector` works as expected. – MSalters Apr 03 '20 at 10:39

1 Answers1

2

I was able to reproduce this error by going through a few online c++ compilers. (some of them accept VLAs following C99 and don't throw any errors)

Writing

int n=20;
bool hash[n]={0};

throws:

main.cpp:6:13: error: variable-sized object may not be initialized bool hash[n]={0}; ^ 1 error generated. compiler exit status 1

As extensively discussed in the comments above, declaring array size/length at runtime is not a feature of standard C++.

However, this would work:

int n=20;
bool hash[n];

because the array elements are not specified/initialized.

For reproducability, here is the link of the online compiler which produced this case.

Always use vectors for such cases.