4 ms·
Is that C89 or C99? I only see that in the latter, though the C89 spec I see may have footnotes removed. From C99: > A conforming implementation shall produce
by mnarayan01 14y ago
Is that C89 or C99? I only see that in the latter, though the C89 spec I see may have footnotes removed. From C99:
> A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined.
would seem to imply that a standard compliant compiler would need to produce a message for an invalid program. I will say it is both interesting and surprising (to me) that at least C99 does allow for the successful translation of an invalid C program.
Regardless, while informative, I'll probably stop posting on this topic before I lose the ability to ;)
- mikeash 14y agoYour quote is where the footnote comes from. I think what you're missing (and what I missed when I first read that while looking this up) is the "violation of any syntax rule or constraint" part. In other words, a diagnostic is required for syntax errors only. For other stuff, like bad pointer dereferences, division by zero, etc., it's optional.
- mansr 14y agoThat is how it has to be. Determining at compile-time whether a given program will invoke undefined behaviour at runtime is equivalent to solving the halting problem, which we know is impossible.
- mikeash 14y agoNot sure which way you mean it, but it's important to note that this is only true if you have C-like undefined behavior in your language, and is not true of computer languages in general. It's not all that hard to define the results of all operations. I believe a lot of languages (e.g. Java) do this. For example: - C makes it undefined to access beyond the end of an array. It's possible to define this as always throwing an exception or terminating the program. - C makes it undefined to overflow a signed integer type. We can define this as following two's complement rules, or terminating the program. - Writing to a constant string is undefined. We can define this to always work, to always terminate the program, or we can even define a type system that makes it impossible to even try. Once you define all behavior like this, then there is no undefined behavior. A valid program is one that your compiler accepts, no more and no less. The program can still have bugs, of course, but if the program compiles, its output is always well-defined.
- mansr 14y agoThis entire thread is specifically about C.