home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19451 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  1.9 KB

  1. Path: sparky!uunet!world!ksr!jfw
  2. From: jfw@ksr.com (John F. Woods)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: NH-- > NH
  5. Message-ID: <20795@ksr.com>
  6. Date: 8 Jan 93 18:16:22 EST
  7. 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>
  8. Sender: news@ksr.com
  9. Lines: 39
  10.  
  11. keith@torme.sharebase.com (Keith Chambless) writes:
  12. >In article <C0JMw6.680@netnews.jhuapl.edu> bandy@netnews.jhuapl.edu (Mike Bandy) writes:
  13. >>So what you're saying is that NH > NH.  I don't believe it.  Your 
  14. >>compiler (or your logic) has some serious problems.
  15.  
  16. No, the code has an obvious bug.  In ANSI Standard C (the only C in which you
  17. can really talk about guarantees of behavior), it is explicitly undefined
  18. behavior to modify an object in a statement (more precisely, between two
  19. "sequence points") and evaluate it separately; to use a concrete example,
  20.  
  21.     if (nh-- > nh)
  22.  
  23. can perfectly well be compiled as
  24.  
  25.     MOV    nh,r0
  26.     SUB    $1,nh
  27.     MOV    nh,r1
  28.     CMP    r0,r1
  29.  
  30. or
  31.  
  32.     MOV    nh,r0
  33.     MOV    nh,r1
  34.     SUB    $1,nh
  35.     CMP    r0,r1
  36.  
  37. i.e. the second evaluation of nh is permitted to be either before or after
  38. the side effect takes place; in fact, as far as ANSI C is concerned, the
  39. side effect *need not* take place, and there need be *no* evaluation of
  40. nh -- "undefined behavior" means completely undefined and unrestrained:
  41. if you violate the warranty, _anything_ is allowed to happen.  The compiled
  42. program could print "BOZO!" to the standard error stream, the CPU could melt,
  43. a hand could reach out of the monitor and slap the programmer silly (would
  44. that it could).
  45.  
  46. As far as pre-ANSI C goes, there was no formal description of the semantics
  47. of the language (and K&R I glossed over the timing of side effects because
  48. depending on the timing is tasteless :-), so it's kind of pointless to argue
  49. about whether the above behavior is "right" for pre-ANSI C.
  50.