Another silly question

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Apr 28 05:04:44 AEST 1989


In article <1266 at l.cc.purdue.edu> cik at l.cc.purdue.edu (Herman Rubin)
		[the infamous proponent of assembly language] writes:
-In article <10135 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
-> In C, pointer arithmetic ALWAYS involves scaling by the size of the
-> pointed-to objects.  This is one of Dennis's really useful insights.
-> It is so fundamental to C that I have to worry about an instructor
-> who claims otherwise.
-For the same operation, one way will be better on one machine, and a 
-different way on another.  There are machines with index operations, where
-the multiplication by the appropriate power of 2 is invisible hardware,
-there are machines where increment and decrement for addresses is invisible
-hardware, and machines where neither of these is the case.  I suspect that
-the number of ways of doing this is comparable to the number of discussants
-of this on comp.lang.c.
-Now suppose I am doing some serious array operations, and I have to know
-whether one array buffer is longer than another.  The elements are of type
-long.  Do I have to do this multiplying and dividing by 4 all the time?
-Another example of "user-friendly" which turns out to be "user-inimical."

I can make no sense whatsoever out of your comment.
	long a[ASIZE], b[BSIZE], *ap = &a[aindex], *bp = &b[bindex];
	if ( ASIZE > BSIZE )
		...
	if ( sizeof a > sizeof b )
		...
	if ( aindex > bindex )
		...
	if ( ap > bp )
		...
You don't have to do any "multiplying and dividing by 4 all the time".
Neither does the compiler.
There is virtually no sensible operation you can attempt with arrays
or pointers in C that requires you to deal with such scaling; it's
taken care of for you by the compiler.



More information about the Comp.lang.c mailing list