在MS Visual C++中启用VLA(可变长度数组)?

发布于 2025-01-21 20:51:47 字数 607 浏览 3 评论 0原文

如何启用在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浪推晚风 2025-01-28 20:51:47

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.

春风十里 2025-01-28 20:51:47

VLA的编写非常整洁,但是当std :: vector的动态内存分配时,您可以使用alloca()获得类似的行为。

http://msdn.microsoft.com/en-us/en-us/library/library/x9sx5da1.aspx

在您的示例中使用Alloca()将给出:

#include <stdlib.h>
#include <alloca.h>

int main(int argc, char **argv)
{
  char* pc = (char*) alloca(sizeof(char) * (argc+5));

  /* do something useful with pc */

  return EXIT_SUCCESS;
}

VLA's are much neater to write but you can get similar behaviour using alloca() when the dynamic memory allocation of std::vector is prohibitive.

http://msdn.microsoft.com/en-us/library/x9sx5da1.aspx

Using alloca() in your example would give:

#include <stdlib.h>
#include <alloca.h>

int main(int argc, char **argv)
{
  char* pc = (char*) alloca(sizeof(char) * (argc+5));

  /* do something useful with pc */

  return EXIT_SUCCESS;
}
温柔戏命师 2025-01-28 20:51:47

我遇到了同样的问题,这在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.

や莫失莫忘 2025-01-28 20:51:47

MSVC 2015不支持C99。
使用动态内存分配使用此逻辑。

#include <stdlib.h>

int main(int argc, char** argv)
{
    char* pc = (char*)malloc((argc + 5) * sizeof(char));

    /* do something useful with pc */
    
    free(pc);
    return EXIT_SUCCESS;
}

MSVC 2015 does not support C99.
Use this logic using dynamic memory allocation instead..

#include <stdlib.h>

int main(int argc, char** argv)
{
    char* pc = (char*)malloc((argc + 5) * sizeof(char));

    /* do something useful with pc */
    
    free(pc);
    return EXIT_SUCCESS;
}
誰ツ都不明白 2025-01-28 20:51:47

要使用C ++创建一个可变长度数组,使用您的示例,您将执行以下操作:

size_t size = argc + 5;
vector<char> pc(size);

如果要将其转换为STD:String:String::

string buffer(pc.begin(), pc.end());

To create a variable length array using c++, using your example, you would do something like the following:

size_t size = argc + 5;
vector<char> pc(size);

If you wanted to convert it over to std:string:

string buffer(pc.begin(), pc.end());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文