Adding two pointers

Robert J Woodhead trebor at biar.UUCP
Mon May 8 04:42:33 AEST 1989


In article <17357 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>>	sub	base,low,r0
>>	sub	base,high,r1
>>	add	r0,r1,r0
>>	div	r0,$2,r0
>	and	r0,$~3,r0
>>	add	r0,base,mid

Gee, if you define things as an array of pointers, you not only get
clearer source code, but more efficient machine code:

	ptr=&ptrArray[(high+low)/2]

becomes (pardon my assembly language, I'm extrapolating from your example)

	add	high,low,r0	; add high and low
	shr	r0,$1,r0	; divide by 2 (gee, a smart compiler!)
	shl	r0,$2,r0	; adjust to wordsize
	add	ptrArray,r0,r0	; add in the base address
	move	r0,ptr		; all done.

Seems to me this whole argument is spurious.  You can't add/mul/div/sub two
pointers together because the result is semantically meaningless.  You want
to redefine the language to ``hack in'' an operation that is already there,
and your hack will result in code that is harder for humans to read, and
harder for compilers to optimize.

-- 
Robert J Woodhead, Biar Games, Inc.  !uunet!biar!trebor | trebor at biar.UUCP
"The lamb will lie down with the lion, but the lamb won't get much sleep."
     -- Woody Allen.



More information about the Comp.lang.c mailing list