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
Latest comments (1)
Pointers can also exist in the heap:
You need to be really precise with wording here. A reference has its own address in memory (just like a pointer) that's distinct from the referent's address, but C++ provides no syntax for accessing a reference's address. For example, in:
the reference
_r
insideS
definitely has its own memory with a unique address.References can also exist in the heap: