home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!world!ksr!jfw
- From: jfw@ksr.com (John F. Woods)
- Newsgroups: comp.lang.c
- Subject: Re: NH-- > NH
- Message-ID: <20795@ksr.com>
- Date: 8 Jan 93 18:16:22 EST
- References: <1993Jan4.140735.11269@wisipc.weizmann.ac.il> <1993Jan7.002651.10741@oracle.us.oracle.com> <C0JMw6.680@netnews.jhuapl.edu> <1993Jan8.210335.28340@sharebase.com>
- Sender: news@ksr.com
- Lines: 39
-
- keith@torme.sharebase.com (Keith Chambless) writes:
- >In article <C0JMw6.680@netnews.jhuapl.edu> bandy@netnews.jhuapl.edu (Mike Bandy) writes:
- >>So what you're saying is that NH > NH. I don't believe it. Your
- >>compiler (or your logic) has some serious problems.
-
- No, the code has an obvious bug. In ANSI Standard C (the only C in which you
- can really talk about guarantees of behavior), it is explicitly undefined
- behavior to modify an object in a statement (more precisely, between two
- "sequence points") and evaluate it separately; to use a concrete example,
-
- if (nh-- > nh)
-
- can perfectly well be compiled as
-
- MOV nh,r0
- SUB $1,nh
- MOV nh,r1
- CMP r0,r1
-
- or
-
- MOV nh,r0
- MOV nh,r1
- SUB $1,nh
- CMP r0,r1
-
- i.e. the second evaluation of nh is permitted to be either before or after
- the side effect takes place; in fact, as far as ANSI C is concerned, the
- side effect *need not* take place, and there need be *no* evaluation of
- nh -- "undefined behavior" means completely undefined and unrestrained:
- if you violate the warranty, _anything_ is allowed to happen. The compiled
- program could print "BOZO!" to the standard error stream, the CPU could melt,
- a hand could reach out of the monitor and slap the programmer silly (would
- that it could).
-
- As far as pre-ANSI C goes, there was no formal description of the semantics
- of the language (and K&R I glossed over the timing of side effects because
- depending on the timing is tasteless :-), so it's kind of pointless to argue
- about whether the above behavior is "right" for pre-ANSI C.
-