Is the behaviour of the compiler undefined, with Undefined Behaviour?

后端 未结 7 760
旧时难觅i
旧时难觅i 2021-01-03 08:48

When I answered this question, I wrote:

First, it is important to note that it is not only the behaviour of the user program that is undefined, it is

相关标签:
7条回答
  • 2021-01-03 09:04

    is it only the behaviour of the translated machine code that is undefined, or is the behaviour of the compiler undefined, too?

    The ISO C and C++ describe what a C and C++ program look like. They do not describe the environment they run in. We generally use the term compiler to refer to the tool that translates C and C++ into machine code; formally, however, the term used is implementation which is definitely wider.

    Therefore, the only behavior which is undefined is the one of the program. This is also given by the definition of UB:

    undefined behavior
    behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

    0 讨论(0)
  • 2021-01-03 09:04

    My own take is that the behavior in "undefined behavior" is that of the implementation. The spec refers to a process of "translation" that we might equate with compilation, but the fact that you can compile a program to executable code is not relevant here, the result is still considered to be part of the implementation, at least in as far as behavior is concerned. Note that while the spec does define how a C program will behave, when it places requirements these are on the implementation, and the behavior of a program can also be considered a requirement (or set of requirements) on the implementation.

    In any case, undefined behaviour can certainly refer to behavior of the compiler. See the note in C11 3.4.3:

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

    "Terminating translation" clearly refers to a compilation failure whereas "terminating a ... execution" clearly refers to behavior of a running program.

    See also Appendix J.2 which lists examples of undefined behavior. Amongst the examples are:

    A nonempty source file does not end in a new-line character which is not immediately preceded by a backslash character or ends in a partial preprocessing token or comment (5.1.1.2)

    It seems ridiculous that this should cause undefined behavior at execution time rather than at translation time. There are various other similar examples. The entire set clearly shows cases where undefined behaviour can occur at both compile time and run time.

    0 讨论(0)
  • 2021-01-03 09:07

    Assuming that "undefined behaviour for a compiler" means "there are no requirements on the behaviour of the executable program produced" then the behaviour of the compiler is undefined when presented with source code containing undefined behaviour constructs.

    Compare this with the behaviour of the compiler with correct source code. All compilers adhering to the standard must produce executable code with equivalent behaviour, the one defined by the standard for the correct source code.

    0 讨论(0)
  • 2021-01-03 09:10

    If a code has undefined behaviour it means that the standards does not know how to handle such thing. Thus it can give any output. I think it doesn't have to do with compiler as it doesn't make sense. It makes sense that it has to be the implementation that works according to standards.

    So, if standards don't know how to handle such code, then how can compilers give a defined output?

    0 讨论(0)
  • 2021-01-03 09:24

    There is no compiler mentioned in the standard and implementation details are up to the vendors.

    The standard defines how code should behave (in a syntactical and semantical way) and/or be constrained in complexity terms regarding some standard library algorithms. The source code doesn't have to have a precise behavior (nor this is defined anywhere). Every compiler just has to produce code that, under the as-if rule, is correct.

    It doesn't make sense to refer to undefined behavior of the compiler

    0 讨论(0)
  • 2021-01-03 09:25

    The C++ standard defines behavior for code, it doesn't define behavior for the compiler. As such, it doesn't really make sense to refer to undefined behavior of the compiler -- it was never well-defined to begin with. The only requirement is that it produces an implementation that conforms to the standard guidelines for the code. How it does this is an implementation detail.

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