Array indexing vs. pointers...

Steven Ryan smryan at garth.UUCP
Wed Sep 21 06:43:42 AEST 1988


>	for (i=0;i<n;i++) {
>	  a batch of code referring to a[i]
>	  }
>versus
>	tmp = a;
>	for (i=0;i<n;,i++,tmp++) {
>	  same code referring to *tmp
>	  }

Just to muddy the waters. Most cpus allow address expressions like
constant_offset+base_register, where base registers are a scarce resource.

In the first case, the loop refers to a[i], b[i], ..., the compiler can
sometimes generate better code by only putting i in a register and leaving
a_base_address, b_base_address, ... as constant offsets. Making a separate
pointer for each array can scrunch the register file.

Just a reminder that optimisation is not a trivial process.



More information about the Comp.lang.c mailing list