10

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;
    }
}
5
  • 1
    Variable length arrays will be in C++14 Commented Feb 25, 2014 at 11:56
  • They are specified by C99 and GCC >= 4.7 (and Clang too AFAIK) offer it as an extension to C++ too. Commented Feb 25, 2014 at 11:57
  • 3
    VLAs did not make it into C++14 Commented Nov 3, 2014 at 6:55
  • why isn't there any one mentioning vector?! Commented Nov 3, 2014 at 7:40
  • 3
    @MarsonMao variable length arrays, if implemented would allocate memory in stack while vector allocates memory in heap. Commented Nov 4, 2014 at 19:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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{..};).
@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.
My understanding was that the status of dynarray was in limbo.
@Matthieu I'm not talking about dynarray
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.
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.

5 Comments

"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++?
C++20 won't be published for a few years yet. Perhaps you mean there are tentative drafts proposed before the committee?
@StephenM.Webb Yes, maybe I used the wrong word, I'll edit it, thank you.
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.