4

I understand in VS all variables must be declared at the top of a block, but if I want a VLA, ie. if I wanted to do something like this:

int result = runalgorithm(); 

int vla[result];

the code above is invalid, because vla must be declared at the top. What is a good solution for this, other than creating an arbitrarily large array?

Natasha Dutta
  • 3,242
  • 21
  • 23
dave
  • 41
  • 1
  • 3
  • 1
    `int* vla = malloc ( result * sizeof *vla);` and after its use, `free(vla)` – Spikatrix Jun 09 '15 at 14:48
  • Visual Studio in the past [did not support VLA](http://stackoverflow.com/q/18521398/1708801) and as far as I know still does not. – Shafik Yaghmour Jun 09 '15 at 14:50
  • @Shafik Yaghmour: Thankfully it does support [_alloca](https://msdn.microsoft.com/en-us/library/wb1s57t5.aspx) which serves much the same purpose. – doynax Jun 09 '15 at 14:52
  • thank you cool guy. this should work to solve my issue. – dave Jun 09 '15 at 14:54
  • I think this feature is very bad practice. Use `malloc` all times or don't use C language. – i486 Jun 09 '15 at 14:55
  • @i486 IMHO, you should mention that is your _personal opinion_. There is nothing _inherently_ wrong trying to use VLA. – Natasha Dutta Jun 09 '15 at 14:58
  • 1
    C89/C90 requires a block to contain a sequence of declarations followed by a sequence of statements. C99 relaxes that requirement and permits declarations and statements to be mixed. Both lines of code in your question are declarations, so they don't violate the C89/C90 restriction. The problem is that C89/C90 doesn't support VLAs. – Keith Thompson Jun 09 '15 at 15:45
  • @Natasha Dutta - You can see "I think" in my comment. What does it mean? Maybe you have little knowledge of C or other programming languages when you cannot understand my opinion. – i486 Jun 10 '15 at 12:46
  • @i486 I don't know how, but maybe I just missed the "I Think" part. Are you sure you did not edit that comment later?(Just asking, no offense). Sorry. No need to be rude for that. An "ad hominem" is really not necessary here. – Natasha Dutta Jun 10 '15 at 14:56
  • Comment is not edited later. – i486 Jun 10 '15 at 20:18
  • "I understand in VS all variables must be declared at the top of a block". That's simply false. Starting from Visual Studio 2013, VS supports C99-style variable declarations, meaning that variable declarations do not have to be at the top of the block. – AnT stands with Russia Jan 19 '16 at 18:40

2 Answers2

1

You cannot. VLA is supported in C99 and later standards. (Support is mandatory in C99; it is optional in C11.) C89 does not have the VLA concept or support for it.

You can choose dynamic memory allocation, instead. have a look at malloc() and family for your reference.

Remember, if you want to use dynamic memory (allocation), you have to free() the allocated memory once you're done using it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Natasha Dutta
  • 3,242
  • 21
  • 23
0

MSVC doesn't support VLAs. Recent versions of MSVC do support declarations mixed with statements in C compiles (I think this started with VS 2013).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760