References to Pointers

Pages: 12
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?
Last edited on
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.

1
2
T  val;
T& ref = val;

The ref is a reference to variable val. An alias. Both represent the same data object. There is only one object. How could it differ from itself?

Can we assume that you understand a reference?


Pointer is a variable, whose value is a memory address. Dereferencing a pointer gives us access to the object, whose address that pointer has.

If val is a pointer and ref is that same pointer, then ...
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.)

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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
}

http://coliru.stacked-crooked.com/a/794e636ef14eea11
keskiverto

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.
Differs? Did I say anything about the T?
1
2
3
using T = int*;
T  val;
T& ref = val;

A reference is a reference no matter what the type T actually is.

Your example has a reference to pointer. A reference, not a pointer.
since ptref is actually a pointer that references another pointer.


NO. That's not what he said. ptref is a reference; ptref is another name for the object known as ptr1. ptref is NOT a pointer to a pointer.
Last edited on
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.
A reference is another name for an already existing variable. An alias.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   int  aVariable = 15;

   int* aPointer = &aVariable;

   int& aReference = aVariable;

   std::cout << "aVariable:  " << aVariable
             << ",\tAddress of aVariable:  " << &aVariable  << "\n\n";

   std::cout << "aReference: " << aReference
             << ",\tAddress of aReference: " << &aReference << "\n\n";

   std::cout << "*aPointer:  " << *aPointer
             << ",\tAddress of aPointer:   " << &aPointer
             << ",\n\t\tAddress in aPointer:   " << aPointer << '\n';
}
aVariable:  15, Address of aVariable:  003CFD34

aReference: 15, Address of aReference: 003CFD34

*aPointer:  15, Address of aPointer:   003CFD38,
                Address in aPointer:   003CFD34

Someone might be known as Harold Rufus Harcourt Blessington III, and have a nickname of Skippy.

Both names for one person.
calioranged wrote:
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.

Version 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int* func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory(main_p);

    delete main_p;
    std::cout << "Goodbye!\n";
}


void getNewMemory(int* func_p)
{

    func_p = new int { 666 };
}


Version 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int** func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory( & main_p );

    delete main_p;
    std::cout << "Goodbye!\n";
}


void getNewMemory(int** func_p)
{
    *func_p = new int { 666 };
}


Version 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int** func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory( & main_p);

    delete main_p;
    std::cout << "Goobye!\n";
}


void getNewMemory(int** func_p)
{
    func_p = new int*;
}


Version 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int** const func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory( & main_p);

    delete main_p;
    std::cout << "Goobye!\n";
}


void getNewMemory(int** const func_p)
{

    func_p = new int*;
}


Version 5:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int** const func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory( & main_p );

    delete main_p;
    std::cout << "Goodbye!\n";
}


void getNewMemory(int** const func_p)
{
    *func_p = new int { 666 };
}


Version 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdint>
#include <iomanip>
#include <iostream>


void getNewMemory(int*& func_p);


int main()
{
    int tmp_val { 13 };
    int* main_p { &tmp_val };

    getNewMemory( main_p );

    delete main_p;
    std::cout << "Goodbye!\n";
}


void getNewMemory(int*& func_p)
{
    func_p = new int { 666 };
}

Repeater said

ptref is NOT a pointer to a pointer.

I didn't say it was a pointer to a pointer. I said it was a pointer that references another pointer:

Brackets are just for notation:

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)
Thanks to all the members who contributed to this topic :)
calioranged wrote:
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 const pointer to an int.

const int**const* is a pointer to a const pointer to a pointer to a const int.

int*& is a reference to a pointer to an int.
Last edited on
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.
Last edited on
Repeater said

Ok. It's NOT a pointer that references another pointer.

Yes sorry you are right. If they both point to the same memory address then they are essentially the same pointer.

Can we say that ptref is a reference to a pointer? Is this the most adequate terminology?
Last edited on
Can we say that ptref is a reference to a pointer? Is this the most adequate terminology?

Yes, that's fine.
Thanks dhayden
At risk of beating a dead horse, just to be sure:

1
2
int x = 7;
int& ref = x;


Here ref is a reference to an int. No pointers whatsoever.
Repeater said

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.

Thanks again.
Last edited on
Pages: 12