tags:

views:

239

answers:

8

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

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

+4  A: 

Just use the '&' operator. e.g :

int x = 3;
int &y = x;
cout<<&y<<endl;

This will return the address of x since y is nothing more than the address of x.

Jesse Emond
I think Sandeep means the *address of the reference itself*
Shmoopty
that will effectively print out the address of x (since y being a reference is just another name for x).
Evan Teran
yea I wasn't actually sure if he/she meant the address of what the reference points to or the reference itself so I posted this answer just in case. But, isn't the reference nothing more than the address of what it points to? So the address of the reference would be the address of "x" in my example? I'm just wondering. :)
Jesse Emond
I meant to say the address of variable itself as it is posssible in case of pointer(pointer has its own address).
Sandeep
Oh well, a reference is just an alias(an alternate name) for an object. It doesn't get its own memory cell.
Jesse Emond
@Sandeep: "pointer has its own address". Correct. That's because the pointer is a *separate* variable holding the address of the variable it points to. In the case of a reference, there is *no separate variable*. The reference is just another name for the variable being referenced. Taking the address of the reference will just return the address of the variable being referenced.
Vulcan Eager
@Agnel: As reference can be implemented with pointers, we must be aware that there can be a time penalty in using reference instead of the variable itself (because of dereferencing), so it is more subtle than "just another name".
rafak
+1  A: 

Not reliably, as references don't have to have a unique location in addressable memory.

Shmoopty
+8  A: 

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

Parashift 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

Brian R. Bondy
You are correct. But sometimes it is easier to use the term alias rather than referent.
Martin York
+2  A: 

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.

AndreyT
A: 
moon
+4  A: 

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, &r = x; &r = &y;. I just checked and GCC will compile it, but with a strongly worded warning, and the resulting program is broken.

Potatoswatter
+1 For quoting the standard.
GMan
A: 

Not by itself. If you want its "address", shove it in a struct or class. Even then that isn't necessarily guaranteed to get you within the vicinity of what you probably want to do which is using a pointer. If you want proof, the sizeof of a reference is equal to the referent type. Try it with char & and see.

MSN
+5  A: 

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

Martin York