0

Possible Duplicate:
Undefined, unspecified and implementation-defined behavior

I'm trying to deepen my understanding of undefined behavior in C++. Suppose a C++ compiler will intentionally detect some cases of undefined behavior - for example, modifying the variable twice between two sequence points:

x++ = 2;

Once that imaginary compiler reliably detects such a situation it will say emit ten totally random machine instructions into the produced machine code.

According to C++ standard, wherever something is classified as UB there're no requirements on what happens. Will the described imaginary compiler be conformant to the C++ standard?

12
  • 6
    It wouldn't be a very popular compiler. Commented Nov 24, 2010 at 9:25
  • The accepted answer to the very question you link to says "Basically, the standard allows anything to happen once you invoke undefined behavior (even nasal demons)." Now tell me why we should not close yours as a dupe? Commented Nov 24, 2010 at 9:29
  • @sbi: Maybe you're right, yet nasal demons are not that easy to implement in the compiler and I prefer to focus on real (even if they are unlikely) things. Commented Nov 24, 2010 at 9:39
  • 1
    @sharptooth: Your question reads: "Is the compiler allowed to...", the answer reads "Basically, the standard allows anything to happen". That's a clear dupe to me. I now see that there's something else asked, too, at the end of the question's fine print. If that's what you actually meant to ask, you might want to change your question's title, before this gets closed. Commented Nov 24, 2010 at 9:57
  • @sbi: What exactly do you attribute as fine print in this question? I tried to make it as clear as possible. If there's a way to improve it I'd like to know. Commented Nov 24, 2010 at 10:02

4 Answers 4

9

Yes. The standard imposes no requirements, so it can do whatever it wants:

undefined behavior

behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements.

Just as a note, that is undefined behavior, but it's not necessarily a good example. On g++ 4.4.1, it will refuse to compile with:

error: lvalue required as left operand of assignment

because the result of a post-increment is not an lvalue.

Sign up to request clarification or add additional context in comments.

Comments

3

Essentially, yes, if and only if those 10 instructions are equally reachable. Consider the following code:

int main () {
  if (false) {
    int x = 0; x++ = 2;
  }
  std::cout << "Hello, world" << std::endl;
}

The UB may be detected at compile time, and the code generation for that particular branch may result in meaningless code. However, the not-so-conditional jump must skip all this and go straight past the closing }

(This question is not a duplicate because the compile-time detection of run-time UB was not covered earlier)

2 Comments

That's interesting. Why is the compiler not allowed to randomly trash the program once it encounters a single instance of UB?
Undefined behavior is a run time property with potentially non causal effect. It has to be in the execution path for it to have an effect (there is a wide class of UB which is data dependent, for instance a[i] = a[j]++).
2

A particular compiler (unless buggy) will always have the same behaviour (definitely no random code) on encountering such constructs (unless the context of the code is different).

In practice, "Undefined behaviour" means "different compilers will handle things differently".

If you want to know "will your imaginary compiler still conform to C++ standards?" - the answer I think is Yes.

4 Comments

Could not a compiler processing code for which behaviour is undefined enter that code into its AST, pass it through various processing and optimisations, and depending on surrounding code, optimisation levels, compiler flags etc. produce machine code that behaves differently, or affect surrounding otherwise well-defined code such that if behaves differently? I'm not saying it's especially likely, but do you have a basis for drawing such a definite conclusion to the contrary...?
Hi... I did say "unless the context of the code is different". The asker said - "emit ten totally random machine instructions into the produced machine code" which I think means "random code even when context is same".
Unfortunately a philosophy seems to have taken hold that compilers are entitled to assume that if receipt of certain input would inevitably cause Undefined Behavior, compilers should assume that such input could not possibly occur, and therefore eliminate code which would have no purpose but to handle such input. As a consequence, Undefined Behavior is not also unconstrained by the laws of time, but also by the laws of causality (e.g. given if (x != 0) foo(); y=1/x; a compiler may omit the conditional check before calling foo, since it would be under no obligation...
...to refrain from calling foo when x is zero.
0

From the Draft Standard / http://www.kuzbass.ru:8086/docs/isocpp/intro.html / [1.3.12]

[Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. ]

Definitely doesn't list inserting random instructions, and it's hard to argue that the "ranges from" aspect would include a decision to insert random instructions as being anywhere on the continuum between the listed behaviours. ;-)

2 Comments

Hmm... There's "in a documented manner" - I guess that could allow random instructions. But then I can't see how it is different from implementation-defined behavior.
Anyway I asked a question on that - stackoverflow.com/questions/4274763/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.