mercredi 29 mars 2017

why can g++ not have variable length array initialization

I am using C++ 11 and this is an example code fragment where I am not able to understand the internal workings of the language.

This works -

int main(){
int n;
cin>>n;
int a[n]; // THIS WORKS due the the extensions

However, this does not work -

int main(){
int n;
cin>>n;
int a[n] = {0} // This does not work;

I am compiling this using:

g++ -std=c++0x file.cpp -o file

  1. I understand that VLA's are not part of the C++ standard and the GNU C++ provides it via compiler extensions.
  2. I also understand that the memory is allocated at runtime using alloca.
  3. What I don't understand is that, if A works, why can B not work?

Aucun commentaire:

Enregistrer un commentaire