home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4442 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: sn.no!orjarive
  2. From: jakob.rivertz@aftenposten.no (Jakob Rivertz)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: x ^= y ^= x ^= y;
  5. Date: Thu, 29 Feb 96 06:52:35 GMT
  6. Organization: Aftenposten A/S
  7. Message-ID: <4h3ivp$966@hasle.sn.no>
  8. References: <1286.6624T1439T237@cs.ruu.nl> <3Du8y*20g@yaps.rhein.de> <3132C4BE.2D60@cs.ruu.nl>
  9. NNTP-Posting-Host: gatekeeper.aftenposten.no
  10. X-Newsreader: News Xpress 2.0 Beta #0
  11.  
  12. In article <3132C4BE.2D60@cs.ruu.nl>, Wessel Dankers <wsldanke@cs.ruu.nl> 
  13. wrote:
  14. >Arno Eigenwillig wrote:
  15. >> In article <1286.6624T1439T237@cs.ruu.nl>, Wessel Dankers writes:
  16. >> > Which can of course be rewritten as:
  17. >> >  x ^= y;
  18. >> >  y ^= x;
  19. >> >  x ^= y;
  20. >> Yes.
  21. >> > or if you want it real fancy:
  22. >> >  x ^= y ^= x ^= y;
  23. >> No. Reread your C text book and find out about sequence points, side
  24. >> effects, and which combinations of them are not allowed.
  25. >If you would have taken a quick peek yourself you would have seen that an
  26. >assignment has a return-value of itself. Anyway, I tested it and it worked.
  27.  
  28. Leave this guy alone, everyone. It's is a troll.
  29.  
  30. For those of you who have been misleaden by these posts, do get the
  31. comp.lang.c-faq, read questions 4.1 through 4.6, and 6.1 (which discusses
  32. this very example). You are not allowed to modify the same value twice
  33. between sequence points. Sequence points are implied by `&&', `||', `,',
  34. `? :' and `;'. For example:
  35.  
  36. <never use these>
  37.         a[i] = i++;
  38.         j = i++ * i++;
  39.         x ^= y ^= x ^= y;
  40.         i = ++i;
  41. </never use these>
  42.  
  43. all invoke undefined behaviour. The compiler can do _anything_, when these
  44. constructs are used. Yes, it may even generate the /correct/ code, but it
  45. may also `format your harddrive' (see recent discussions on comp.lang.c).
  46. Even if it /works/ on your compiler, there is no guarantee that it works
  47. on other compilers, or even on future versions of your compiler.
  48.  
  49. PS: You may use the eor-trick with the blitter, as you then _specify_ the
  50. order of evaluation. It works, but is slower than the copy-temp scheme.
  51. The eor-trick uses (2reads + 1write) per element, while the copy-temp
  52. scheme uses (1read + 1write) per element. Also, "x^=y; y^=x; x^=y;" works
  53. (the semi-colons act as sequence points).
  54.  
  55. -- 
  56. Jakob <jakob.rivertz@aftenposten.no>
  57.