Path: utzoo!attcan!uunet!cs.utexas.edu!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Another silly question Message-ID: <10153@smoke.BRL.MIL> Date: 27 Apr 89 19:04:44 GMT References: <2459@nmtsun.nmt.edu> <10135@smoke.BRL.MIL> <1266@l.cc.purdue.edu> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 34 In article <1266@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) [the infamous proponent of assembly language] writes: -In article <10135@smoke.BRL.MIL>, gwyn@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.