home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16568 < prev    next >
Encoding:
Text File  |  1992-11-19  |  2.8 KB  |  69 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gumby!wupost!cs.uiuc.edu!sparc0b!pjl
  3. From: pjl@cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: RUN TIME ARRAY INDEX ERRORS
  5. Message-ID: <Bxz3zH.4MH@cs.uiuc.edu>
  6. Sender: news@cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <1992Nov19.102258.4237@alchemy.chem.utoronto.ca>
  9. Distribution: na
  10. Date: Thu, 19 Nov 1992 17:16:29 GMT
  11. Lines: 56
  12.  
  13. In <1992Nov19.102258.4237@alchemy.chem.utoronto.ca> mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) writes:
  14.  
  15. >Hello experienced people!
  16. >     There seem to be two ways to detect array index errors at
  17. >run time. One is to explicitly check the index.
  18. >int x[20];
  19.  
  20. >if (j >= 0 && j < 20 && function1(x[j]))
  21. >    ...
  22. >     The other way is the C++ way,to overload the [] operator.
  23. >     Explicit checks clutter the code enormously. The operator
  24. >overloading method is transparent to us unless we remember the
  25. >class definition. It's neat.
  26.  
  27. *****>    Of course, you have to have a class for which [] is overloaded;
  28.     you can't overload [] for built-in types.
  29.  
  30. >     My question to ask you is this: How did C survive all these
  31. >years without such an obviously essential feature? How was
  32. >anybody able to construct programs of 100,000 plus lines of C
  33. >that worked well,without this feature? I'm a naive programmer;
  34. >there is some magic there that escapes me.
  35.  
  36. *****>    Obviously, in a working program, range checks serve only to
  37.     degrade performance.  To not go outside of vector bounds, just
  38.     don't do it.  Simple.  If you're dealing with a vector, you
  39.     should know its size or know what the sentenel is; if you don't,
  40.     you shouldn't be using the vector in the first place.
  41.  
  42.         Patient:    "Doctor, it hurts when I do this."
  43.         Doctor:        "So don't do that."
  44.     
  45.     For its alleged weaknesses, C is lean, fast, portable, close to
  46.     the machine, and flexible (unlike straight-jacket languages like
  47.     Pascal).  Languages shouldn't be defined to have a zillion
  48.     built-in checks to save programmers from themselves, IMHO.
  49.  
  50.     BTW, it is possible to perform source-to-source translations in
  51.     C to have the code peppered with range-checking code or I
  52.     imagine that some vendor's compilers have the option of doing
  53.     something similar by inserting range-checking code.  Such checks
  54.     would be disabled for a final product, of course.
  55.  
  56.     There are far more serious bugs in programs than range-checking
  57.     errors, so catching those doesn't make a dent anyway, IMHO.
  58.     Errors in algorithms, misreading or misinterpretation of
  59.     specifications, logic-erros, (typographical errors like that
  60.     one), ad nausium, are out there; range-checks are the tip of the
  61.     proverbial ice-berg, but if you use a vector properly, as I
  62.     originally stated, you should have no range errors.
  63.  
  64.     (Oh, I can just see the responses to this coming. :) )
  65. -- 
  66.     - Paul J. Lucas                University of Illinois    
  67.       AT&T Bell Laboratories        at Urbana-Champaign
  68.       Naperville, IL            pjl@cs.uiuc.edu
  69.