Well, I came across this, and I don't know what to think, I know that to allocate memory the operator is necessary new
, however I don't understand why this code works?, and in any case, what are the possible failures? (maybe it's a new c++11 feature that I'm not aware of?)
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";
Thank you
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 their own to the compiler you are currently using.
Trying to compile it with another compiler might throw an error if that compiler doesn't support that feature.