home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!destroyer!gumby!yale!mintaka.lcs.mit.edu!hal!mjd
- From: mjd@hal.gnu.ai.mit.edu (Sievan Janacziewski)
- Subject: Re: Pointers outside of array boundary
- Message-ID: <mjd.712419017@hal>
- Originator: mjd@hal.gnu.ai.mit.edu
- Sender: news@mintaka.lcs.mit.edu
- Organization: MIT Laboratory for Computer Science
- References: <1992Jul28.180945.22332@hubcap.clemson.edu>
- Date: Wed, 29 Jul 1992 14:10:17 GMT
- Lines: 28
-
- mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
-
- > float *x = malloc(10*sizeof(float));
- > --x;
-
- >which is supposed to give an array whose elements are addressed
- >as x[1]..x[10].
- >My question is: Can anyone name an existing machine on which this code
- >could fail, or give a really convincing hypothetical argument why it
- >is a bad idea? My interlocutor claims the criticism of this form is
- >legalistic, and not based on practical considerations.
-
- I was going to make up some thing about how maybe the array gets
- allocated at the top of memory, but there's a much better reason not to
- do this: C arrays always start at 0, never at 1, and by resorting to
- cute compiler hacks to fudge up the appearance of a 1-based array, one
- will only confuse oneself and other people who have to read,
- understand, maintain, and modify the code.
-
- If 0-based arrays are unclear to your correspondent, then he or she
- should practice more, so that they become more familiar, rather than
- resorting to obfuscation to cover his or her inadequacies as a programmer.
-
- This has nothing to do with legalism.
- It is the most practical of all possible considerations.
-
- Sievan Janacziewski
- mjd@gnu.ai.mit.edu
-