Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!labrea!rutgers!mailrus!cornell!uw-beaver!uw-june!pardo From: pardo@june.cs.washington.edu (David Keppel) Newsgroups: comp.std.c Subject: Can the address of a variable change during a function? Message-ID: <6472@june.cs.washington.edu> Date: 20 Nov 88 00:31:02 GMT Reply-To: pardo@cs.washington.edu (David Keppel) Organization: U of Washington, Computer Science, Seattle Lines: 68 Summary: Expires: Sender: 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@cs.washington.edu {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo