1
int function(const char *p)
{
    const int str_size = strlen(p);
    char parameter[str_size];
    ...
}

The above code compiled using Microsoft Visual Studio 2019 C compiler gives errors:

Error E0028 expression must have a constant value

Error C2057 expected constant expression

Error C2466 cannot allocate an array of constant size 0

Error C2133 'parameter': unknown size

I have compiled the same code using GCC and Clang successfully.

Is there a way to relax Microsoft C compiler permissiveness? I tried /permissive but it didn't help. Any other solution?

p.s.
Why do I need to use Microsoft compiler? Becuase I need to build a Windows Audio Processing Object (APO), and the above code is part of the static library that needs to be linked with the APO.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Danijel
  • 8,198
  • 18
  • 69
  • 133
  • 1
    No, MSVC doesn't support VLAs. – Shawn Dec 30 '20 at 11:08
  • 1
    @Danijel As far as I know MS VS C++/C does not support variable length arrays. If you are compiling your program as a C++ program then use std::vector. – Vlad from Moscow Dec 30 '20 at 11:08
  • I haven't checked the last few releases, but I don't think it supports C11's `__STDC_NO_VLA__` macro either. – Shawn Dec 30 '20 at 11:09
  • 3
    You have tagged both `[c]` and `[c++]`. They are different languages. You should detail which one you intend to use here. – Damien Dec 30 '20 at 11:12
  • I believe `MinGW` supports this, so you can use that compiler instead if you wanted. – WBuck Dec 30 '20 at 11:24
  • If the arrays are small, you might consider using `_alloca`. – Paul Sanders Dec 30 '20 at 11:35
  • 1
    [From devblogs at MS](https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc) , in short, don't hold your breath; it's not there, and there is no intention it ever will be. – WhozCraig Dec 30 '20 at 11:36
  • @WhozCraig I'm not holding my breath for C99 support. 21 years just isn't enough time! "While there is currently no support for any C11 optional features"?!?! Liars. They didn't leave out their version of the ***optional*** Annex K... – Andrew Henle Dec 30 '20 at 11:38
  • 1
    @AndrewHenle it took *forever* for them to support `stdint.h` and `inttypes.h`. Their interest in C compliance has actually gotten *better* in the last 5+ years, though by any measure of literally every other toolchain, they're still moving at a snails pace. That itself is an indicator of just how badly it rotted for such a long period of time. – WhozCraig Dec 30 '20 at 11:40
  • See: https://stackoverflow.com/questions/5246900/enabling-vlas-variable-length-arrays-in-ms-visual-c I guess you have to rewrite the source code so that it conforms to the standard. Not a big deal, I suppose. – zkoza Dec 30 '20 at 11:48

1 Answers1

4

Does Microsoft C compiler allow variable sized arrays?

No. According to Microsoft C - ANSI Conformance their compiler conforms to the standard for the C language as set forth in the 9899:1990 edition of the ANSI C standard. This edition does not include VLA:s.

Is there a way to relax Microsoft C compiler permissiveness?

It's not about permissiveness. VLA:s are simply not implemented in their compiler.

Any other solution?

To get a similar behavior (stack allocation and having the memory automatically freed when the function returns), you could use the deprecated _alloca function.

int function(const char *p)
{
    const int str_size = strlen(p);
    char* parameter = (char*) _alloca(str_size);
    ...
}

Note: A stack overflow exception is generated if the space cannot be allocated. The stack overflow exception is not a C++ exception; it is a structured exception.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108