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

  1. Xref: sparky comp.lang.scheme:2871 comp.lang.c:19281
  2. Newsgroups: comp.lang.scheme,comp.lang.c
  3. Path: sparky!uunet!microsoft!wingnut!russpj
  4. From: russpj@microsoft.com (Russ Paul-Jones)
  5. Subject: Re: applying or
  6. Message-ID: <1993Jan06.002044.795@microsoft.com>
  7. Date: 06 Jan 93 00:20:44 GMT
  8. Organization: Microsoft Corporation
  9. References: <1993Jan1.184655.7023@viewlogic.com>
  10. Lines: 67
  11.  
  12. In article <1993Jan1.184655.7023@viewlogic.com> josh@viewlogic.com (Josh Marantz) writes:
  13. >
  14. > [about short circuit evaluation of && and || (C syntax)]
  15. >
  16. >I was sort of glad to move to C/Scheme, where this short-circuiting
  17. >was guaranteed, although I never thought about the compiler
  18. >optimizations that the short-circuit guarantee prevents!  Would it be
  19. >legal for a C or Scheme compiler to avoid the short-circuiting if it
  20. >could prove there were no side effects involved, and if it looked like
  21. >a fruitful optimization?
  22.  
  23. I can't speak for the Scheme language, but side effects are not the 
  24. only problem in C; short circuit evaluation is often used to 
  25. prevent "bad" runtime behavior.
  26.  
  27. In the subprogram:
  28.  
  29. if (a) && (b)
  30.     c;
  31.  
  32. where a, b, and c stand for legal C expressions, the programmer 
  33. may rely on the fact that b should only be evaluated if a is 
  34. non-zero.  If a == 0, then b is allowed to be a "bad" 
  35. expression (e.g. division by zero, dereference of an invalid pointer
  36. or out-of-bounds array arithmetic, a call to a function with invalid 
  37. parameters, arithmetic overflow) without affecting the behavior of 
  38. the rest of the program.
  39.  
  40. Even if b is just a simple variable, if it is not initialized there 
  41. may be problems (I can't find if Standard C categorizes access 
  42. of an uninitizalized object as "harmless" or not).
  43.  
  44. So, there are some instances where this optimization might be made:
  45.  
  46. b = 4;
  47. if (a*7)/3 > 100/a || b
  48.     fred();
  49.  
  50. I would say that fred() may be called without evaluating the silly 
  51. expression, even if a == 0.  But I would suspect that it would take 
  52. a fairly clever compiler to safely find many of these instances.
  53.  
  54. On another note, it is good C programming practice to do a little 
  55. optimization as a programmer in taking advantage of short-circuit 
  56. evaluation.  For example, if the expressions a and b are really 
  57. independent, without side effects, and of roughly equal cost to 
  58. evaluate, but b == 0 is more likely than a == 0, I would write:
  59.  
  60. if (b) && (a)
  61.     c;
  62.  
  63. I would probably not be too excited about a compiler that evaluated 
  64. a first to find it non-zero most of the time.
  65.  
  66. If this has no relevance to Scheme, I'd suggest limiting followups 
  67. to comp.lang.c.
  68.  
  69. >-- 
  70. >Joshua Marantz                          You make my life and times
  71. >Viewlogic Systems,                        a book of bluesy Saturdays
  72. >josh@viewlogic.com                      And I have to choose...
  73.  
  74. -Russ Paul-Jones
  75. russpj@microsoft.com
  76. I have no connection with the compiler folk here, and I have no idea 
  77. what our compiler does with these operators.  And, I'm not in any 
  78. way speaking for my employer.
  79.