Is there any way to find the address of a reference?

Is there any way to find the address of a reference?

Making it more specific: The address of the variable itself and not the address of the variable it is initialized with.


Solution 1:

References don't have their own addresses. Although references may be implemented as pointers, there is no need or guarantee of this.

The C++ FAQ says it best:

Unlike a pointer, once a reference is bound to an object, it can not be "reseated" to another object. The reference itself isn't an object (it has no identity; taking the address of a reference gives you the address of the referent; remember: the reference is its referent).

Please also see my answer here for a comprehensive list of how references differ from pointers.

The reference is its referent

Solution 2:

NO. There is no way to get the address of a reference.
That is because a reference is not an object, it is an alias (this means it is another name for an object).

int  x = 5;
int& y = x;

std::cout << &x << " : " << &y << "\n";

This will print out the same address.
This is because 'y' is just another name (an alias) for the object 'x'.

Solution 3:

The ISO standard says it best:

There shall be no references to references, no arrays of references, and no pointers to references.

I don't like the logic a lot of people are using here, that you can't do it because the reference isn't "guaranteed to be just a pointer somewhere anyway." Just as int x may be only a processor register with no address, but magically becomes a memory location when & x is used, it still may be possible for the compiler to allow what you want.

In the past, many compilers did allow exactly what you're asking for, eg

int x, y;
int &r = x;
&r = &y; // use address as an lvalue; assign a new referent

I just checked and GCC will compile it, but with a strongly worded warning, and the resulting program is broken.

Solution 4:

No.

As Bjarne Stroustrup says in TC++PL, a reference can be thought of as just another name for an existing entity (object or function). While this is not always the most precise description of the underlying low-level mechanism that implements references, it is a very good description of the concept the references are intended to implement at the language level. Not surprisingly, the language provides no means to obtain the address of reference itself.

At language level reference is not guaranteed to occupy a place in storage, and therefore in general case it has no address.

Solution 5:

From another instance of this same question: $8.3.2/3 - "It is unspecified whether or not a reference requires storage (3.7).".

So the C++ standard allows the compiler/runtime implementor to choose whether or not a reference lives in a separate memory location. However note that if it does live in a separate memory location, you can't find its address in a standard-compliant manner. So don't do it.

If you take an address of a reference, by definition in the C++ standard, you will get the address of what it refers to, rather than the address of the reference, if in fact that reference even exists as a separate entity in the runtime environment or not.