Can code that will never be executed invoke undefined behavior?

后端 未结 9 1641
予麋鹿
予麋鹿 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:29

    This article discusses this question in section 2.6:

    int main(void){
          guard();
          5 / 0;
    }
    

    The authors consider that the program is defined when guard() does not terminate. They also find themselves distinguishing notions of “statically undefined” and “dynamically undefined”, e.g.:

    The intention behind the standard11 appears to be that, in general, situations are made statically undefined if it is not easy to generate code for them. Only when code can be generated, then the situation can be undefined dynamically.

    11) Private correspondence with committee member.

    I would recommend looking at the entire article. Taken together, it paints a consistent picture.

    The fact that the authors of the article had to discuss the question with a committee member confirms that the standard is currently fuzzy on the answer to your question.

提交回复
热议问题