Array size at run time without dynamic allocation is allowed?
It is valid C99, it is not valid C++. This is one of not a few differences between the two languages.
This is valid in C99.
C99 standard supports variable sized arrays on the stack. Probably your compiler has chosen to support this construct too.
Note that this is different from malloc
and new
. gcc
allocates the array on the stack, just like it does with int array[100]
by just adjusting the stack pointer. No heap allocation is done. It's pretty much like _alloca
.
It is valid only in C99. Next time you may try checking your code in a reliable compiler.
This is known as VLAs (variable length arrays). It is standard in c99, but gcc allows it in c++ code as an extension. If you want it to reject the code, try experimenting with -std=standard
, -ansi
and -pedantic
options.