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?

jII
  • 1,134
  • 2
  • 17
  • 29
  • 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 '13 at 06:02
  • 1
    The question might not be the same, but I think the answers apply to this one. – juanchopanza Jun 13 '13 at 06: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. – Lewis Kelsey Jan 17 '20 at 11:59

5 Answers5

11

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')

Patashu
  • 21,443
  • 3
  • 45
  • 53
  • Would it be possible to obtain the memory location of `r`? – jII Jun 13 '13 at 05: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 '13 at 06: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.

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
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.

Community
  • 1
  • 1
Ælex
  • 14,432
  • 20
  • 88
  • 129
  • 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. – juanchopanza Jun 13 '13 at 06: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.

user1502952
  • 1,390
  • 4
  • 13
  • 27