在 MS Visual C++ 中启用 VLA(可变长度数组)?
如何启用 VLA、C99 中定义的可变长度数组、MS Visual C++ 中的使用,或者根本不可能?
是的,我知道 C++ 标准基于 C89,并且 VLA 在 C89 标准中不可用,因此在 C++ 中不可用,但 MSVC++ 也应该是 C 编译器,可以使用 / 来打开这种行为TC 编译器参数(编译为 C 代码 (/TC)
)。但这样做似乎并没有启用 VLA,并且编译过程失败,并在构建为 C++ 时出现相同的错误(编译为 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-us/ cpp/c-language/ansi-conformance 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/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 中是不可能的,相反你可以使用向量来做几乎相同的事情,唯一的区别是堆资源管理例程(新建/删除)的开销可以忽略不计。
虽然 VLA 很方便,但是从堆栈中分配不确定数量的内存而冒着堆栈溢出的风险通常不是一个好主意。
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:
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: