Is it possible not to declare new memory for a dynamic arrray?

3

Well, I came across this, and I do not know what to think, I know that to allocate memory the operator new is necessary, however I do not understand why this code works ?, and in any case, what are the possible failures? (Maybe it's a new C ++ 11 feature that I do not know?)

    cout << "introduzca la capacidad ";
    cin >> capacidad;
    // como es esto posible?
    double array[capacidad];

    for (int i = 0; i < capacidad; i++){
        cout << "Numero " << i+1 << ": ";
        cin >>  array[i];
    }

    for (int i = 0; i < capacidad; i++)
        cout << "Numero " << i+1 << ": " << array[i] << "\n";

Thanks

    
asked by Jacobo Córdova 22.06.2017 в 08:22
source

1 answer

2
double array[capacidad];

This is what is known as VLA (Variable Length Array). It is an array whose size is unknown at compile time. Note that its size is indicated by a variable, which is loaded from std::cin .

This is illegal in C ++. However, many compilers support it as an extension of the compiler you are using at this time.

Maybe, when trying to compile it with another compiler , an error will be generated, if this compiler does not support that feature.

    
answered by 22.06.2017 / 08:28
source