c++ - Why I can use array that initiated with 0 size: Char ch[0]; -
this code:
#include<iostream> using namespace std; int main(){ char ch[0]; cin >> ch; cout << ch; return 0; }
input1:
abcdefghijklmnopqrstuvwxyza
output1:
abcdefghijklmnopqrstuvwxyza
(working fine, don't know why)
input2:
abcdefghijklmnopqrstuvwxyzab
output2:
abcdefghijklmnopqrstuvwxyzab_
(request input)
input3:
abcdefghijklmnopqrstuvwxyzabc
output3: (runtime error)
when output2 request input, , put input2, output same output2 (with request input again), , output1 or output2 appear when put input1 or input2 in there
can explain phenomenon? why happens?
an array of size 0 not valid:
if constant-expression (5.19) present, shall integral constant expression , value shall greater zero.
if compiler accepts it, merely non-standard extension. gcc accepts issue diagnostic if add -pedantic
option:
warning: iso c++ forbids zero-size array ‘arr’ [-pedantic]
nonetheless, reading non-standard array of 0 size undoubtedly give undefined behaviour.
Comments
Post a Comment