Hi,
just wondering, why does it work when I create a static array with size unknown at compile time inside a function? I.e. small example:
The code runs fine, the compiler (gcc 4.5.1 / icpc 11.1) does not complain (though if I change int a[5] to int a[n] in main, the code does not compile as expected).
Is it possibly legal because n is known when local storage is allocated on the stack when the function is called? (Though if I add std::cin>>n; before int b[n] the code still compiles, which is surprising).
Thanks
just wondering, why does it work when I create a static array with size unknown at compile time inside a function? I.e. small example:
Code:
#include <iostream>
void cpy(int a[], unsigned n){
int b[n];
std::copy(a, a+n, b);
std::cout<<b[0]<<" "<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<"\n";
}
int main(int argc, char **argv){
unsigned n;
std::cin>>n;
int a[5]={1,2,3,4,5};
cpy(a, n);
return 0;
}
Is it possibly legal because n is known when local storage is allocated on the stack when the function is called? (Though if I add std::cin>>n; before int b[n] the code still compiles, which is surprising).
Thanks
Comment