Pointer vs Reference in C++: The Final Guide

A pointer in C++ is a variable that holds the memory address of another variable.

A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable. Hence, a refe…


This content originally appeared on DEV Community and was authored by ZigRazor

A pointer in C++ is a variable that holds the memory address of another variable.

A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable. Hence, a reference is similar to a const pointer (not to be confused with a pointer to a constant value!).

Key differences

Pointer

  • A pointer can be initialized to any value anytime after it is declared.
int a = 5;
// some code
int *p = &a;
  • A pointer can be assigned to point to a NULL value.

  • Pointers need to be dereferenced with a *.

  • A pointer can be changed to point to any variable of the same type.

Example:

int a = 5;
int *p;
p = &a;
int b = 6;
p = &b;

Reference

  • A reference must be initialized when it is declared.
int a = 5;
int &ref = a;
  • References cannot be NULL.

  • References can be used ,simply, by name.

  • Once a reference is initialized to a variable, it cannot be changed to refer to a variable object.

Other Differences

Memory Details

A pointer has its own memory address and size on the stack whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack.

Arithmetic operations

Various arithmetic operations can be performed on pointers whereas there is no such thing called Reference Arithmetic.(but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 6).)

When use Pointer and when use Reference

The performances are exactly the same, as references are implemented internally as pointers. But still you can keep some points in your mind to decide when to use what :

Use references :

  • In function parameters and return types. Use pointers:
  • Use pointers if pointer arithmetic or passing NULL-pointer is needed. For example for arrays (Note that array access is implemented using pointer arithmetic).
  • To implement data structures like linked list, tree, etc and their algorithms because to point different cell, we have to use the concept of pointers.

As said in a C++ official FAQ :

Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don’t need “reseating”. This usually means that references are most >useful in a class’s public interface. References typically appear >on the skin of an object, and pointers on the inside.

The exception to the above is where a function’s parameter or return value needs a “sentinel” reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the nullptr value this special significance (references must always alias objects, not a dereferenced null pointer).

Note: Old line C programmers sometimes don’t like references since they provide reference semantics that isn’t explicit in the caller’s code. After some C++ experience, however, one quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g., programmers should write code in the language of the problem rather than the language of the machine.

Other Resources

For More "The Final Guide" see the Index Page


This content originally appeared on DEV Community and was authored by ZigRazor


Print Share Comment Cite Upload Translate Updates
APA

ZigRazor | Sciencx (2021-09-01T06:42:42+00:00) Pointer vs Reference in C++: The Final Guide. Retrieved from https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/

MLA
" » Pointer vs Reference in C++: The Final Guide." ZigRazor | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/
HARVARD
ZigRazor | Sciencx Wednesday September 1, 2021 » Pointer vs Reference in C++: The Final Guide., viewed ,<https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/>
VANCOUVER
ZigRazor | Sciencx - » Pointer vs Reference in C++: The Final Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/
CHICAGO
" » Pointer vs Reference in C++: The Final Guide." ZigRazor | Sciencx - Accessed . https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/
IEEE
" » Pointer vs Reference in C++: The Final Guide." ZigRazor | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/. [Accessed: ]
rf:citation
» Pointer vs Reference in C++: The Final Guide | ZigRazor | Sciencx | https://www.scien.cx/2021/09/01/pointer-vs-reference-in-c-the-final-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.