[英]what happens when you create an array with non-const length in c++?
AFAIK this code is not a valid c++ code by standard: AFAIK此代码不是标准的有效c ++代码:
int a = 5;
int b[a];
but it seems many compilers can compile that code (mostly with warning) and it just behaves as expected. 但似乎很多编译器可以编译该代码(主要是警告),它只是按预期运行。 Am I wrong is is it compilers being nice to me?
我错了是编译器对我好吗?
It is called variable length array (VLA) which is not allowed by Standard C++, any version of C++, though some GCC supports this as an extension. 它被称为可变长度数组(VLA),标准C ++,任何版本的C ++都不允许这样做,尽管有些GCC支持它作为扩展。
If you're using GCC, then 如果您正在使用GCC,那么
-pedantic
option, you will see warning. -pedantic
选项编译它,你会看到警告。 -pedantic -Werror
option, you will see warning turned into error. -pedantic -Werror
选项编译它,您将看到警告变为错误。 VLA is allowed only by C99, though not by other versions of C. VLA只允许通过C99,但不能通过其他版本的C.
The compiler is being nice. 编译器很好。 :)
:)
It's actually part of the C standard, and some compilers (like GCC) extend C++ with this feature. 它实际上是C标准的一部分,一些编译器(如GCC)使用此功能扩展了C ++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.