下面的代码应该生成错误,因为在编译期间编译器无法知道数组大小.
int f; std::cin >> f; int c[f]; c[100] = 5;
我正在使用gcc(Ubuntu 4.8.2-19ubuntu1)4.8.2进行编译,它不仅仅是编译,而是以某种方式运行.
怎么会发生?
解决方法
C99 accepts variable length arrays,and gcc accepts them as an extension in C90 and C++.
使用-pedantic或-Wvla将其转换为C代码中的警告,并且-Werror = vla将其转换为错误.