12

In C++, is the address of a reference to a dereferenced pointer guaranteed to be the same as the address of the pointer?

Or, written in code, is the following assertion guaranteed to always hold true?

SomeType *ptr = someAddress;
SomeType &ref = *ptr;
assert(&ref == ptr);
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Jesper
  • 121
  • 4
  • 3
    @dlev: how could he possibly "try" whether it is *guaranteed* to be the same? And why would he ask whether it's guaranteed, if all he wants to know is whether it happens to be equal for one particular run of one particular program on one particular C++ implementation? – Steve Jessop May 19 '11 at 14:47
  • @Steve he can't, obviously, but running through it a couple times should be more that enough to convince you that it's true. That said, you're right, he did say *guaranteed*. – dlev May 19 '11 at 14:49
  • The code is correct, the question is more "questionable". I would read "the address of the pointer" as `&ptr`, which is not the same as the address of the object pointed to. – Bo Persson May 19 '11 at 14:50
  • @dlev: on that basis I'm convinced that `sizeof(int) == sizeof(void*)` is guaranteed. I predict badness ahead ;-). We only know that trying Jesper's `assert` gives the right answer because we happen to know there's a fundamental reason why you're going to get the same answer everywhere. But if you didn't know that it was the same everywhere then you couldn't rely on the test. And if you did know it was the same everywhere, you'd already know the answer to the question... – Steve Jessop May 19 '11 at 14:53
  • @Steve what could possibly go wrong? It's all just bytes, anyway. – dlev May 19 '11 at 14:56
  • @dlev: true, how can bytes possibly go wrong? There's only 256 of them, collectively we've got 'em outnumbered. – Steve Jessop May 19 '11 at 14:58
  • @Steve, I don't think the C standard disallows more than 256 distinct bytes! Surely you mean "octets". :-) – Nicholas Wilson Jun 04 '13 at 14:34
  • @NicholasWilson: sure, but read the conversation: I think I'm facetiously assuming that anything that appears to be true when you run a bit of test code, is always true. Hence, only 256 distinct values of char ;-) – Steve Jessop Jun 04 '13 at 14:50
  • @Steve Indeed. PS. What's the 256th value on your machine? I have signed bytes over here! ;-) – Nicholas Wilson Jun 04 '13 at 15:01
  • @Nicholas: Um. -128 (or 127, depending which is the 1st value). – Steve Jessop Jun 04 '13 at 15:02

4 Answers4

4

Yes, that is correct and will always be true.

Reference is nothing but an Alias of the type which it is referring to. It does not have a separate existence, it is always tied up to the one it is referring.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

Yes, provided of course that someAddress is not a null pointer, or otherwise not allowed to be dereferenced. In that case, behavior is undefined, although your implementation might well behave as though they are equal, especially with low optimization levels.

If you want to be precise, then &ref isn't really the "address of a reference", it's the "address of the referand of a reference". Since ref was bound to *ptr, that means the referand of ref and the referand (or pointee if you prefer) of ptr are the same object, and hence the two addresses &ref and ptr are equal.

And as Bo points out, what you're comparing &ref with is the "value of the pointer", or the "address stored in the pointer", rather than "the address of the pointer".

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • +1 it's important to notice that `&ref` does not return the address where the reference itself is stored, but the address of the object being referenced. – Matthieu M. May 19 '11 at 15:28
2

Yes, if the reference itself has an address, it's managed by the implementation and not accessible from code. In any case, it's just a different name for the same object.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

Yes, your understanding is correct. And for 99.(9)% of the code in the world, your code will be correct. For the pedants in the audience (myself among them), this assertion is not always true:

SomeType *ptr = someAddress;
SomeType &ref = *ptr;
assert(&ref == ptr);

Consider this program:

#include <cassert>
struct SomeType { void* operator&() { return 0; } };
int main() {
  SomeType *ptr = new SomeType;
  SomeType &ref = *ptr;
  assert(&ref == ptr);
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308