[英]what happens if you create a variable length array and compile using g++
Following code compiles fine since g++ allows it, but will it cause undefined behavior?以下代码编译良好,因为 g++ 允许它,但它会导致未定义的行为吗? Or my code will work fine?还是我的代码可以正常工作? what does it mean that c++ standard disallows variable length array if no error is generated on using it?如果使用时没有产生错误,c++ 标准不允许变长数组是什么意思?
#include <iostream>
using namespace std;
int main()
{
int x;
cin >> x;
char abc[x];
cout << sizeof(abc) << "\n";
return 0;
}
GCC documents its support of VLAs here ( emphasis mine ) GCC在这里记录了它对 VLA 的支持(强调我的)
Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++ . ISO C99 中允许可变长度自动 arrays,作为扩展 GCC在 C90 模式和C++ 中接受它们。
CLANG documentation too follows suit but mentions clearly that the standard doesn't accept ( emphasis mine ) CLANG文档也效仿,但明确提到该标准不接受(强调我的)
GCC and C99 allow an array's size to be determined at run time. GCC 和 C99 允许在运行时确定数组的大小。 This extension is not permitted in standard C++ .标准 C++ 中不允许此扩展。 However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs.但是,Clang 支持这种可变长度 arrays 以与 GNU C 和 C99 程序兼容。
If you would prefer not to use this extension, you can always disable it with -Werror=vla
to disallow compilation.如果您不想使用此扩展,您可以随时使用-Werror=vla
禁用它以禁止编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.