According to my opinion, the following piece of code should not compile. Yet, it does and prints perfectly:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
In fact, it does even if I remove "const" from the parameter.
void arrayTest(const int size) {
int myArray[size];
for(int i=0; i<size; i++) {
myArray[i] = i;
}
for(int i=0; i<size; i++) {
cout << myArray[i] << " ";
}
cout << endl;
}
int main() {
arrayTest(15);
return 0;
}
I am puzzled because I know that the size of an array is a constexp which must be evaluated at compile time. Also, I do not involve any sort of dynamic allocation (i.e. I do not use malloc(), new, etc). The function's local variables are all created on the stack. No heap involvement, whatsoever!
So, why is it compiling?