Re: what is the output of this program?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 29 May 2008 20:12:46 -0700
Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx> writes:
user923005 wrote:
Refusing to compile code that exhibits undefined behavior seems more
like a feature than a bug to me.
In fact, I wish that all my compilers would do that. (At least in the
cases where undefined behavior can be repaired).
IMHO that's an unfortunate wish. Here, for example,
is the start of a program that exhibits undefined behavior:
#include <unistd.h>
...
Do you really want the compiler to reject it?
As the Rationale puts it, "the ability to write machine-
specific code is one of the strengths of C." If C were
*unable* to poke I/O registers, *unable* to call dlsym() and
convert a void* to a function pointer, *unable* to overflow
an `int' without trapping, C would be far less useful than
it actually is.
Sure, but there's undefined behavior and then there's undefined
behavior.
There are plenty of constructs whose behavior the standard doesn't
define that can nevertheless be useful in non-portable code.
On the other hand, some constructs, such as ``C == C++'' or
``i = i++'' are, in a sense, more purely undefined, to the extent that
they should never appear in normal C code. Regardless of the
portability of the program, there's always a better way to expression
whatever the intent was.
For such constructs, I certainly have no problem with a compiler
warning about them, or even rejecting the translation unit *if* it can
prove that the code will always be executed.
The tricky (and perhaps undecidable) part is determining which
instances of UB are potentially ok and which are not.
(On the other hand, the way in which the particular compiler
terminated, with an internal assertion failure, indicates a bug in
that compiler -- though the standard doesn't say what a diagnostic has
to look like.)
[...]
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: what is the output of this program?
- From: Martin
- Re: what is the output of this program?
- From: Eric Sosman
- Re: what is the output of this program?
- References:
- what is the output of this program?
- From: Kenny O'Clock
- Re: what is the output of this program?
- From: Pietro Cerutti
- Re: what is the output of this program?
- From: Walter Roberson
- Re: what is the output of this program?
- From: Richard Heathfield
- Re: what is the output of this program?
- From: Joachim Schmitz
- Re: what is the output of this program?
- From: Richard Heathfield
- Re: what is the output of this program?
- From: Joachim Schmitz
- Re: what is the output of this program?
- From: Richard Heathfield
- Re: what is the output of this program?
- From: Three Headed Monkey
- Re: what is the output of this program?
- From: user923005
- Re: what is the output of this program?
- From: Eric Sosman
- what is the output of this program?
- Prev by Date: How to trace function calls?
- Next by Date: Re: what is the output of this program?
- Previous by thread: Re: what is the output of this program?
- Next by thread: Re: what is the output of this program?
- Index(es):
Relevant Pages
|