Path: utzoo!mnetor!uunet!husc6!bbn!rochester!srs!craig From: craig@srs.UUCP (Craig Schmackpfeffer) Newsgroups: comp.lang.c Subject: Re: Indexing vs pointers Message-ID: <834@srs.UUCP> Date: 2 May 88 15:33:30 GMT References: <1988Apr8.183815.3187@utzoo.uucp> <449@goofy.megatest.UUCP> <2200@louie.udel.EDU> <587@vsi.UUCP> <629@clinet.FI> Reply-To: srs!craig@cs.rochester.edu (Craig Schmackpfeffer) Organization: S.R. Systems, Rochester NY Lines: 44 In article <629@clinet.FI> msa@clinet.UUCP (Markku Savela) writes: >In article <587@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes: >> >>Let's put this goto business in perspective: >> >> /* >> * strcpy() - written by John Doe >> */ >> strcpy(dst, src) >> char dst[], src[]; >> { >> int i; >> >> for (i = 0; dst[i] = src[i]; i++) >> ; >> } >> >> Who objects to this? > > I don't! I'm actually using similar loops in my C-programs >that are written for MS-DOS machines. Just try compiling the above >code and a variation using pointers with LARGE MEMORY MODEL! > > Compare the results (in ASM). Believe or not, the variant >using indexes is much more compact than the one using pointers (and >saying "register i" improves it even more...) > > The question is of course: is there really any penalty in >using indexed versions with other architectures (like motorola)? > >-- >markku savela, msa@clinet.fi The problem occurs when you are trying to index anything bigger than a single character. The index must be scaled by the size of the elements in the array. If you use pointers, an add instruction can be used to increment to the next element. However if you use an index on a compiler that doesn't optimize, it will have to generate code to multiply the index each time through the loop. Craig -- Craig Schmackpfeffer @ S.R. Systems {allegra,rutgers,ames}!rochester!srs!craig