我一直认为c++中不允许使用可变长度数组(引用:Why aren't variable-length arrays part of the C++ standard?)。但是为什么这段代码可以编译并运行?
#include <iostream>
using namespace std;
int main () {
int n;
cin >> n;
int a[n];
for (int i=0; i<n; i++) {
a[i] = i;
}
for (int i=0; i<n; i++) {
cout << a[i] << endl;
}
}
最佳答案
当前的 C++ 标准不要求编译器支持 VLA。但是,允许编译器供应商将 VLA 作为扩展来支持。例如,GCC >= 4.7。
最初提议 VLA 出现在 C++14 中,但该提议没有成功。它们最终也没有出现在 C++17 中。
关于c++ - C++中是否有可变长度数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22013444/