logo
Send your CVContact

/

logo
Background image

Blog

Reference vs. Pointer

Wojciech Stróżyński 23.10.2018

The case seems trivial. Everyone knows what the pointer is used for and what the reference is for. But are we sure we know the differences between them and know where to use them? Let’s find out.

Let’s start with the differences between the tw

CechaWskaźnikReferencja
Brak przypisaniaMożliwy - nullptrNiemożliwy. Referencja musi odnosić się do konkretnego obiektu
Ponowne przypisanieWskaźnik przyjmować różne wartości, jeśli tylko nie został zainicjowany jako const
int * const ptr_i = &i
Raz przypisana referencja nie może ulec zmianie - zawsze odnosi się do tego samego obiektu
&zmiennaZwraca adres tego wskaźnikaZwraca do adres to obiektu, do którego odnosi się referencja
sizeof(zmienna)Zwraca rozmiar wskaźnikaZwraca rozmiar obiektu do którego się odnosi
o:

Despite the fact that the sizeof() called with the reference as an argument returns the size of the object to which the reference refers, in practice most compilers (if not all) implement the reference as a pointer. Thus, the testimonial physically in memory takes up exactly the same amount of space as the pointer… or how much? You will find out how much the pointer weighs in a separate article. For now, however, let’s go back to the topic of the differences between the reference and the pointer.

Therefore, if a reference is implemented as a physical variable, it is important to bear in mind that the ref_i in the following example

int &ref_i = i;
int *ptr_i = &i;

it will not be completely removed as a result of compiler optimization, and the ref_i variable will physically occupy exactly the same amount of memory space as the ptr_i.
I mean, IT MAY not be removed, because what the compiler does with our code may surprise us a lot. In practice, you may find that our code will only return values from the main function at all.

When the reference, and when the pointer?

The answer to this question is not clear, because these two concepts can be used interchangeably in most cases. But there is one general rule – use testimonials whenever possible. Only if you can’t, use a pointer. Where can’t you?

    • Object is optional – need to use nullptr. Alternatively, we can use boost::optional (introduced to the standard from C++17).
    • You need to use a library written in C (or C-style).
    • Storing references on objects in STL containers – this is impossible because objects stored in the wi-sector must be „assignable”. Const int is also out of the question.You can use std::reference_wrapper, or a regular pointer, to work around the problem.
    • You need to operate on pointers to memory – work close to the equipment. However, it is recommended that you limit their use as much as possible and create a good abstraction that no longer exposes a pure pointer API outside.
    • In Herb Sutter’s words, if we need to delegate an object held in unique_ptr or shared_ptr but we don’t want to give it away, we should pass it on as an pointer. Thus, we maintain a convention in which a pointer can be null or point to an object of a given type. Developers too often refer to the entire smart pointer here. However, this is not recommended. A function signature that contains a reference on a smart pointer is reserved for a function that modifies the smart pointer itself, not what that smart pointer stores.
      
      

      For a good start, however, you should consider whether passing a pointer to an object as a function argument is necessary in our case at all. Maybe it can be designed differently?

Anticipating comments

You can create a reference that is empty. This is done as:

int *p = NULL;
int &r = *p;
r = 1;

In the example above, however, deferening a NULL pointer in itself is an undefined behavior. Initiating a reference with something that causes undefined behavior to continue is wrong. The above code, although it can compile without error, is absolutely incorrect, and for arguing with it anything should be punished with a glass … what am I writing?! A glass of pu-erha!

logo
ContactSend CV