5

In any normal compiler for C or C++, variable length arrays are working normally but in Visual Studio Community 2019, VLAs are not working. How can i in any way use Visual Studio as an IDE (becasue i like it's features) and still have VLAs in C and C++

I tried to change the compiler it uses. I tried to locate the migwin compiler but couldn't do it. All online tutorials differ from what i see in the latest version of Visual Studio 2019.

int n;
cin>>n;
int arr[n]; // This line gives an error

int arr[n]; //This line should work in Visual Studio 2019. It doesn't matter what compiler it uses. Just I need to make this thing work in VS Community 2019 because i want to use it as an IDE.

Pera
  • 173
  • 2
  • 14
  • 3
    This looks like an xy problem. Why not simply use `std::vector` instead? – G.M. Oct 19 '19 at 10:58
  • this depend not from visual studio (at all) but from compiler. *CL* not support this, but support more power [`_alloca`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/alloca?view=vs-2019) so you can use `int* arr = (int*)_alloca(n * sizeof(int))` (of course you need check value of `n`). vs2019 support also clang compiler toolset. may be this work with clang (vla) – RbMm Oct 19 '19 at 11:11
  • 2
    `any normal compiler for C or C++` are you sure? Definitely C++98 or C89 compilers won't support that. And MSVC, GCC, Clang, ICC aren't the only compilers in the world. Besides VLA isn't so good that it's not mandatory anymore in C11 – phuclv Oct 19 '19 at 12:29
  • @RbMm on Linux it's [`alloca`](http://man7.org/linux/man-pages/man3/alloca.3.html). But see [Why is the use of alloca() not considered good practice?](https://stackoverflow.com/q/1018853/995714) – phuclv Oct 19 '19 at 12:31
  • @phuclv - i know this, but not agree that use `alloca` is *always* not good. for small memory allocation (up to several kb in user mode) - the best. however this is most near to vla, where also allocation was in stack – RbMm Oct 19 '19 at 13:07
  • By any normal compiler, I meant C14 and above. I believe no one or negligible amount of people use C++98 or so when C14 is easily compatible at most of the places. I didn't know so many people use older compilers even when the newer ones are there for so long. – Pera Oct 20 '19 at 14:41
  • @G.M. std::vector does not allow to allocate a dynamic amount of bytes on the stack, and sometimes using the hepa (malloc / new, etc) is forbidden as it can break real-time constraints of a piece of code. – Jean-Michaël Celerier Dec 31 '20 at 12:40

3 Answers3

8

This answer is about C++.

Variable length array is not ISO C++ standard, some compilers accept it as an extension. e.g. gcc

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++.


Edit

Such an extension still can be a conforming implementation as long as it doesn't alter the behavior of well formed program.


If you use VLA though, then your code is not portable as other compilers may or may not have such an extension and the one that has now can stop working anytime the vendor decides to drop the support.

If you don't know the size at compile time and want to use C++ then use std::vector. You can simply change your code to:

int n;
cin>>n;
vector<int> arr(n);

However if you need to have VLA anyway here is a list of compilers support it: Live on godbolt (compilers with green mark have the support)

MSVC is not in the list. So you may use clang in your VS. Here is a tutorial. Or even easier you can use out of the box support on VS2019:

On Windows, it’s easy to install the Clang tools. Just grab the “Clang compiler for Windows,” an optional component of the “Desktop development with C++” workload. This will install everything you need to develop with Clang on Windows.

VS19 clang

Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 2
    Something cannot be both illegal and an extension. It is either illegal and a compiler that supports it is providing a variant of C++ (i.e., not standard C++), or it is legal (although not required by the C++ standard, i.e., not part of the core language required for well-formed programs) and an extension. And this answer does not speak to the question, which is not about what C or C++ supports or allows but is about using the Visual Studio interface with the effective language supported by a compiler of their choice. – Eric Postpischil Oct 19 '19 at 11:11
  • @EricPostpischil I thought we are talking about iso. I edited my answer though. – Oblivion Oct 19 '19 at 11:25
  • Something cannot be both illegal in the ISO standard and an extension. An extension is a behavior not specified, but not prohibited, but the standard. Per the C++ standard, “A conforming implementation may have extensions…” So extensions are **explicitly permitted**, not illegal. – Eric Postpischil Oct 19 '19 at 11:43
  • @EricPostpischil " error: ISO C++ forbids variable length array 'arr' [-Wvla] 9 | int arr[n];" Maybe I miss the meaning of forbidding. https://wandbox.org/permlink/w3EewWvhKtMKzkix – Oblivion Oct 19 '19 at 11:50
  • (a) This answer states both that variable length arrays are forbidden in C++ and that some compilers support them as an extension. Those cannot both be true. I do not assert at this time which is true. (b) The question is tagged both C and C++. (c) The question is primarily about using a compiler of the OP’s choice in conjunction with the Visual Studio interface. Whether any particular feature is or is not part of C or C++ is irrelevant. The question is how to get Visual Studio to accept and use the OP’s choice of compiler (or otherwise configure Visual Studio to get variable length arrays). – Eric Postpischil Oct 19 '19 at 11:53
  • @EricPostpischil you like my words or not, it is forbidden in ISO C++. Some compiler have such a support as an extension. I don't call a code C++ anymore, if I cannot use it with all the compilers I want to. I explained in the second part of my answer how to get it working in MSVC. – Oblivion Oct 19 '19 at 12:00
  • This answer does not explain in its second part how to get it working in MSVC. It links to a web page that may explain. Stack Overflow answers should contain the answer in the answer, as it is intended to be a durable repository of information, and external links may change. External links are fine for supplemental information, but they do not count as an answer. As for what you call C++ or an extension, these are defined in the C++ standard, and, when you decide to use your own different meanings, you should expect to be voted down. – Eric Postpischil Oct 19 '19 at 12:14
  • @EricPostpischil I agree that the answer should not be merely links as they can break, so I added more info from the link. I still believe it is extension and not ISO so I don't change that part. – Oblivion Oct 19 '19 at 18:32
  • Whether an extension is standard C++ or not is not a matter of belief or opinion. The standard says “A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any well-formed program.” Therefore, having an extension is in conformance with the standard, as a matter of fact. – Eric Postpischil Oct 19 '19 at 20:55
  • @EricPostpischil thanks makes sense now. I edited the iso part. I guess gcc should edit it's warning message too :) – Oblivion Oct 19 '19 at 21:22
  • 1
    thanks for the answer but i actually did all the research and also knew about clang already. The problem was i just couldn't select clang from the list of compilers because it wasn't available in the list. Just now I solved the problem by searching more and saw that we need to make a CMake Project instead of Console Application. There in the project properties there is an option to select Clang. Now VLAs are working. – Pera Oct 20 '19 at 14:27
2

VLAs can be used in "CMake Project" which are C++ applications. Create a new "CMake Project" instead of "Console Application" and then go to Project in upper-left menus and select the last option (CMake settings for ProjectName). It will open a json file. Under Toolset option, click the drop down menu to select Clang.

Above VS2019 16.1, Clang is already available. If it's not available, click "Modify" VS2019 in Visual Studio Installer and from C/C++ Development tools, select "Clang tools for windows". This will install Clang.

So the main thing is to select "CMake Project" instead of "Console Application" which is often not shown in any instructions. VLAs will work in the .cpp file now and Visual Studio 2019 can be used as an IDE will VLA support.

https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/

Pera
  • 173
  • 2
  • 14
  • 1
    What has CMake to do with variable length arrays? That's an interesting crosslink. – Wör Du Schnaffzig Jan 13 '21 at 08:26
  • The answer is fully correct but maybe hard to follow. VS compiler does not support C99 standard for VLA (E0028 expression must have a constant value). As such you need to compile with Clang which can be done either from a Visual Studio .sln (change Platform Toolset) or CMake project. CMake seems more portable. https://learn.microsoft.com/en-us/cpp/build/clang-support-cmake?view=msvc-170 – Gunnar Bernstein Nov 07 '22 at 10:38
1

In any normal compiler for C or C++, variable length arrays are working normally

Depending on how I read that sentence, it's either tautological or nonsensical. Yes, normal things work normally, but VLA arrays "work" in C++ only in the sense that they're not defined by the language.

VLAs are defined by C, and have been for 20 years. And, yes, many C compilers — but not C++ compilers — support them. A glaring exception is Microsoft's compiler, which does not. It does not conform to the C99 standard (let alone C11, C14, or C17). You could say they're a little behind. Their position seems to be that they'll implement as much of the C language as they find convenient within the context of a C++ compiler. Those parts of C that are not part of C++ don't qualify.

This much is clear: keeping the C compiler current is not a priority for Microsoft.

Just I need to make this thing work in VS Community 2019 because i want to use it as an IDE.

The simple truth of the matter is that if you want to work in modern C, you can't use Microsoft's compiler. You might be able to figure out how to configure the IDE to use the GNU compiler. At some point, though, you might start to ask if it's worth your time to wrestle with a system so hostile to your goals.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31
  • I find your answer a little bit misleading. Why would you consider the system hostile? Visual Studio had built-in support for Clang since March '19, which does support C11/C14/C17 (and at the time of writing C2X, I believe)*. It is just a matter of changing the Toolset within the VS Project file. Note: C11+ support means that newer syntax constructs are supported. The stdlib is sadly still outdated but can be easily supplemented using `musl`, for instance. – Julian Kirsch Sep 12 '20 at 02:46
  • 1
    @JulianKirsch, you may be right. Maybe "hostile" is too strong. Unsupportive? Uninterested? Indifferent? Microsoft probably isn't actually hostile toward anyone programming in C but, given the lack of support for 20+ years, it's fair to say they don't care. Note, the OP did mention the IDE problem is independent of the compiler. So, yeah, you can use a different compiler, but not the whole language in the IDE. IMO, problems like that are why emacs was invented. – James K. Lowden Sep 18 '20 at 20:58
  • I guess the whole thing just became obsolete: https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ TL;DR: MSVC will support C11 and C17 (aka informal C18) soon – Julian Kirsch Sep 18 '20 at 21:18
  • @JulianKirsch, thanks very much for the heads up! I note the fine print, though: "Astute readers will note that VLAs are also not supported." He then prattles on with 100% bogus claims about security and performance, which IMO are just a smokescreen for a business decision about the needs & wants of Microsoft's customers, not to mention Microsoft itself. – James K. Lowden Sep 28 '20 at 19:30
  • @Julian Kirsch: I thought just that. However, even if i enable the C-language standard "ISO C17 (2018)-Standard (/std:c17)" in concordance with the "/c++latest" in VS2019 Pro, variable length arrays are still not supported. Seems to be the sad fact! – Wör Du Schnaffzig Jan 13 '21 at 08:31