The situation when the code in C ++ is syntactically valid, but its behavior is not defined in the Standard, in the Russian-language literature is often called simply indefinite behavior. In the Standard itself, there are as many as 3 terms for such situations: undefined behavior , unspecified behavior, and implementation-defined behavior . In this short note we will understand how they differ.
This term is used to describe situations where C ++ code is completely valid, but its behavior depends on the implementation (for example, the compiler or the runtime environment), and this behavior is documented . For example, the size in bytes of a pointer or type of int depends on the specific implementation or settings of the compiler, but this is described in the documentation, and you can rely on this documentation.
The term means that the behavior of valid C ++ code is not defined by the Standard and depends on the implementation; moreover, it is not documented (at least officially). Example: the order of calculating the values of the function arguments is determined by the compiler, but nowhere is there a description of how. The standard tells us: these are features of behavior that are not fixed anywhere, therefore, they cannot be relied upon. Therefore, the behavior of your code should not depend on these features.
This is the most dangerous variant of uncertainty. In the Standard, it serves to describe behavior that can lead to completely unpredictable consequences. The most vivid examples are: reversing an array or dereferencing a pointer to a freed object. Worst of all, the program does not necessarily end immediately or will give any error at all, however, you can no longer rely on its behavior.
In conclusion, let me remind you once again that all the above terms refer to syntactically valid code that will be successfully compiled (however, compilers often issue warnings for the most obvious cases of undefined behavior ). A code that is not valid in terms of the Standard is called an ill-formed program .
Source: https://habr.com/ru/post/450910/