Another silly question

Pete Holsberg pjh at mccc.UUCP
Wed May 24 03:46:28 AEST 1989


In article <1677 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
=Well, if the program that used subscript notation was something like:
=
=	for (i = 0; i < LEN; i++)
=		a[i] = 0;
=
=and the program that used pointer notation was something like:
=
=	p = &a[0];
=	while (p < &a[LEN]) 
=		*p++ = 0;
=
=the answer has nothing whatsoever to do with the equivalence of "a[i]"
=and "*(a + i)", since the latter program doesn't use the latter
=construct, so you did ask the wrong question.

So it seems!

=It has, instead, to do with the fact that the equivalence of the two
=constructs in question is not as trivial as the equivalence of "a[i]"
=and "*(a + i)", and therefore it may be less likely that the compilers
=will generate the same code for them.  

OK, so even though the two pieces of code are doing the same job and one uses
index notation while the other uses pointer notation, the compiler is not 
likely to notice this.

=There may well be compilers that
=*do* generate the same code for them - rewrite the first loop as:
=
=	for (i = 0; i < LEN; i++)
=		*(a + i) = 0;
=
=and then note that on most architectures, this requires that the value
=in "i" be multiplied by "sizeof a[0]" before being added to the address
=represented by the address of "a[0]", and do a strength reduction on
											    ^^^^^^^^^^^^^^^^^^
												could you explain this?

=that multiplication; you then find the induction variable not used, and
                                        ^^^^^^^^^^^^^^^^^^
										and this?

=eliminate it, and by the time the smoke clears you have the loop in the
=first example generating the same code as the loop in the second
=example.  (I don't know whether there are any compilers that do this or
=not.)
=
=If the code generated for the two constructs is different, that could
=account for performance differences.

I'll try it.  Thanks for the explanation.
-- 
Pete Holsberg, Mercer County Community College, Trenton, NJ 08690  
{backbone}!rutgers!njin!princeton!njsmu!mccc!pjh



More information about the Comp.lang.c mailing list