I can't find much literature on references to pointers (probably not correct term), so I seek the advice of members on this subject.
1 2
int* ptr1 = nullptr;
int*& ptref = ptr1;
Is ptref just a pointer that constantly references ptr1? Can the memory addresses or values at the memory addresses of these pointers ever differ from each other?
A reference is an alias. In your example, the compiler does not generate a memory location for a new pointer. ptr1 and ptref are two names for the same thing.
So no, the memory addresses or values pointed to will never differ.
Thanks AA. Is there a more adequate terminology than 'reference to pointer' that we should be using?... since ptref is actually a pointer that references another pointer.
> I can't find much literature on references to pointers
It is quite rare to see references to pointers (or pointers to pointers) in idiomatic C++ code.
It could typically be used in legacy C-style code (written in C++) to manipulate linked structures.
(A C hack attempting C++ would probably continue to use pointers to pointers.)
#include <iostream>
struct node { int value ; node* next ; };
void push_front( node*& first, int v ) {
auto pnode = new node { v, first } ;
first = pnode ;
}
int main() {
node* head = nullptr ;
for( int i = 0 ; i < 10 ; ++i ) push_front( head, i ) ;
for( node* p = head ; p ; p = p->next ) std::cout << p->value << ' ' ;
std::cout << '\n' ;
// clean up elided for brevity
}
The code in the snippet you have provided differs from mine. You are explaining references and pointers which I understand. My example features a pointer that references another pointer (working on the terminology) which I am just seeing for the first time.
A reference is a synonym for another object. It's an alias. It's just another name for some object that already exists.
A reference is NOT a pointer Sometimes it is implemented using a pointer, but that's entirely the compiler's business. From your perspective it's just a synonym.
Is ptref just a pointer that constantly references ptr1?
I wouldn’t say ‘just’, but it is somewhat very similar: The C++ Programming Language, Fourth Edition, Part II: Basic Facilities, 7.7.1. Lvalue References:
Bjarne Stroustrup wrote:
The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. It doesn’t do much harm to think about references that way, as long as one remembers that a reference isn’t an object that can be manipulated the way a pointer is:
[omissis]
In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run time.
Compared to pointers, references have a much simpler syntax and they won’t set traps for you in the code:
(same source as above, few pages before):
• You access a reference with exactly the same syntax as the name of an object.
• A reference always refers to the object to which it was initialized.
• There is no “null reference,” and we may assume that a reference refers to an object (§7.7.4).
- - -
Just to have fun, can you guess, without executing, which of the following codes will run and which will fail?
Just to asses how much you love the pointer syntax :-) Le me confess I hate them.
I didn't say it was a pointer to a pointer. I said it was a pointer that references another pointer
When talking about pointers "references" can be another way of saying "points to". When I hear "a pointer that references another pointer" it makes me think of a pointer that points to a pointer (e.g. int**).
Even the C standard uses the word "reference" when describing pointers.
The C standard wrote:
A pointer type describes an object whose value provides a reference to an entity of the referenced type.
calioranged wrote:
1 2
int* ptr1 = nullptr;
int (*) & ptref = ptr1;
ptref is a pointer ^ (*)
1 2
int* ptr1 = nullptr;
int* (&) ptref = ptr1;
that references ^ (&)
1 2
int* ptr1 = nullptr;
int*& (ptref = ptr1);
another pointer ^ (ptref = ptr1)
You should read types from right-to-left.
int* const is a constpointer to an int.
constint**const* is a pointer to a constpointer to a pointer to a const int.
I said it was a pointer that references another pointer
Ok. It's NOT a pointer that references another pointer.
The implementation might choose to use a pointer, or might choose to use nothing at all. To think of a reference as physically existing, in a particular program actaully compiled, in the guise of a pointer that references another pointer is correct sometimes for a given situation in a given C++ implementation (useless to you, but some people do like to know how their compilers do their implementations of things), but in the C++ language, wrong.
1 2
int* ptr1 = nullptr;
int* & ptref = ptr1;
How many pointers are there here? One. How can ptref be a pointer that references another pointer? There is only one pointer here. There is no "another" pointer. In the C++ language, there is one and only one pointer here.
I said it was a pointer that references another pointer
What other pointer? There is only one pointer here.
Here ref is a reference to an int. No pointers whatsoever.
Yes I understand that in the example you have given, ref is just a reference to the x variable. But in the example I provided, ptref is a reference to the ptr1 pointer.