Why doesn't the compiler detect out-of-bounds in string constant initialization?

后端 未结 6 1980
误落风尘
误落风尘 2021-01-18 00:13

I read this question and its answer in a book. But I didn\'t understand the book\'s justification.

Will the following code compile?

6条回答
  •  忘掉有多难
    2021-01-18 00:59

    Array-bound checking happens at runtime, not compile time. The compiler has no way of doing the static analysis of the above code that would be necessary to prevent the error.

    UPDATE: Apparently the above statement is true for some compilers and not others. If your book says it will compile, it must be referring to a compiler that doesn't do the checking.

提交回复
热议问题