0

I came across following statement. How is that possible that two things reside at same address i.e. actual variable and reference variable have same memory address?

Regardless of how a reference is implemented, a reference has the same memory address as the item it references.

Cœur
  • 37,241
  • 25
  • 195
  • 267
danish sodhi
  • 1,793
  • 4
  • 21
  • 31
  • 4
    A reference does not have an address at all. – Baum mit Augen Oct 25 '17 at 13:27
  • 1
    References are not objects, don't have memory or a size and you cannot `std::memcpy` them. Whenever you try to do any of those things you get the traits of the object they reference instead. Someone else can dig up the standard quotes for that. – nwp Oct 25 '17 at 13:28
  • 1
    A reference is just another name for an existing object. That's all. So when you take the address, you are taking the address of the object. Of course it'll be the same each time. – StoryTeller - Unslander Monica Oct 25 '17 at 13:34

3 Answers3

5

That's because a reference is not a "thing": it's an alternate name for an object (which may be a variable).
It does not have an address: trying to get the address of a reference will only give you the address of the object it aliases.

The sentence is misleading.

Quentin
  • 62,093
  • 7
  • 131
  • 191
4

This means that the implementation of a reference is an implementation detail of the compiler. You cannot get the address of a reference variable using the & address of operator. It will always yield the address of the referenced instance.

user0042
  • 7,917
  • 3
  • 24
  • 39
3

This means that if you take the address of the reference variable (using &) it will be the same as when taking the address of the referenced variable. This is the whole purpose of references, they are the same thing as what they "point" to.

gonutz
  • 5,087
  • 3
  • 22
  • 40