Hi,
I am sure that the size of c style arrays must be known at compile tim
but the code below compiles. I am using g++ 4.2.4 and really confused!
Code:int main(){
int N = 5;
int x[N];
x[2] = 3;
}
Printable View
Hi,
I am sure that the size of c style arrays must be known at compile tim
but the code below compiles. I am using g++ 4.2.4 and really confused!
Code:int main(){
int N = 5;
int x[N];
x[2] = 3;
}
but it also works without setting the value of N like this
Code:int main(){
int N;
int x[N];
}
AFAIK that should not compile. The size of arrays must be a nonzero constant.
Now I'm confused too, as this example clearly shows that the size of x is not only unkown at compile time, it is correctly (and dynamically) sized to 12*4 or 13*4 according to i.Code:int main()
{
int N = 5;
++N;
N*=2;
int i;
std::cin >> i;
if (i==1) ++N;
int x[N];
x[2] = 3;
std::cout << N << std::endl;
std::cout << sizeof(x) << std::endl;
}
It is a g++ extension .. to turn off, compile with:
g++ -pedantic