Pointers in C++ are variables that store memory addresses and allow reassignment, dynamic allocation, and nullability, and references in C++ are aliases to variables, they are simpler, safer, and cannot be reassigned or nullified. In this article, let’s learn in detail the differences between pointers and references.
Table of Contents:
What is the Pointer Variable?
In C++, the pointer is simply a variable that stores the memory address of another variable. Based on the pointer type, it determines the type of variable, points to the data type of the same type, and is declared using the (*) operator.
Syntax:
type* pointer_name;
The syntax for the pointers consists of two components:
- Pointer Declaration: The declaration of the pointer is used by the (*) operator and the & operator to access the address of the variable.
- Dereferencing: To modify or access the variable of the pointer, we use the (*) operator.
Example :
Output:
Here, the num declares the variable with 7, the ptr refers to the pointer variable that holds the memory address of the num, and int* is used to access the value stored at the memory address.
What is the Reference Variable?
In C++, the reference variable is another name for an existing variable. The reference is implemented to store the address, which is similar to the pointer. Reference is declared as ampersand (&) next to type.
Syntax:
type& reference_name = existing_variable;
(&) indicates the reference, while type declares the datatype(int, float, string) of the variable being referenced, reference_name refers to the name of the reference, and existing_variable is the variable that mentions the variable that the reference refers to.
Example:
Output:
Here, num is an integer variable with 7, ref declares the memory location of the variable. In the above code, if we change the ref value, it also modifies the value of num.
Initialization of Pointer and Reference
1. Initialization of Pointer Variable
If a pointer is set to nullptr, it doesn’t need to point to a valid object at the time of declaration, but it’s important to initialize it before using it.
Example:
In the above code, the ptr1 is assigned to nullptr, i.e., it doesn’t point to any memory address; on the other hand, the ptr2 points to the memory address of the variable a.
2. Initialization of Reference Variable
A reference is always initialized when the reference variable is declared and can refer to a different variable at a later time.
Example:
In this code, the ref refers to a; if the value of ref is changed, then the value of a is also changed.
Re-Assigning the Pointer and Reference Variable
1. Reassigning the Pointer
A pointer has the chance of reassigning to another variable or nullptr during the execution of the program.
Example:
In this code, at first, ptr is assigned to a, then, the ptr is again assigned to b. Now the final ptr is pointing to b instead of a.
2. Reassigning the Reference
Once the reference is assigned to the variable, it cannot be reassigned to another variable.
Example:
Here, ref is a reference to the memory location of a, and therefore, after reassigning it with b, the value of a becomes 20, but ref remains to refer to a, but its value is already updated to 20.
Dereferencing the Pointer and Reference
1. Dereferencing the Pointer
A pointer has to be dereferenced using the operator (*) to access or modify the value stored at the memory address it points to.
Example:
Here, the pointer ptr holds the address of a; by dereferencing the pointer using *ptr, we can modify the stored address of a. In this case, after the dereferencing, the variable changes from 10 to 20.
2. Dereferencing the Reference
A reference is dereferenced automatically, so there is no need to use any operator explicitly to access the value it refers to. You can use the reference like a normal variable.
Example:
In this code, the ref refers to a. At first, the a = 10, but when ref = 20 is executed after ref refers to a, the a value changes to 20 since ref is an alias for a.
Nullability in Pointer and Reference
1. Nullability in Pointer
A pointer can be null, it holds the nullptr during the execution
Example:
In the above example, the pointer, ptr, points to the null pointer, i.e., it doesn’t point to any valid memory address because it is not pointing to any variable or object.
2. Nullability in Reference
A reference cannot be null since it has to be bound to a valid object during initialization. Once a reference is bound to a variable, it always points to that variable and cannot be made to point to another object. A reference cannot be assigned as ‘nullptr’ since it always has to have a valid target.
Example:
In the above code, the ref always refers to the variable a; if the value of ref is changed, then the value of a also changes. But make sure to always assign a value to the ref.
Memory Allocation in Pointer and Reference
1. Memory Allocation in Pointer
We use pointers in C++ to allocate memory dynamically by using new and delete. You can also describe arrays or objects in dynamic memory with pointers.
Example:
Here, int* ptr = new int(10) dynamically allocates the memory of an integer with 10, and delete ptr; deletes the allocated memory to avoid the memory leaks.
2. Memory Allocation in Reference
Dynamic memory allocation cannot be handled with references. But they are just aliases for another variable.
Example:
The first line dynamically allocates an integer with the value ‘10’ and stores its address in ‘ptr’. The second line declares ‘ref’ as a reference to the allocated integer, so ‘ref’ is an alias for ‘*ptr’. Modifications to ref will take place in the same memory as ‘ptr’.
Function Usage in Pointer and Reference
1. Function Usage in Pointer
Pointers are very useful when you want to assign data structures like arrays and linked lists. Pointers are also useful when handling the dynamic memory allocation.
Example:
In the above code, the pointer ptr points to the function foo(int* ptr), which modifies the variable value. The line *ptr sets the value as 20.
2. Function Usage in Reference
References are often used in functions when they are assigned as pass by value and pass by reference without the need of pointers.
Example:
Here, the ref refers to foo, so whenever the inside value of the function changes, the original variable also changes.
Pointer Arithmetic in Pointer and Reference
Pointer Arithmetic in Pointer
A pointer is a variable holding the memory address of another variable. It can be used for arithmetic operations such as increment (ptr++), decrement (ptr–), addition (ptr + n), and subtraction (ptr – n).
Example:
Output:
In the above code, the first element got initialized with an integer array and pointer. It points to the first value and increments the ptr to the next element, and lastly prints the second value.
Pointer Arithmetic in Reference
A reference is an alias for a variable that already exists and cannot be used for arithmetic operations. It cannot be modified to point to another variable once it is initialized.
Example:
Output:
The above code initializes the integer x and a reference ref referring to the x, after modification, the x value changes to 20.
Function Parameter Differences in Pointer and References
Function Parameter in Pointer
In C++, the pointer parameter(int* ptr) allows the changes of the original variable and for reassignment. Pointer parameter accept the null-ptr.
Example:
Output:
The above code passes the address of x to modifyPointer, by using the pointer the value of x modifies to 20. The modified value is then printed as 20.
Function Parameter in Reference
As we discussed above, a reference is an alias for a variable that always refers to the same variable and is never allowed to assign the nullptr.
Example:
Output:
The above code passes the reference of x to modifyPointer, which mpdifies the value of x to 30. The modified value is printed as 30.
Use Cases for Pointers and References
- const Pointers & References: const pointers restrict address or value modification, whereas const references disallow change.
- Delete in Pointers & Reference: delete in Pointers is used to deallocate dynamically allocated memory, whereas references do not require delete.
- Smart Pointers & Reference: unique_ptr, shared_ptr, and weak_ptr automatically handle memory, unlike normal pointers and references.
Key Differences of Pointer and Reference
Function |
Pointer |
Reference |
Initialization | Can be assigned to nullptr | Must be assigned the variable other than null |
Re-assignment | Can be reassigned to any variable | Re-assignment is not possible |
Dereferencing | Must include * operator | Automatically dereferenced |
Nullability | Can be null | Cannot be null |
Memory Allocation | Holds the variable address | Cannot hold the dynamic memory allocation |
Function usage | Can be used in data structures | Can be used as pass by value ad pass by reference |
Pointer Arithmetic | Supports arithmetic operations like ptr++, ptr–, ptr + n | Not allowed |
Function Parameter Differences | Can modify the original variable by passing its address and can be reassigned inside the function | Modifies the original variable directly but cannot be reassigned |
Conclusion
In C++, the pointers are flexible, allowing reassignment and dynamic memory allocation, but pointers require careful handling to avoid memory leaks. References are simpler, safer, and cannot be reassigned or nullified. The choice of pointers and references depends on the task.
FAQs
1. What is a pointer in C++?
In C++, the pointer is a variable that stores the memory address of another variable.
2. What is a reference in C++?
The reference is similar to the pointer, the reference is an alias of an existing variable.
3. Can a pointer be null?
Indeed, yes, a pointer can be assigned to nullptr.
4. Can a reference be null?
No, the reference cannot be assigned as null; the reference should always be assigned as a valid variable.
5. Can you change the value that the reference refers to?
No, once the reference is assigned to the value, it cannot be reassigned.