home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!darwin.sura.net!mips!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.c
- Subject: Re: Pointers outside of array boundary
- Date: 29 Jul 1992 18:23:36 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 22
- Message-ID: <156nn8INNe8f@early-bird.think.com>
- References: <1992Jul28.180945.22332@hubcap.clemson.edu> <mjd.712419017@hal> <1992Jul29.151518.19815@hubcap.clemson.edu>
- NNTP-Posting-Host: telecaster.think.com
-
- In article <1992Jul29.151518.19815@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
- >In this case, wasting the 0th element of an array
- >seems like a relatively small price to pay, if that's the only way
- >to write 1-based code.
-
- But it's not. You can also use a macro, e.g.
-
- #define AREF(array,index) (array)[(index)-1]
-
- While this looks like it will require an extra subtraction for every array
- access, if the array you're indexing is automatic or global (as opposed to
- dynamically allocated by malloc()) then a decent compiler should be able to
- optimize it away, by precomputing the constant address array-1 (yes, this
- is precisely the address that the original poster was trying to compute in
- his C code -- the difference here is that the compiler is doing it, and it
- knows whether it's valid for the target architecture, while the application
- programmer can stick to writing portable code).
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-