the size of an array must be known before compilation right?
so why does this code work?
is my compiler making it work by allocating dynamic memory or what?
1 2 3 4 5 6 7 8 9
#include <iostream>
usingnamespace std;
int main(int argc, char **argv)
{
int i = atoi(argv[1]);
int a[i];
return 0;
}
the funny thing is that if I make it a global array
it does give an error if I use a non constant expression
I see, trouble is error or not the thing works, I tried with several
numbers as arguments to the program and the array is allocated normally
I put some values in it and printed them back and it didn't give me a "segmentation fault" error
besides what is the size that is actually being used? I mean it can't be the one I give since is not
dynamic memory, can it possibly be using a default large size?
Just so you know, C++ doesn't allow it because it's a branch of C89, which didn't allow it. C99, which was publish one year after the current C++ standard, later allowed variable-sized stack arrays, and some compilers (to my knowledge, only GCC) added it to C++ as a non-standard extension.
Remember that just because a compiler allows it, it doesn't mean it's standard.