Are variable length arrays there in c++?

一个人想着一个人 提交于 2019-11-26 08:54:21

问题


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;
    }
}

回答1:


The current C++ standard does not require that compilers VLAs. However, compiler vendors are permitted to support VLAs as an extension.

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




回答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.



来源:https://stackoverflow.com/questions/22013444/are-variable-length-arrays-there-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!