pointers vs references in cpp

Pointers vs References in C++

In C++  pointer and reference, both are used to access the object indirectly. So it is important to know when to use the pointer and when to use reference. This article explains the difference between pointer and reference (Pointers vs References) in C++. Here, I will discuss some important differences between the reference and pointer which help you to make the decision when to use the reference and when to use the pointer. Also, I will use some code snippets that help you to understand why references and pointers are different from each other.

You can also see the below-mentioned article,

 

Pointers vs References  in C++:

If we left some points, then the references are similar to the pointers. Below I have selected some points that are differentiated pointer and reference in C++ (Pointers vs References):

1. Declaration and initialization:

The syntactic structure of the reference declaration is similar to the pointer declaration. The difference is that while a pointer declaration uses the * operator, a reference declaration uses the & operator. It means pointer name followed by the * (asterisk ) but the reference name followed by & (Address-of operator).

Example,

//create an variable
int data = 6;



//rData (reference) refer to data
int& rData = data;



//pData (pointer) refer to data
int *pData = &data;

     OR
     
int *pData;
pData = &data;

 

2. Initialization with a NULL pointer:

A pointer can point to the null pointer, but a reference can’t refer to NULL.

//reference refer to null
int& rData = NULL; //error



//pointer point to null
int *pData = NULL; // ok

 

3. Rebinding:

You should remember, that once a reference bind with any object, it will not rebind with another object. But it is not true for pointers we can initialize pointers multiple times as per the requirement and it is not necessary to initialize the pointers at the time of the creation.

/****reference****/
int& rData;  //error

int& rData = a; //ok, rData (reference) refer to 'a' 

rData = b; //error



/****Pointer****/
int *pData; // ok

int *pData = &a; // ok, pData point to 'a' 

pData = &b; // ok, pData point to 'b'

 

4. Way of accessing the value:

A reference can be thought of as a name of an object. So we don’t need any extra operator while accessing the value. But with pointers, we need the indirection operator while accessing the value. Let’s see an example code for a better understanding,

#include <iostream>
using namespace std;

int main()
{
    //create an variable
    int data = 6;

    //pointer point to data
    int *pData = &data;

    //rOffData refer to data
    int& rData = data;


    cout << "Value of data, rData and pData" << endl;

    cout << "data = " << data << endl;

    //accessing value by reference
    cout << "rData = " << rData << endl;

    //accessing value by pointer
    cout << "*pData = " << *pData << endl;

    return 0;
}

Output:

Value of data, rData and pData
data1 = 6
rData = 6
*pData = 6

 

 If you want to learn a programming language online, you can check the courses, a free trial is available.

Click to Get your Free Trial

 

5. Failed dynamic_cast:

A failed dynamic_cast doesn’t have the same effect on a pointer and reference:

1. A failed dynamic_cast on a pointer returns a null pointer.
2. A failed dynamic_cast on a reference throws an exception of type std::bad_cast. This makes sense because it can’t return a null reference.

 

6. Storage:

it is unspecified whether or not a reference requires storage. But still, I have seen some examples where a reference shares the same memory address with the original variable but also takes up some space on the stack. Let’s see an example code,

void fun(int& rChangeData)
{
    rChangeData = 10;
}


int main()
{
    int data = 5;

    int& rData = data;

    fun(rData);

    return 0;
}

In the above code, rData should not take space on the main stack, but the reference rChangeData of function will take a place on its stack. It means when calling the function “fun” with rData as an argument, the address of data will be pushed on the stack of functions.

 

7. Safety:

References are quite safer than pointers; it is the reason C++ folk insists to use reference in C++ programming. But it is up to you and your requirement.

Pointers are more powerful than references because they allow rebinding and nullability. Also if you have seen “Spider-Man Movie”, then you know that “great power comes with great responsibilities”, so you always need to worry about a pointer not being null and not changing its pointed target.

But it is also not 100% true that a reference is always safe, you also need to be careful before using a reference.

Let’s see an example, where the reference is invalid and can cause undefined behavior and bad thing is that it would be hard to trace such type of issue.

int* pData;


int& rData = *pData;

 

 

Some other differences between references and pointers:

1. There shall be no references to references but pointer to pointer possible.

2. There shall be no arrays of references but an array of pointers possible.

3. There shall be no pointers to references but pointers to pointers possible.

 

Recommended Post