I need to create a bool array with an unknown length passed by parameter. So, I have the following code:
void foo (int size) {
bool *boolArray = new bool[size];
for (int i = 0; i < size; i++) {
if (!boolArray[i]) {
cout << boolArray[i];
}
}
}
I thought that a boolean array was initializing with false values...
Then, if I run that code in Eclipse (on Ubuntu), it works fine for me, the function prints all the values because !boolArray[i] return true (but the values are not false values, they are garbage values). If I run it in Visual Studio, the values are garbage values too, but the function does not print any value (because !boolArray[i] returns false). Why the array values are not false values by default?!? And why !boolArray[i] returns false in Visual Studio but it returns true in Eclipse?!?
I read this question: Set default value of dynamic array, so if I change the code like the following, it works fine for me too (in Eclipse and in Visual Studio)! But I have no idea why.
void foo (int size) {
bool *boolArray = new bool[size]();
for (int i = 0; i < size; i++) {
if (!boolArray[i]) {
cout << boolArray[i];
}
}
}
Sorry for my bad English!
Thanks in advance!