10

I am learning about pointers and references, and my question refers to this explanation, in particular the following section:

This suggests that the declaration int& ri = i creates a new memory cell, which has a value of &i and exists in unknown memory location.

To test this theory, I wrote a simple case, the result which is seen below:

enter image description here

I am perplexed by the fact that r and i have the same memory address, which seems to contradict the readings. The result suggests that int& ri = i loosely means "create an alias for memory cell i and call it r" such that both refer to exactly the same cell.

Is the document correct, or have I missed something?

3
  • I do not think its a duplicate, I am asking whether the reference actually occupies a cell in memory, not whether that address can be obtained. There is a difference.
    – jII
    Jun 13, 2013 at 6:02
  • 1
    The question might not be the same, but I think the answers apply to this one. Jun 13, 2013 at 6:14
  • It is up to the compiler. It makes sense for the memory to be directly referenced rather than indirect and I can't see why the compiler wouldn't make that optimisation, in which case it would say i, ri above that box and ri addr box and arrow wouldn't exist. Jan 17, 2020 at 11:59

5 Answers 5

12

Since r is a reference to i, all operations on r are transformed by the compiler into operations on i. So doing &r, gives you the memory address i is in.

(Note that unlike pointers, references have the property of not being 're-referenced' after declared - they always reference the same thing - so there is no way to write operations that operate 'on the reference', not 'on what is referenced')

2
  • Would it be possible to obtain the memory location of r?
    – jII
    Jun 13, 2013 at 5:59
  • 10
    @jesterII It is not possible to even ask the question. There is no guarantee that a reference has an address at all, it may be optimized out of existence by the compiler (unlike a pointer which must be somewhere)
    – Patashu
    Jun 13, 2013 at 6:00
9

C++11 §8.3.2/4

It is unspecified whether or not a reference requires storage.

By declaring a lvalue reference (T&) you create a conceptual alias to an existing memory location. Compiler may use the 'as if' rule to treat it as it wishes. It may create a pointer, it may just directly access memory, but you shouldn't care how it will be implemented.

The PDF you are reading describes a possible implementation of lvalue references but is wrong in general case. A good mind model for lvalue references would be giving a second name to the same variable, so you can access the same data via several different names (and scopes).

Also, you can't take an address of or create a pointer to rvalue, but you can create an rvalue reference to it.

2

The document says "Both pi and ri contain addresses that point to the location of i, but the difference lies in the appearance between references and pointers when they are used in expressions.", which is true.

You wrote "[reference] loosely means "create an alias for memory cell i and call it r" such that both refer to exactly the same cell" which is true too.

You probably misunderstood the document, you are right and so is the document.

0

I think this SO post may be of help to you. Quoting an answer given there:

A reference is an implicit pointer. Basically you can change the value the reference points to but you can't change the reference to point to something else.

I am not sure what you mean by "memory cell". Looking at your code in assembly may help you understand the lower-level semantics that are taking place. Since you appear to be using windows, I would suggest, if you are curious enough, to run your app in ollyDbg.

My take (and I could be very wrong) is that the compiler treats pi as a mutable pointer, whereas ri is an immutable pointer.

1
  • 1
    I think in this case looking at the assembly might be confusing: the compiler might require storage to implement a reference in some circumstances, and not in others. Semantically, a reference is just an alias, so it doesn't require any storage per se. Jun 13, 2013 at 6:16
0

r is independent reference(concept) or just another name for i. Whatever changes are made to either of them will be reflected in each other.

Not the answer you're looking for? Browse other questions tagged or ask your own question.