Are variable length arrays there in c++?
Asked Answered
C

2

9

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;
    }
}
Committee answered 25/2, 2014 at 11:55 Comment(5)
Variable length arrays will be in C++14Cythera
They are specified by C99 and GCC >= 4.7 (and Clang too AFAIK) offer it as an extension to C++ too.Spiculate
VLAs did not make it into C++14Hanger
why isn't there any one mentioning vector?!Vibes
@MarsonMao variable length arrays, if implemented would allocate memory in stack while vector allocates memory in heap.Committee
S
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.

Supersaturate answered 25/2, 2014 at 12:0 Comment(5)
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{..};).Mcginn
@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.Supersaturate
My understanding was that the status of dynarray was in limbo.Mcginn
@Matthieu I'm not talking about dynarraySupersaturate
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.Pinch
G
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.

Grecize answered 5/4, 2018 at 5:22 Comment(5)
"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++?Geosphere
C++20 won't be published for a few years yet. Perhaps you mean there are tentative drafts proposed before the committee?Catch
@StephenM.Webb Yes, maybe I used the wrong word, I'll edit it, thank you.Grecize
@Geosphere So, what's your idea about why C11 make it an optional feature and why C++ don't support it? Thanks for your comment.Grecize
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.Geosphere

© 2022 - 2024 — McMap. All rights reserved.