Is a C++ compiler allowed to emit random code once it encounters a construct that is classified as undefined behavior? [duplicate]

StackOverflow https://stackoverflow.com/questions/4265167

  •  27-09-2019
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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)

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.

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. ;-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top