home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / linux / 10066 < prev    next >
Encoding:
Internet Message Format  |  1992-09-13  |  2.8 KB

  1. Xref: sparky comp.os.linux:10066 comp.lang.c:13296
  2. Newsgroups: comp.os.linux,comp.lang.c
  3. Path: sparky!uunet!usc!sol.ctr.columbia.edu!grunt!matthew
  4. From: matthew@psych.psy.uq.oz.au (Matthew McDonald)
  5. Subject: Re: GCC 2.2.2d bug
  6. Organization: Psychology Department, University of Queensland
  7. Followup-To: comp.lang.c
  8. References: <1992Sep4.202913.14346@hippo.ru.ac.za> <1992Sep4.230902.2523@novell.com> <1992Sep6.162717.24242@pool.info.sunyit.edu>
  9. Message-ID: <1992Sep7.114713.5075@ctr.columbia.edu>
  10. Sender: news@ctr.columbia.edu (The Daily Lose)
  11. Date: Mon, 7 Sep 1992 11:47:13 GMT
  12. X-Posted-From: chrysis.psy.uq.oz.au
  13. X-Posted-Through: sol.ctr.columbia.edu
  14. Lines: 58
  15.  
  16. James Henrickson writes:
  17. |>Brendan B. Boerner writes:
  18. |> >In article <1992Sep4.202913.14346@hippo.ru.ac.za> pi@cs.sun.ac.za writes:
  19. |> >>I have found that gcc is generating the wrong code for the following
  20. |> >>code fragment:
  21. |> >>
  22. |> >>  int p = 10;
  23. |> >>  p = p++;
  24. |> >>
  25. |> >>From my C background, I have found the value to be 10.  Unfortunately
  26. |> >>(for the gcc maintainers) the result is 11.
  27. |> >
  28. |> >I have this idea that if you posted this to comp.lang.c, Chris Torek or
  29. |> >someone similar would point out that this is correct behaviour.  My gut
  30. |> >feeling is that it SHOULD be 11 but I'm not a C guru.
  31. |> 
  32. |> Since the value of p isn't used for anything, it looks like the compiler
  33. |> is optimizing it for you.  Here's another test:
  34. |> 
  35. |> printf("%d\n",(p = p++));
  36. |> printf("%d\n",p);
  37. |> 
  38. |> The results should, I think, be:
  39. |> 10
  40. |> 11
  41.  
  42.     There is no correct answer, either 10 or 11 are perfectly
  43. legal results. This isn't completely intuitive.
  44.  
  45.         When you use an operator (like =, ++, -= ...)
  46. that modifies the value of a variable the C compiler is under
  47. no obligation to put the new value into the variable immediately.
  48. The variable is only guaranteed to have its *new* value after the
  49. next end-of-statement or *logical* boolean operator, (or other
  50. `sequence point'.)
  51.  
  52.         What's happening is that the two assignments to p (p++ and p=...)
  53. don't have to happen in any particular order, as long as they both get
  54. done before the next semicolon.
  55.  
  56.         "p=..." (which sets p to 10) might modify p first or "p++"
  57. (which sets it to 11) might modify p first. You can't tell which will
  58. happen first and which will happen last and overwrite the value of the first.
  59.  
  60.         That's what the language definition says and that's what the
  61. compilers have done for nearly ten years now. Both Harbison & Steele
  62. and K&R spend a fair bit of time explaining evaluation order. If this
  63. confuses you, take a look in either of those.
  64.  
  65. Executive summary:
  66.         It's not a gcc bug.
  67.         Don't modify a variable twice in the same expression.
  68.  
  69. Followups to comp.lang.c
  70.  
  71. =-=-
  72.            Matthew McDonald  matthew@psych.psy.uq.oz.au
  73.         Kill the Lawyers.       Boycott AT&T.
  74.