Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!ames!sun-barr!decwrl!decvax!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Yet Another Silly Question Message-ID: <13606@haddock.ima.isc.com> Date: 6 Jun 89 18:28:37 GMT References: <6458@sdcsvax.UCSD.Edu] <2550091@hpisod2.HP.COM] <18229@unix.cis.pittsburgh.edu> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 26 In article <18229@unix.cis.pittsburgh.edu> jcbst3@unix.cis.pittsburgh.edu (James C. Benz) writes: >[someone writes about the loop: for (p=&a[0]; p<&a[MAX]; ++p) ...] >The machine still has to dereference &a[MAX] on each iteration of the loop, >doesn't it? There's no dereference involved. It does have to *evaluate* &a[MAX]. >unless the optimizer is really on top of things, it has no way of generating >the comparison address as a constant. It really has no way of knowing that >&a[MAX] won't change during the course of the program, so it must be >re-computed on each iteration of the loop. The value "&a[MAX]" cannot change during the scope of the identifier "a". On a VAX-like architecture, the array a (which has block scope and auto storage duration) is commonly implemented as a constant offset from a frame pointer; the additional offset MAX*sizeof(int) will be absorbed by constant folding. The only non-obvious optimization is to keep the value &a[MAX] in a register. >In any situation I hope to encounter in the kind of work I do, (database >admin) I will trade off the readability of the first example against the run >time efficiency of the second, and choose readability every time. I don't necessarily agree that pointers are less understandable than indexing, but I agree that readability/maintainability should be the first concern. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint