如果我想在主函数中声明一个动态大小数组,可以这样做:
int m;
cin>>m;
int *arr= new int[m];
以下操作无法执行,因为编译器在编译过程中必须知道每个符号的大小,除非它是外部符号:
int m;
cin>>m;
int arr[m];
我的问题是:
Why does the compiler have to know the size of arr in the above code? It is a local symbol which is not defined in the symbol table. At runtime, the stack takes care of it(same way as m). Is it because the compiler has to ascertain the size of main() (a global symbol) which is equal to the size of all objects defined in it?
If I have a function:
int func(int m)
Could I define int arr[m] inside the function or still I would have to do
int *a= new int[m]