[comp.lang.c] Addresses of static versus auto variables

ka@june.cs.washington.edu (Kenneth Almquist) (01/22/89)

john@chinet.chi.il.us (John Mundt) writes:
> A static variable will run faster if it is addressed scads of times
> since its final address does not have to be computed each time it
> is accessed.  

This is not necessarily true.  The offset of an automatic variable can
generally be represented in one or two bytes, whereas the address of a
static variable generally takes four bytes on modern machines.  Thus
the cost of loading the address of a variable on the stack includes
the cost of reading one or two bytes plus the cost of an addition.  The
cost of loading the address of a static variable, on the other hand,
includes the cost of reading four bytes.  So it will cost less to take
the address of an automatic variable if the cost of an addition is less
than the cost of reading two or three extra bytes.

A very common way to move data around in a computer is to pass it through
the ALU.  On such a machine that works this way, the addition is free (if
the instruction set is designed correctly).  On the other hand, the cost
of reading two or three extra bytes is not likely to be zero.
				Kenneth Almquist