A C++ implementation that detects undefined behavior?

后端 未结 10 1384
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 14:13

A huge number of operations in C++ result in undefined behavior, where the spec is completely mute about what the program\'s behavior ought to be and allows for anything to

10条回答
  •  囚心锁ツ
    2020-11-27 14:55

    Just as a side observation, according to the theory of computability, you cannot have a program that detects all possible undefined behaviours.

    You can only have tools that use heuristics and detect some particular cases that follow certain patterns. Or you can in certain cases prove that a program behaves as you want. But you cannot detect undefined behaviour in general.

    Edit

    If a program does not terminate (hangs, loops forever) on a given input, then its output is undefined.

    If you agree on this definition, then determining whether a program terminates is the well-known "Halting Problem", which has been proven to be undecidable, i.e. there exists no program (Turing Machine, C program, C++ program, Pascal program, in whatever language) that can solve this problem in general.

    Simply put: there exists no program P that can take as input any program Q and input data I and print as output TRUE if Q(I) terminates, or else print FALSE if Q(I) does not terminate.

    For more information you can look at http://en.wikipedia.org/wiki/Halting_problem.

提交回复
热议问题