home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16572 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  2.5 KB

  1. Path: sparky!uunet!caen!sdd.hp.com!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: RUN TIME ARRAY INDEX ERRORS
  5. Date: 19 Nov 1992 18:16:15 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 43
  8. Message-ID: <1egllfINNb57@early-bird.think.com>
  9. References: <1992Nov19.102258.4237@alchemy.chem.utoronto.ca>
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <1992Nov19.102258.4237@alchemy.chem.utoronto.ca> mbersohn@alchemy.chem.utoronto.ca (M. Bersohn) writes:
  13. >     My question to ask you is this: How did C survive all these
  14. >years without such an obviously essential feature? How was
  15. >anybody able to construct programs of 100,000 plus lines of C
  16. >that worked well,without this feature? I'm a naive programmer;
  17. >there is some magic there that escapes me.
  18.  
  19. Your naivete is in assuming that all the hundreds of thousands of lines of
  20. C code out there work properly.  Many standard Unix commands, for instance,
  21. will screw up badly if given too much input, for instance (there was an
  22. article in CACM a year or so ago that demonstrated the results of giving
  23. random input to various Unix commands -- it wasn't very pleasant).  The
  24. Internet worm took advantage of a lack of bounds checking in the finger
  25. server (it used gets(), which doesn't have a buffer size parameter).
  26.  
  27. But the practical answer to your question is that the reason most C
  28. implementations don't do array bounds checking is performance.  And the
  29. reason it isn't as big a problem as it might be is that it's not really
  30. that hard to keep from accidentally going outside an array.  You don't need
  31. to check before each array operation (as a simple bounds checker (such as
  32. one implemented by overloading operator[]) would), but only when setting
  33. the index variable.  Often you can take advantage of invariants so that you
  34. know that the index is valid; for instance, in the code
  35.  
  36. int my_array[ARRAY_SIZE], i;
  37.  
  38. for (i = 0; i < ARRAY_SIZE; i++)
  39.     my_array[i] = f(i);
  40.  
  41. it's not necessary to do bounds checking on each assignment, since the loop
  42. is constructed so that it can't exceed the array bounds.  A good optimizing
  43. compiler might be able to infer this and leave out bounds checks, but it's
  44. a difficult optimization to implement.
  45.  
  46. Also, if you use the right library routines, (e.g.  fgets() rather than
  47. gets(), strncpy() rather than strcpy() when copying between different-sized
  48. arrays) you get automatic checking there.
  49.  
  50. -- 
  51. Barry Margolin
  52. System Manager, Thinking Machines Corp.
  53.  
  54. barmar@think.com          {uunet,harvard}!think!barmar
  55.