Can code that will never be executed invoke undefined behavior?

后端 未结 9 1654
予麋鹿
予麋鹿 2020-11-27 17:52

The code that invokes undefined behavior (in this example, division by zero) will never get executed, is the program still undefined behavior?

int main(void)         


        
9条回答
  •  天命终不由人
    2020-11-27 18:22

    It depends on how the expression "undefined behavior" is defined, and whether "undefined behavior" of a statement is the same as "undefined behavior" for a program.

    This program looks like C, so a deeper analysis of what the C standard used by the compiler (as some answers did) is appropriate.

    In absence of a specified standard, the correct answer is "it depends". In some languages, compilers after the first error try to guess what the programmer might mean and still generate some code, according to the compilers guess. In other, more pure languages, once somerthing is undefined, the undefinedness propagate to the whole program.

    Other languages have a concept of "bounded errors". For some limited kinds of errors, these languages define how much damage an error can produce. In particular languages with implied garbage collection frequently make a difference whether an error invalidates the typing system or does not.

提交回复
热议问题