Why do I need a dynamic array?

I'm trying to understand the need for dynamic arrays. My understanding is that this shouldn't work, but it does. Can anyone explain this to me?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>

using namespace std;

int main ()
{

int arrsize;

cout << "Enter array size: ";
cin >> arrsize;

int a [arrsize];

for (int i = 0; i < arrsize; i++){
    cout << "Enter array values: ";
    cin >> a [i];}
 
for (int i = 0; i < arrsize; i++)
    cout << a [i];

return 0;

}
Assuming you're referring to the fact that you're declaring a runtime-sized array like a fixed-size array, it was nonstandard. Compilers supported this as extensions to the language.
IIRC it was standardized in C++14
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3497.html
Last edited on
Actually this "feature" didn't make it into the standard. VLA are still not supported by the current standards, and hopefully never will be. If you want this "feature" I recommend using std::vector.

Now after saying the above, several compilers support this "feature" as a compiler specific option, which is why the program is compiling.

Topic archived. No new replies allowed.