Does ANSI insist this is legal?

Walter Murray walter at hpclwjm.HP.COM
Fri Mar 2 04:39:37 AEST 1990


Ronald Guilmette writes:

> I agree that any attempt to use a space which is not long enough (to hold
> the pointer value initializer) can (allowably, under the standard) cause you
> to lose all your files, can cause your boss to receive rude mail, and can
> cause a core dump, but the $64,000 question is "When may these catastrophies
> occur?  At run-time or at compile-time?  Either?  Both?"

> If the standard is trying to say that "the *run-time* behavior is undefined"
> then that is one thing.  If it is trying to say that the compiler may (or can)
> flag an error at compile time, that is an entirely different thing.

I think this is an excellent question.  Simplifying the example:

   main (void)
   {
      char c; int i;
      c = (char)&j;
   }

Assuming a char can't hold a pointer, the behavior of this code is undefined.
May a conforming implementation produce an error at compile time?  I would
tend to say No.  Suppose the assignment were instead:

   c = 1 || (char)&j;

Presumably this is intended to be legal.  (See the last footnote in
Section 3.4.)  So there appear to be expressions that are legal,
like division by zero, AS LONG AS THE PROGRAM CONTROL FLOW IS SUCH
YOU DON'T TRY TO EVALUATE THEM.

Yet, according to the definition in 1.6, "undefined behavior" may
involve "terminating a translation".  An example might be using the
same identifier more than once as a label name in the same function.

The bottom line seems to be that it may not always be clear when
"undefined behavior" may include failing to translate.

Walter Murray
----------



More information about the Comp.std.c mailing list