8

I had always thought that variable length arrays were not allowed in c++(Refer :Why aren't variable-length arrays part of the C++ standard?) .But than why does this code compile and work?

#include <iostream>
using namespace std;

int main () {

    int n;
    cin >> n;

    int a[n];

    for (int i=0; i<n; i++) {
        a[i] = i;
    }

    for (int i=0; i<n; i++) {
        cout << a[i] << endl;
    }
}
Community
  • 1
  • 1
nikhil_vyas
  • 513
  • 5
  • 16

2 Answers2

9

The current C++ standard does not require that compilers support VLAs. However, compiler vendors are permitted to support VLAs as an extension. GCC >= 4.7, for example, does.

It was originally proposed that VLAs would appear in C++14, however the proposal did not succeed. They also, ultimately, did not appear in C++17.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Will they really be supported ? They have been widely discussed and a number of issues were raised that did not seem trivial to solve (the most infamous being `array.~dynarray(); new (&array) dynarray{..};`). – Matthieu M. Feb 25 '14 at 12:18
  • 2
    @MatthieuM. Runtime-sized arrays are part of C++14. They are not exactly the same as C VLAs. The code in the question will work the same in C99 as C++14. Well, modulo the use of `cout` and so on. The array part of it is what I focus on. – David Heffernan Feb 25 '14 at 12:22
  • My understanding was that the status of `dynarray` was in limbo. – Matthieu M. Feb 25 '14 at 13:04
  • @Matthieu I'm not talking about dynarray – David Heffernan Feb 25 '14 at 13:08
  • Yes, the proposal for VLAs in C++ is a joke, and a bad one at that: It pretty much allows for 1D VLAs on the stack only. It does not provide any support for pointers to arrays of variable lengths as C99 does, and thus does not allow allocating a 2D VLA and passing it down to a function to work on the data. – cmaster - reinstate monica Apr 05 '18 at 13:16
2

C99 permits VLA, but C++ never permits that, because the performance of VLA is so unfriendly. And in C11, VLA becomes an optional feature.

Before, it's said that VLA would appear at C++17. But now C++17 is published, and no VLA, either. (And it seems C++20 won't have VLA. The recent documents haven't talk about it at all.)

Although the standard doesn't support it, GNU compiler supports it as an extension.

con ko
  • 1,368
  • 5
  • 18
  • 2
    _"C++ never permits that, because the performance of VLA is so unfriendly."_ So, you'll have a link to the official discussions where performance was cited as a reason against including VLAs in C++? – underscore_d Apr 05 '18 at 12:57
  • C++20 won't be published for a few years yet. Perhaps you mean there are tentative drafts proposed before the committee? – Stephen M. Webb Apr 05 '18 at 13:08
  • @StephenM.Webb Yes, maybe I used the wrong word, I'll edit it, thank you. – con ko Apr 05 '18 at 13:10
  • @underscore_d So, what's your idea about why C11 make it an optional feature and why C++ don't support it? Thanks for your comment. – con ko Apr 05 '18 at 13:15
  • 2
    Don't try to pawn this off on me! You're the one who wrote the answer, so you should cite *your* sources. I mean, you even mention "recent documents" in your answer, yet you somehow can't link to any of them. – underscore_d Apr 05 '18 at 13:17