Are unspecified and undefined behavior required to be consistent between compiles of the same program with the same compiler in the same environment?

前端 未结 9 1486
我在风中等你
我在风中等你 2021-01-25 22:04

Let\'s pretend my program contains a specific construct the C++ Standard states to be unspecified behavior. This basically means the implementation has to do something reasonabl

9条回答
  •  终归单人心
    2021-01-25 22:42

    But can this behavior differ between compiles of the same program on the same compiler with same settings in the same environment?

    Yes.

    In other words, if I dereference a null pointer on line 78 in file X.cpp and the implementation formats the drive in such case does it mean that it will do the same after the program is recompiled?

    The results of undefined behaviour are almost always caused by code emitted by the compiler interacting with the operating system and/or hardware in ways not specified by the language designer. So if you dereference a NULL pointer, what happens is really nothing to do with the compiler but will depend on how the underlying OS/hardware deals with an invalid memory access. If the OS/hardware always deals with this in a consistent manner (for example via a trap), then you could expect UB to be consistent, but this has nothing to do with the language or the compiler.

提交回复
热议问题