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

  1. Path: sparky!uunet!psinntp!cmcl2!adm!news
  2. From: stone@hilbert.math.grin.edu (John David Stone)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Ok, why doesn't THIS cause an error?
  5. Message-ID: <34861@adm.brl.mil>
  6. Date: 4 Jan 93 15:03:24 GMT
  7. Sender: news@adm.brl.mil
  8. Lines: 47
  9.  
  10.  
  11.         Alan Mead writes:
  12.  
  13. >           I'm was a little amazed when I accidentally wrote the below
  14. >  bug.  I cannot imagine why you are allowed to alter the counter of a
  15. >  for..do loop.  I suppose it's too hard for the compiler to check?
  16. >
  17. >  program bug;
  18. >  var i,j,k : integer;
  19. >
  20. >    function whatever;
  21. >  {  var i,j,k:integer;}  { this was the missing line }
  22. >    begin
  23. >      i := 2;
  24. >      for k := 1 to i*x do
  25. >       ...
  26. >    end;
  27. >
  28. >  begin
  29. >    for i := 1 to TheEnd do
  30. >      whatever;
  31. >  end.
  32.  
  33.         Mead is right.  Under the Pascal standard, this program is
  34. incorrect in two different ways.  The assignment "i := 2" is incorrect,
  35. because i is the control variable of a for-loop in the block within which i
  36. is declared.  And the statement that begins "for k := 1 to i*x" is
  37. incorrect, because k is not declared in the block closest-containing that
  38. statement.  (In other words:  Under the Pascal standard, loop control
  39. variables must always be declared locally.)
  40.  
  41.         It is not very difficult for a compiler to check these constraints;
  42. Pascal compilers almost always do so.  In fact, the rules are designed in
  43. part to make such checking easy and fast.  In my opinion, the explanation
  44. for Turbo Pascal's failure to perform the checks is that they're entirely
  45. negative -- skipping them makes incorrect programs acceptable but does not
  46. change the semantics of any correctly written program.  The designers of
  47. Turbo Pascal asked themselves, "Why have the compiler spend any time at all
  48. performing checks whose only result is to irritate programmers who have
  49. made trivial errors?"  There's no good answer to this question unless you
  50. care a lot more about portability, maintainability, and standardization
  51. than Borland does.
  52.  
  53. ------  John David Stone - Lecturer in Computer Science and Philosophy  -----
  54. --------------  Manager of the Mathematics Local-Area Network  --------------
  55. --------------  Grinnell College - Grinnell, Iowa 50112 - USA  --------------
  56. --------  stone@math.grin.edu - (515) 269-3181 - stone@grin1.bitnet  --------
  57.