Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!pyrdc!gmu90x!gmuvax2!2179ak From: 2179ak@gmuvax2.gmu.edu (JDPorter) Newsgroups: comp.sys.ibm.pc Subject: Re: Turbo C far pointers Message-ID: <579@gmuvax2.gmu.edu> Date: 15 Nov 89 22:42:32 GMT References: <4037@cs.yale.edu> Reply-To: 2179ak@gmuvax2.UUCP (JDPorter) Organization: George Mason Univ. Fairfax, Va. Lines: 20 In article <4037@cs.yale.edu> spolsky-joel@CS.YALE.EDU (Joel Spolsky) writes: >In article hp0p+@andrew.cmu.edu (Hokkun Pang) writes: >>I read from a book that "ptr[i]" will be converted to "*(ptr+i)" by the >>compiler, so the "*(ptr+1)" is faster than "ptr[1]". Is that right? >That's ridiculous. *(A+1), A[1], and 1[A] all produce exactly the same >code. There is no reason to believe that the compiler prefers one over Sorry, I must disagree. (item #0: '1[A]' looks very alien and undigestible to me.) But to get to the point: *(A+1) does NOT produce the same code as A[1]. (not for MSC, anyway.) The first form takes the pointer, increments it by one (as a pointer entity), and dereferences it. The second form places the specified index into an index register (or offset register, if you prefer) and dereferences the pointer PLUS the offset. In general, the SECOND form executes in FEWER cycles (contrary to the C programmer's notion that pointers are always the most efficient.) John Porter