From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,effb80d4bb7716dd X-Google-Attributes: gid103376,public From: William Clodius Subject: Re: Pointer Arithmetic (was: Wanted: Ada STL....) Date: 1999/02/02 Message-ID: <36B78CDD.1372@lanl.gov>#1/1 X-Deja-AN: 439963714 Content-Transfer-Encoding: 7bit References: <790f4q$3l@bgtnsc01.worldnet.att.net> <797na3$obg$1@nnrp1.dejanews.com> Content-Type: text/plain; charset=us-ascii Organization: Los Alamos National Lab Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-02-02T00:00:00+00:00 List-Id: adam@irvine.com wrote: > > I don't know how this relates to STL, but I think I can see what the poster > is getting at. Pointer arithmetic can be efficient when you're trying to > step through an array, since you don't have to keep multiplying an index by a > constant value to access the array elements. I'm not sure how much of a > difference that makes these days---chips have gotten so fast that > multiplication might not be that big a deal any more. But anyway, let's > assume for argument's sake that efficiency is a concern and that we want to > avoid multiplication if we can. > ??? What are your reasons for thinking that indexing an array requires multiplications that are not also required for the useage of pointers? For most machines in use the most efficient implementations of arrays is as a single contiguous block that is accessed by mapping indices directly to the equivalent pointer operations. (The exceptions are more easilly mapped to arrays and not to pointers.) It is true that mapping involving an index other than the most rapidly varying one can be thought of as involving a multiplication, but the same is true of the equivalent pointer arithmetic. In both cases the same functionality can be obtained by adding a (n easilly determined) finite integer to the current memory index. The analysis for the compiler to do this is so simple, I would not be surprized if Ada Lovelace discussed it for Babage's machine in the 1800s. Any compiler that does not perform that analysis is broken. It is sometimes thought that because it is relatively easy for the programmer to move some of the pointer arithmetic outside the loop, that pointer arithmetic is more efficient, but the algorithms necessary to perform that motion by the compiler have been available since the first Fortran compiler, ca. 1957. Any compiler that does not perform that analysis is a toy and not a professional system. It is more likely that a programmer will not perform the necessary analysis for the pointers, than that a compiler will not perform the analysis. -- William B. Clodius Phone: (505)-665-9370 Los Alamos Nat. Lab., NIS-2 FAX: (505)-667-3815 PO Box 1663, MS-C323 Group office: (505)-667-5776 Los Alamos, NM 87545 Email: wclodius@lanl.gov