home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!rfg
- From: rfg@netcom.com (Ronald F. Guilmette)
- Subject: Re: Overflow in constant expression (was: Standard conformance and GCC 2.3.3)
- Message-ID: <1992Dec30.093906.24206@netcom.com>
- Organization: Netcom Online Communications Services (408-241-9760 login: guest)
- References: <1992Dec30.011211.11409@netcom.com> <1992Dec30.075041.10907@sq.sq.com>
- Date: Wed, 30 Dec 1992 09:39:06 GMT
- Lines: 58
-
- In article <1992Dec30.075041.10907@sq.sq.com> msb@sq.sq.com (Mark Brader) writes:
- +
- +> #2) GCC fails to detect the error in the following code, even when the
- +> -pedantic-errors option is used.
- +>
- +> unsigned long overflow = LONG_MAX + 3; /* ERROR */
- +
- +I presume this is being called erroneous because of the constraint:
- +
- +# Each constant expression shall evaluate to a constant that
- +# is in the range of representable values for its type.
-
- Yes.
-
- +in section 3.4 (ANSI) / 6.4 (ISO). However, 3.4/6.4 also says:
- +
- +# The semantic rules for evaluation of a constant expression
- +# are the same as for non-constant expressions.
- +
- +This presumably includes the assertion in 3.3/6.3:
- +
- +# If an "exception" occurs during the evaluation of an expression
- +# (that is, if the result is not mathematically defined or not in
- +# the range of representable values for its type), the behavior
- +# is undefined.
- +
- +Since the constraint on the constant-expression applies to the *result*
- +of evaluating it, but the undefined behavior due to overflow applies
- +*during* the evaluation, I think "undefined behavior" wins.
-
- Well, you're wrong. x3j11 has already ruled on this exact issue. In
- Technical Information Bulletin #1, request for interpretation #31
- (and its response) make it quite clear that as fas as x3j11 is concerned,
- the expression (LONG_MAX + 3), when appearing in a context requiring
- a constant expression, is a constraint violation. Interestingly enough,
- it is also x3j11's interpretation (or so I have been led to believe)
- that the expression (ULONG_MAX + 3) does *not* violate any constraint!
- (But that's an entirely separate issue.)
-
-
- // Ron ("Loose Cannon") Guilmette uucp: ...uunet!lupine!segfault!rfg
- //
- // "On the one hand I knew that programs could have a compelling
- // and deep logical beauty, on the other hand I was forced to
- // admit that most programs are presented in a way fit for
- // mechanical execution, but even if of any beauty at all,
- // totally unfit for human appreciation."
- // -- Edsger W. Dijkstra
- --
-
- // Ron ("Loose Cannon") Guilmette uucp: ...uunet!lupine!segfault!rfg
- //
- // "On the one hand I knew that programs could have a compelling
- // and deep logical beauty, on the other hand I was forced to
- // admit that most programs are presented in a way fit for
- // mechanical execution, but even if of any beauty at all,
- // totally unfit for human appreciation."
- // -- Edsger W. Dijkstra
-