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

前端 未结 9 1066
感情败类
感情败类 2021-01-27 05:57

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-27 06:38

    No, that's partly the reason undefined/implementation-defined behaviors exist in the standard. Undefined behavior isn't guaranteed to be the same between multiple compiles of the same source code on the same computer (say, with different optimization flags).

    The committee clearly prefers well defined behavior. Implementation-defined behavior exists when the committee believes that multiple implementations exist for some concept, and there is no reason to prefer one over another in all cases. Undefined behavior exists when the committee believes it is too hard to keep any promises under reasonable implementations.

    In many cases, undefined behavior is implemented as something without a check. The behavior is then up to the operating system, if there is one and if it notices something less-than kosher took place.

    As an example, dereferencing memory that you don't own is undefined. In general the OS will kill your program if you do that. However if the stars align just right, you may manage to dereference memory that you don't own under C++'s rules (eg., you didn't get it from new, or you already deleted it) but that the OS believes you own. Sometimes you'll get a crash and sometimes you'll just corrupt memory somewhere else in your program, and sometimes you'll get away undetected (if the memory hasn't been handed back out, for instance).

    Race conditions are considered undefined, and they are notorious for being different during different runs of the program. You'll probably get different behavior each time you smash your stack if your operating system does not notice.

    Double deletes are undefined. Generally they lead to crashes, but the fact that they are undefined means you can't rely on things crashing.

    0 讨论(0)
  • 2021-01-27 06:40

    That's the purpose of specifying it as undefined...it means there is no telling what will happen, either on different or even the same platform (with repeated tests).

    0 讨论(0)
  • 2021-01-27 06:41

    If it's undefined behaviour then by it's very nature what will happen is undefined, you can't rely on it to be the same under any circumstances.

    Unspecified behaviour on the other hand is something left up to individual vendors to decide how to implement, if there are ambiguities in the language spec for example. This will be consistent between compiles and runs, but not necessarily between different vendors. So, for example, relying on unspecified behaviour when you only build using Visual Studio is fine but if you try and port the code to gcc it may fail or produce a different behaviour than you are expecting.

    0 讨论(0)
提交回复
热议问题