🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2309

Re: Pointer Arithmetic

#2309
From: Nathan D Green
Date: Sat, 16 Mar 1996 17:20
51 lines
2291 bytes
Jens Schweikhardt (schweikh@itosun.ito.uni-stuttgart.de) wrote:
: In article <4i805f$47d@solutions.solon.com>,
: Dan T. Abell <dabell@quark.umd.edu> wrote:
[snip]
: >Do most compilers use plain ol' multiplication or do they use bit-shift  
: >operators.  Also, does the answer depend on the type of the array p?

: The answer is, as so often, "it depends".
: What the compiler will generate for an int array acces depends on

[Among other things...]

: - What the instruction set of the machine supports.
:   There may be machines that have instructions to compute
:   base+(index * somesize) [I bet there are!]

Intel 80386 and up have such a mode for 32-bit addressing.
Valid values of somesize are 1,2,4,8.

16-bit addressing is still stuck with bit shifting.

(According to the references I have.)

: If you want to be sure how your compiler does it, trying the -S option
: might give an assembler source of your C code. I once investigated it with
: gcc on an i486 machine with sizeof int = 4. It does use bit shifts
: to compute adresses in int arrays. You don't need to be an assembler
: guru to see what's going on in a small program.

I tried "result=int_array[index];" with gcc 2.7.2 (and 2.5.8) on an i486 
and got:

	movl index,%eax
	movl int_array(,%eax,4),%edx
	movl %edx,result

Which looks like at least the current gcc uses scaled indexes.

: To put it in a nutshell:
:   You only can be sure by looking at the compiler output.

: Bye, Jens

: -- 
: SIGSIG -- signature too long (core dumped)

--
Nathan Green			(ngreen@plains.nodak.edu)
GCS d? s: a21.3>21.4 C+++ UV P>+ L++>+++ E W N++ o+>++ K+ 
w---(--) M- V PS PE Y t+ 5 X R tv-(--) b++>+++ DI++>+++ D+ 
G+>++++ e(*)>+ h!>++ !r !y+ (Version 3.1)
Technical Details
Message-ID: <4iet9l$b04@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4i805f$47d@solutions.solon.com> <4i97kl$8i5@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: ngreen@plains.nodak.edu (Nathan D Green)
Newsgroups: comp.lang.c.moderated
Subject: Re: Pointer Arithmetic
Date: 16 Mar 1996 11:20:53 -0600
Organization: North Dakota Higher Education Computing Network (NDHECN)
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4iet9l$b04@solutions.solon.com>
References: <4i805f$47d@solutions.solon.com> <4i97kl$8i5@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: TIN [version 1.2 PL2]