5

We are studying for our CS midterm on Tuesday.

Our professor put some study material online, including the following:

"Further, you should be able to draw a memory diagram given some code, such as:"

void foo( int &x )
{
 x = 1000;
}

void bar( int *x )
{
 *x = 1000;
}

void foobar( int x )
{
 x = 1000;
}

int main()
{
   int x = 1234;
   int &y = x;

   int *z = &x;

   int  array_1[5];
   int *array_2[5];

   array_1[0] = 10;
   array_2[0] = (int*)10;
   array_2[1] = &y;

   array_2[2] = &x;

   foo( x );
   foo( y );
   foo( *z );

   bar( &x );
   bar( &y );
   bar( z );

   foobar( x );
   foobar( y );
   foobar( *z );

   return 0;
}

We are trying to go through it one step at a time, to see what is allocated on the stack, what is allocated on the heap, and what is the value of each thing.

What we don't understand is: &y holds the address of x, but &y = &x... so what is the address of y? Like, doesn't the stack need to hold y???

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Exact duplicate of [Is there any way to find the address of a reference?](http://stackoverflow.com/questions/1950779/is-there-any-way-to-find-the-address-of-a-reference). – Carl Norum Mar 04 '12 at 23:21
  • 1
    What do your class notes and textbook have to say on the matter? – Rob Kennedy Mar 04 '12 at 23:25
  • 3
    Though I don't think the question is an exact duplicate, the chosen answer to the question linked by @CarlNorum is definitely relevant and worth a read. – André Caron Mar 04 '12 at 23:28

4 Answers4

9

Long story short - there is no spoon y. So nothing will be allocated on stack for y. This is because y in your case is a reference.

Reference is just an alias and has no address. In other words - it is the same thing as x, but named differently. That's how you should think of references as a C++ programmer. In fact, compiler might use an address of the object in order to implement a reference (i.e. when you pass by reference). And even in that case it might be only stored in a register, thus have no memory address. But these are implementation details you are not supposed to know about :) I recommend you check out this C++ References FAQ.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
  • Wow. Bad me. For some reason I was fairly certain references got their own pointer allocated, but dumping some code I see you are quite right. Thinking more it has to be this way since they need to alias when taken by address. Duh. Deleted my response. :) – Joe Mar 04 '12 at 23:29
  • References are usually implemented as pointers. And those pointers get allocated on the stack. I don't think this is correct. The real point is that there is no way to get the address of this pointer although it exists! See my answer. – usr Mar 04 '12 at 23:38
  • 1
    In this toy example the compiler probably optimized out the reference but that would not be true for references passed as method args. – usr Mar 04 '12 at 23:39
  • @usr: In case with method args, address of the actual variable is passed. That is most likely to be passed through a register and thus won't take any space on stack whatsoever. –  Mar 05 '12 at 15:03
  • z is also likely to be optimized away. **So is anything in this main method because it is side-effect free.** If we are talking about optimizing compilers, any statements about stack space are unreliable. Also: Talking about the stack is usually talking about an implementation detail. If we are talking about the real world his professor kind of needs some education here! – usr Mar 05 '12 at 18:16
3

y is neither an object nor a function, thus it has no address. It is a reference to the variable x, which means that whenever you use y it is as if you used x. Note that this does not at all require that y holds a pointer to x.

In the function main, y probably does not exist at all in the generated executable; the compiler can trivially replace all uses of y as if they were uses of x.

In your function bar (again, assuming it actually exists in the generated executable and is not entirely inlined), x, which is a reference, does have to exist somewhere, because it must refer to an object outside the scope of the function. Such references are typically implemented as pointers, but implementers are free to implement references however they best see fit.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

Think of references as "just another name for". In other words, "y" is just another name for "x" and therefore doesn't have an address.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
0

Regarding int &y = x; If you look at an assembly (compiled with a -S) listing, you will see that in many cases the address of x is places on the local stack....hence you could consider that stack address as the address of y.

Tony C
  • 1