Indexing vs pointers

Craig Schmackpfeffer craig at srs.UUCP
Tue May 3 01:33:30 AEST 1988


In article <629 at clinet.FI> msa at clinet.UUCP (Markku Savela) writes:
>In article <587 at vsi.UUCP> friedl at 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 at 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



More information about the Comp.lang.c mailing list