I am not sure why I am receiving the error message:
error C2466: cannot allocate an array of constant size 0
When I run the code:
C++ masters, please help me.Code:int s; cout<<"Enter the size:\n"; cin>>s; int array[s];
I am not sure why I am receiving the error message:
error C2466: cannot allocate an array of constant size 0
When I run the code:
C++ masters, please help me.Code:int s; cout<<"Enter the size:\n"; cin>>s; int array[s];
The current C++ standard requires array sizes to be compile time constants. I recommend you think about using std::vector instead of the array, but if you insist on arrays then you'll need to dynamically allocate the memory using new/delete.
Jim
Well, AFAIK, this should be in C99 standard/C++98, so this is supposed to work.
[edit]
I think its a gcc extension when it comes to C++ code(Its not there in the C++ standard).
[edit]
Last edited by Aslaville; 10-13-2014 at 02:39 AM.
The gcc compiler has "hacks" that allow VLA, and some other "features", but the current C++ standard C++11 specifically disallows this "feature". To force gcc to avoid the "hack" you must use a compiler switch to turn off the "hack" (and most other compiler specific "features") and abide by the standard (-Wpedantic). See this [URL="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options"]link[/URL and note by default gcc doesn't issue warnings for anything other than "required" warnings, but by using various options can issue many more types of warnings.
Jim