[comp.lang.c] Reusing register variables

wong@rtech.UUCP (J. Wong) (12/12/86)

In article <1706@batcomputer.tn.cornell.edu> garry%cadif-oak@cu-arpa.cs.cornell.edu writes:
>In a recent article kanner@apple.UUCP (Herbert Kanner) wrote:
>>...
>>		L = *((sometype *) chp)++;
>>...
>>Our problem arises with the allegation that K&R makes this construct
>>illegal...
>
>This was some discussion on the net a few months about whether this ought
>to be legitimate.  The context I would use it in is trying to recycle
>"valuable" register variables to point to different things during the life
>of a routine. For example, if I happen to know that A and B never overlap
>during execution, and that my machine only has 1 available-to-C register,
>I might want to do:
>
>	    ...
>	    register	char	*A;
>#           define	B	((float *)A)
>	    ...
>
>and expect both *A++ and *B++ to work and to be in registers for me. If
>I know that a pointer and an int take the same space and instructions on
>my machine I might also want to get away with:
>
>	    register	char	*A;
>#	    define	B	((int)A)
>	    ...
>	    ... *A++; ...
>	    ...
>	    ... B = 27;
>
>Unfortunately, that's not the way things stand on any compiler I have access
>to - your compiler must be exceptionally forgiving.
>
>I think the new spec doesn't disturb the status quo.
>

A better way to reuse register variables is to block structure your code, e.g.,

	{
		register char	*A;

		code using A
	}
	{
		register float	*B;

		code using B
	}

This is clearer and avoids any problems posed by "(type *)A++".
It also reuses register variables (at least on the machines
with which I've dealt, and hence is completely portable in my
experience.)
-- 
				J. Wong		ucbvax!mtxinu!rtech!wong

****************************************************************
You start a conversation, you can't even finish it.
You're talking alot, but you're not saying anything.
When I have nothing to say, my lips are sealed.
Say something once, why say it again.		- David Byrne