Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!biar!trebor From: trebor@biar.UUCP (Robert J Woodhead) Newsgroups: comp.lang.c Subject: Re: Adding two pointers Message-ID: <542@biar.UUCP> Date: 7 May 89 18:42:33 GMT References: <2765@buengc.BU.EDU> <563@lzaz.ATT.COM> <4093@ficc.uu.net> <17340@mimsy.UUCP> <17357@mimsy.UUCP> Reply-To: trebor@biar.UUCP (Robert J Woodhead) Organization: Biar Games, Inc. Lines: 31 In article <17357@mimsy.UUCP> chris@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@biar.UUCP "The lamb will lie down with the lion, but the lamb won't get much sleep." -- Woody Allen.