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