Why is it allowed to declare an automatic array with size depending on user input?

  • A+
Category:Languages

I'm using MinGW to compile for C++11 and I found out that this doesn't throw an error:

int S; cin>>S; char array[S]; 

While this does ("storage size of 'array' isn't known"):

char array[]; 

To me, the size is also unknown in the first case, as it depends on what the user input is.

As far as I knew, automatic arrays are allocated at compile time in stack memory. So why wouldn't the first example fail?

 


It's not. C++ doesn't have variable-length arrays, though some compilers allow it as an extension of the language.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: