Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the functional difference between a const pointer (not a pointer to const) and a reference?

I am currently learning C++ from C++ Primer, and it explains how a reference is an alias to another variable name. It also explains how a pointer points to another variable. It states that the difference between a pointer and a reference is that pointers can be reassigned and references can't.

In the following code example, what can I do with the pointer or reference that I can't do with the other?

double pi = 3.14;
double &piRef = pi;
double *const piPnt = π

//both of these examples are valid and do the same thing
piRef = 3.14159;
*piPnt = 3.14159;

//however, if I attempt to reassign what the pointer points to, it is illegal.
//this is the same as with a reference, as a reference can't be reassigned either
double tau = 6.28;
piPnt = τ

I am aware of the internal differences of each (such as that a pointer is an object, a reference isn't). I am interested in how those differences matter to the programmer beyond a slightly different syntax. As such, this is not a duplicate of this question in which the accepted answer only talks about internal differences.

like image 243
john01dav Avatar asked Oct 05 '15 06:10

john01dav


1 Answers

From a functional point of view pointers and references are indeed the same thing... they reference an object and are not a copy of that object.

The only real difference in addition to not being able to rebind a reference is that a pointer can be NULL (i.e. it can point to nothing) while a reference is assumed to always reference an object.

You technically can actually end up with a reference that is referencing no object (e.g. passing *p to a function expecting a reference where p is the null pointer) but this is "undefined behavior".

In other words pointers are more "flexible" than references and this allows the compiler to ignore

  • That a reference can change the object it's referencing
  • That a reference can have no object

And this can in some cases produce faster code (however for the second point de-referencing a null pointer is undefined behavior and not a runtime error; the compiler therefore is not mandated to generate code that does something special in this case: that a pointer can actually point to no object is indeed irrelevant from a code generation point of view because it's in the contract between programmer and compiler that this will never happen).

The "price" to pay for the added flexibility of rebinding and having NULLs is that the syntax is (somewhat gratuitously) more annoying.

If the pointer cannot be reassigned (because the pointer itself is const) then there are no practical differences whatsoever except for the more verbose-but-explicit syntax. This because despite being an object a const-declared pointer cannot be altered even using aliasing.

While from a syntactic point of view you can take the address of the const pointer, cast the address to an address of a non-const pointer and change the value pointed to, such an operation would be undefined behavior and whatever happens (e.g. ignoring the assignment) the compiler is going to be right if taken to court :-)

like image 70
6502 Avatar answered Oct 11 '22 18:10

6502