Language: EN


What is a Reference

Today we are going to talk about the concept of REFERENCE in programming. It is a very important concept, and very simple if we understand it correctly.

However, it is one of the topics that most confuse programmers, even those who have been programming for a long time. It’s not click bait, I assure you it’s true. The concept of REFERENCE is poorly understood.

The use of REFERENCES has important implications in aspects such as constant / immutable, value types / reference types, stack, heap, efficiency… and a hundred thousand more things. So it is convenient that you try to understand it well.

In addition, it is a concept that applies to all programming languages, whether you know it or not. While the most modern ones take care of hiding part of the difficulty of their management, in others such as for example C++ or, Java it is more visible.

So let’s get down to business. What is a REFERENCE? Let’s see what the dictionary says:

Reference is a relationship established between an expression and that to which it alludes

Okaaaay… we are as we were, thank you very much dictionary. So it’s better to see it with a practical case.

Imagine you have a book. At the end of the book, you want to add a bibliography to another book. Obviously you don’t copy the entire book you want to mention, you simply add a review. Or in other words, you reference other books 😉.


That is, a REFERENCE is an element that serves as a link for us to go to other information. But it does NOT contain the information, the information is elsewhere. The reference is only the link.

I managed to finish the introduction without saying that a REFERENCE is something that refers to something! Yay for me! 👏

References in computing

In computing we have a very everyday and very simple example, which you use every day, and that will be very useful for us to explain what a reference is. The shortcuts to files and folders.

You have on your hard drive your favorite movie. The Dark Knight, the Avengers, Frozen. Or whatever, I don’t care. But it’s on your hard drive, with the director’s comments and 4k resolution, taking up its 20GB.


Your movie

Now you have a shortcut on your desktop. When you double click, the movie opens for you. But the shortcut is not the movie. It is only a link that takes you to the real movie.


A shortcut to the movie

In fact, you can have different shortcuts that lead you to the same movie. There is only one movie, but you have different ways to access it.


Several shortcuts to the same movie

So this is a referencing mechanism. In this case, of your Avatar movie. Or Interstellar, Toy Story 3. Or whatever, we still don’t care.

Now the typical error of a person starting in computing. “Luis, I’ve sent you 218 movies in this email”. And you think … “218 movies? How is that going to fit in an email? And did it take him 2 seconds to send it?”

Obviously NOT. Your newbie friend has just sent you only the shortcuts 🤦‍♂️. The shortcuts weigh very little, because they do not contain the real movie. So he can send you thousands. But they are useless to you, because the information is not on your computer.

References in programming

In programming, REFERENCES are variables that do not contain data, but contain a link to the data. Just like the shortcut did not have the movie data, it was just an access to the data.

Why did someone come up with such a mess? Well, as you can imagine, not by choice. Without REFERENCES, computers could not function.

Mainly for two reasons:

Speed when copying

Copying or moving REFERENCES is much faster than working with the actual data. Remember your newbie friend who copied only the shortcuts, and not the movies. It is much faster, because the references are very small.

If two parts of a program need to work on the same data, they can exchange references. Otherwise, each part would have to make its own copy, modify it, and return it.

REFERENCES allow programs to greatly improve their speed. In fact, literally without references we wouldn’t have computers.

Dynamic memory

The other reason for using REFERENCES is the need to use dynamic memory. Your program does not always know what data it needs. For example, it has to save numbers entered by the user… but we don’t know how many numbers the user is going to enter.

Except for the simplest programs, most will need to allocate memory during their execution. For that they launch a process we call allocation. The operating system and the Allocator are involved in this process.

We won’t go into detail, but at the end of the process we will have our space reserved in memory. But we have no idea where that space is, they have to tell us where. So they are required to give us a REFERENCE.


Your variable is in room 0x23

It’s like when we go to reserve a room in a hotel. We go to the reception, ask for a room, and when they finish they have to tell us what number it is.

What is a pointer Advanced

Many languages have the concept of POINTER. Feared by some, loved by others, and overrated by almost everyone, the truth is that the term does not leave anyone indifferent.

So what is a pointer? A POINTER is one of the simplest mechanisms for referencing. It is not the simplest, or the most difficult, or the most powerful. It is simply one of the simplest.

Basically, a POINTER is a variable that contains a memory address where there is a value. Pointers are called that because they “point” to a memory location.

Think of a laser pointer, the one you use in a presentation with a whiteboard. What is a laser pointer? Something that serves to point. Well, the same, only this is a pointer to memory, something that points to a location in memory.

So, POINTERS are a mechanism for making REFERENCES. All pointers are references, but not all references are pointers. There are other mechanisms, some better and some worse.

Internally, probably, all reference systems use pointers to a greater or lesser extent. Although in the end, this is saying nothing. Yes, and everyone will use memory addresses and bytes, and things, so what?

So I’m going to demystify the concept of POINTER. It is much more important that you understand the abstract concept of REFERENCE. Pointers are a particular case of implementing a referencing system.