Why is C++ more picky than other language on making an array from a variable?

Java Int x = 5; Int[] y = new int[x]; ✓ all good C++ Int x= 5; Int y[x]; Nope Even if x is a constant going through a method C++ is picky about it Const int x = 5 Method(x); } Method ( const int x){ Int y[x]; } Again it hates that


9/10/2019 11:50:19 PM

Stuart Moss

4 Answers

New Answer


This is a pretty good question that was heavily discussed prior to the C++x0 talks. See the answers here: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard It's a controversial point to say the least given the concessions the C99 standard made to allow variable length arrays.


Author seems to compare dynamical array in java with constant array in cpp? But there are many ways to make dynamical array in c or cpp: malloc & realloc in c, instruction new in cpp or even STL vector. So, what's the problem?)


Ace thanks, it makes alot of sense now