0

So simple code like:

int n;
cin >> n;
int s[n], p[2*(n-1)][3];

I have to translate to:

int  n;
cin >> n;
vector<int> s(n, 0);
vector<vector<int>> p(2 * (n - 1), vector<int>(3));

I would like to see something like:

int  n;
cin >> n;
mat s(n), p(2*(n-1), 3);

I definitely do not want to use new\make_unique and std::array+std::vector mix for such simple stuff. Two lines are an ugly mess IMHO so I seeek for a way to keep C like sintax.

So what is a workaround? Any define/standard header/copy-pastable STL based C++ type?

user2736738
  • 30,591
  • 5
  • 42
  • 56
DuckQueen
  • 772
  • 10
  • 62
  • 134
  • 2
    VLAs are not supported by C++ standard (but, are supported by extensions to some compilers, e.g.: g++). – Algirdas Preidžius Jan 26 '18 at 10:14
  • 6
    Answer: There's no VLA in C++. – iBug Jan 26 '18 at 10:14
  • 4
    what is the problem with the second snippet? To me it is completely unclear what is the question – 463035818_is_not_an_ai Jan 26 '18 at 10:14
  • See https://stackoverflow.com/questions/5246900/enabling-vlasvariable-length-arrays-in-ms-visual-c – sbabbi Jan 26 '18 at 10:15
  • I would personally make `p` a vector of arrays, like `std::vector> p(2 * (n - 1));`. – Some programmer dude Jan 26 '18 at 10:15
  • Regarding your updated title: "_Is there a VLAs (variable length arrays) support workaround for VS2017?_" If VLAs are not supported by C++ standard, why should any, one, compiler support it? If some provide extensions for such functionality, it doesn't matter that they (or any other compiler) have to. – Algirdas Preidžius Jan 26 '18 at 10:19
  • Allocating huge arrays on the stack is not a good idea. – user7860670 Jan 26 '18 at 10:25
  • 2
    IMO the only workaround is the second piece of code in your question. What's wrong with it? If you think it's _"an ugly mess"_ then maybe C++ is not for you. – Jabberwocky Jan 26 '18 at 10:26
  • @Michael Walz: Say you want to teach C++ to a kid. A kid can read simple code, kids tipically are a bit more confused with constructs like `vector> p(2 * (n - 1), vector(3));` instead of `int p[2*(n-1)][3];` because long code boilerplate shadows/hides real logic. Even bigger problem arrives when they can use ideone at home and VS2017 for school projects. – DuckQueen Jan 26 '18 at 10:35
  • Use a matrix class like [this in Dr. Dobb's](http://www.drdobbs.com/a-c-matrix-template-class/184403323) or write your own. –  Jan 26 '18 at 10:37
  • OK, the question has actually totally changed with all your edits, mabe you should post an all new question about how to write `vector> p(2 * (n - 1), vector(3));` like `mat p(2*(n-1), 3);` – Jabberwocky Jan 26 '18 at 10:39
  • @Michael: 1) isn't clarification what are edits for?) 2) it has not changed for me as inclusion of long external matrix class is a less preferable option to add some define for C support/stl include. There is some C support in VS2017, so why it is so hard for them to add this quite old feature is a mistary for me – DuckQueen Jan 26 '18 at 10:52
  • @DuckQueen yes of course, but sometimes too many edits totally transform the question and you might get better exposure if you ask a new question about the _actual_ problem only. Not my downvote BTW. – Jabberwocky Jan 26 '18 at 11:00
  • In general it's not a good idea to keep C syntax in C++. Instead use C++ syntax. – Xam Jan 26 '18 at 20:01
  • @Xam: only when C++ syntax is in any way superior, here it just sucks=) – DuckQueen Jan 27 '18 at 15:32

4 Answers4

7

Variable length arrays are not supported by standard C++.

std::vector<int> is the idiomatic way of implementing a contiguous block of int data whose size is not known at compile time. A good rule of thumb is to use std::vector until you find a compelling reason not to.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
5

Variable length arrays (i.e. arrays where at least one dimension is not a compile time constant) are not supported in standard C++. Hence, you cannot write something like cin >> n; int s[n]. Some extensions exist, but still - for very large values of n, you might get troubles if the compiler at hand puts such an array on the "stack", which is usually more limited than the heap.

The standard way is to use std::vector<int> s(n) or, if - though usually not recommended - for some reason you want a "plain" array not wrapped by an object, you could write int *s = new int[n];, although it is then incumbent on you calling delete[] s; when you no longer need the array.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
2

alloca is another alternative. Not standard but widely supported.

john
  • 85,011
  • 4
  • 57
  • 81
1

By standard C++, there's no Variable Length Arrays (VLAs). Either use an STL like std::vector, or define the variables as const (that you can't modify at runtime).

iBug
  • 35,554
  • 7
  • 89
  • 134