Can the address of a variable change during a function?

David Keppel pardo at june.cs.washington.edu
Sun Nov 20 10:31:02 AEST 1988


Consider the following bit of C:

	    int
	zorch()
	{
	    int i, j;

	    j = 0;
	    bork (&i);
	    while (--i)
		j += i;
	    splat (&i, &j);
	    j += i;
	    return (j);
	}

Most current implementations will assign a location to each of i and
j at function declaration elaboration (e.g., when the function is
entered).  I can imagine, however implementations in which the address
of a variable is not constant throughout the function (see below).  In
this case, "bork" could squirrel away the pointer to 'i', and "splat"
could, say, read whatever is at the end of that 'squirreled away'
pointer.  In this case, the behavior of the program will be *very*
different than if the variables had been declared with

	    auto int i;
	    int j;

In which case the address of 'i' passed to bork() and splat() is (I
think) guaranteed to be the same.  I believe that the compiler is free
to implement the first bit of code as something like:

	; i => r2
	; j => r3


		clear	r3		; j <- 0
		move	r2,-(sp)	; give i an address
		move	sp,r0		; pass address of i
		call	bork

		move	(sp),r2		; unspill i
	L0:
		dec	r2		; --i
		br.leq	L1
		add	r2,r3,r3	; j += i
		branch	L0

	L1:
		move	r3,(sp)		; give j i's old address
		move	sp,r0		; pass address of j
		move	r2,-(sp)	; give i a new address
		move	sp,r1		; pass address of i
		call	splat

		add	(sp),-4(sp),r0
		sub	#8,sp,sp	; patch up sp
		return

I also belive that 'auto' has been removed from dpANS C.
Summarizing, the question is: "can a local parameter have several
addresses during one invocation of the function that it is declared
in?"  All help is appreciated.

	;-D on  ( Who would have thunk it... )  Pardo
-- 
		    pardo at cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo



More information about the Comp.std.c mailing list