register variables

Ken Arnold%CGL arnold at ucsfcgl.UUCP
Thu Jun 27 07:35:29 AEST 1985


In article <6115 at ucla-cs.ARPA> alex at ucla-cs.UUCP writes:
>>Just be somewhat careful to declare things in the order you care
>>about.  This cuts both ways -- if you move to a machine with fewer
>>registers than yours, you can loose the most critical register uses in
>>favor of less critical.  For example, let's posit a two register
>>machine and the following code
>>
>>	register int	i, j;
>>
>>	for (i = 0; ...)
>>		for (j = 0; ...)
>>
>>Now, move it to a one register machine, and "j", which is the most
>>tested variable, is out of a register and "i", which is used less
>>often, is still in one.  Not what you want.
>
>You are assuming the compiler assigns register variables in the order
>they are declared, which is not always true.

It SHOULD always be true.  If a compiler doesn't put things in
registers in the order declared, it is not following (a) standard
practice, or (b) K&R (the closest thing we currently have to a
standard).  K&R says

	A "register" declaration is best thought of as an "auto"
	declration, together with a hint to the compiler that the
	varibles declared will be heavily used.  Only the first few
	such declarations are effective.

"first few" means "first few".  In the above listing, "i" comes before
"j" and therefore is part of the "first few" before "j" is.  Any compiler
which puts "j" in first is doing it wrong.

		Ken Arnold



More information about the Comp.lang.c mailing list