Why does the Line#1 not generate any compilation error?
Ideally, it should!
It is not legal code to create an array of size 0
on local storage.
Ideally, the compiler should issue you an error, probably some compiler extension allows this to compile but as per the standard this is not a valid code.Try compiling with -pedantic
.
Reference:
C++03 Standard 8.3.4/1:
If the _constant-expression+ (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.
Further,
Why does the Line#2 not generate any compilation error?
Because writing beyond the bounds of an allocated array is Undefined Behavior. An Undefined Behavior does not need the compiler to provide you any diagnostic. Note that once the code exhibits an Undefined Behavior literally anything can happen and all bets are off.