c++ - Does "Undefined Behavior" really permit *anything* to happen? -


this question has answer here:

edit: question not intended forum discussion (de)merits of undefined behavior, that's sort of became. in case, this thread hypothetical c-compiler no undefined behavior may of additional interest think important topic.


the classic apocryphal example of "undefined behavior" is, of course, "nasal demons" — physical impossibility, regardless of c , c++ standards permit.

because c , c++ communities tend put such emphasis on unpredictability of undefined behavior , idea compiler allowed cause program literally anything when undefined behavior encountered, had assumed standard puts no restrictions whatsoever on behavior of, well, undefined behavior.

but relevant quote in c++ standard seems be:

[c++14: defns.undefined]: [..] permissible undefined behavior ranges ignoring situation unpredictable results, behaving during translation or program execution in documented manner characteristic of environment (with or without issuance of diagnostic message), terminating translation or execution (with issuance of diagnostic message). [..]

this specifies small set of possible options:

  • ignoring situation -- yes, standard goes on have "unpredictable results", that's not same compiler inserting code (which assume prerequisite for, know, nasal demons).
  • behaving in documented manner characteristic of environment -- sounds relatively benign. (i haven't heard of documented cases of nasal demons.)
  • terminating translation or execution -- diagnostic, no less. ub behave nicely.

i assume in cases, compilers choose ignore undefined behavior; example, when reading uninitialized memory, presumably anti-optimization insert code ensure consistent behavior. suppose stranger types of undefined behavior (such "time travel") fall under second category--but requires such behaviors documented , "characteristic of environment" (so guess nasal demons produced infernal computers?).

am misunderstanding definition? these intended mere examples of constitute undefined behavior, rather comprehensive list of options? claim "anything can happen" meant merely unexpected side-effect of ignoring situation?

edit: 2 minor points of clarification:

  • i thought clear original question, , think people was, i'll spell out anyway: realize "nasal demons" tongue-in-cheek.
  • please not write an(other) answer explaining ub allows platform-specific compiler optimizations, unless also explain how allows optimizations implementation-defined behavior wouldn't allow.

yes, permits happen. note giving examples. definition pretty clear:

undefined behavior: behavior international standard imposes no requirements.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -