在MS Visual C++中启用VLA(可变长度数组)?
如何启用在MS Visual C ++中定义的VLA,可变长度数组的使用,或者根本无法使用?
是的,我知道C ++标准是基于C89,并且VLA在C89标准中不可用,因此在C ++中不可用,但是MSVC ++也应该是C编译器,可以使用 / / / / / / / compiler进行。 TC编译器参数(编译为C代码(/TC)
)。但是,在构建与C ++时,这样做似乎并没有启用VLA,并且编译过程失败(编译为C ++代码(/TP)
)。也许MSVC ++ C编译器仅符合C89,或者我缺少某些内容(某些特殊的构造或Pragma/Define)?
代码样本:
#include <stdlib.h>
int main(int argc, char **argv)
{
char pc[argc+5];
/* do something useful with pc */
return EXIT_SUCCESS;
}
编译错误:
错误C2057:预期常数表达
错误C2466:无法分配常数大小0
的数组错误C2133:'PC':未知大小
How can I enable the use of VLAs, variable length arrays as defined in C99, in MS Visual C++ or that is not possible at all?
Yes I know that the C++ standard is based on C89 and that VLAs are not available in C89 standard and thus aren't available in C++, but MSVC++ is supposed to be a C compiler also, a behavior that can be switched on using the /TC compiler parameter (Compile as C Code (/TC)
). But doing so does not seem to enable VLAs and the compiling process fails with the same errors when building as C++ (Compile as C++ Code (/TP)
). Maybe MSVC++ C compiler is C89 compliant only or I am missing something (some special construct or pragma/define)?
Code sample:
#include <stdlib.h>
int main(int argc, char **argv)
{
char pc[argc+5];
/* do something useful with pc */
return EXIT_SUCCESS;
}
Compile errors:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'pc' : unknown size
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
MSVC不是C99编译器,不支持可变长度阵列。
在 https://learn.microsoft.com/en.microsoft.com/en-en-us/ CPP/C语言/ANSI符合性 MSVC记录为符合C90。
MSVC is not a C99 compiler, and does not support variable length arrays.
At https://learn.microsoft.com/en-us/cpp/c-language/ansi-conformance MSVC is documented as conforming to C90.
VLA的编写非常整洁,但是当
std :: vector
的动态内存分配时,您可以使用alloca()
获得类似的行为。http://msdn.microsoft.com/en-us/en-us/library/library/x9sx5da1.aspx
在您的示例中使用
Alloca()
将给出:VLA's are much neater to write but you can get similar behaviour using
alloca()
when the dynamic memory allocation ofstd::vector
is prohibitive.http://msdn.microsoft.com/en-us/library/x9sx5da1.aspx
Using
alloca()
in your example would give:我遇到了同样的问题,这在MS Visual C ++ 2015中是不可能的,而是可以使用向量进行几乎相同的操作,唯一的差异是可忽略的Heap Resource Manage Manage Manage Arawine(new/Delete)。
尽管VLAS很方便,但是从堆栈溢出风险的堆栈中分配非确定性的内存通常不是一个好主意。
I met same problem, this is not possible in MS Visual C++ 2015, instead you can use vector to do almost the same, only difference is neglectable overhead of heap resource manage routine(new/delete).
Although VLAs is convenient, but to allocate non-deterministic amount of memory from the stack at risk of stack overflow is generally not a good idea.
MSVC 2015不支持C99。
使用动态内存分配使用此逻辑。
MSVC 2015 does not support C99.
Use this logic using dynamic memory allocation instead..
要使用C ++创建一个可变长度数组,使用您的示例,您将执行以下操作:
如果要将其转换为STD:String:String::
To create a variable length array using c++, using your example, you would do something like the following:
If you wanted to convert it over to std:string: