We have seen references in detail.
Now, let's pass to real pointers.
The main difference is that a pointer
is a variable which actually exists in memory.
It is not just a label for a variable which already exists,
it is a new variable.
To understand what pointers are, one must first know that variables,
generally, are identified by an address in memory.
Suppose, for example, that I have a variable x which exists in memory
and which contains something, anything, and that I look
at memory like this as I go along.
At a certain place, the value of x
will be written to memory in binary form,
and at another moment it will be something else, some other variables.
Well, this variable x is identified by its address,
the place in memory where it begins.
So, we can take the address in memory of a variable.
And a pointer is just that, precisely,
it is a variable which contains the address of another object in memory.
A pointer is simply a variable which contains the address of another variable.
It is a sort of variable of variables, just like a bookmark
for a website, which I took as an example in the introduction,
is the address of a site which exists elsewhere.
And so this way,
references and pointers are fundamentally different.
A reference is not a variable which contains an address,
it is just another label.
There is a big difference between having another label on the same drawer
and another drawer which contains the address of something.
A pointer thus has an extra level, a level of abstraction.
A pointer is really like a book from an address book.
A page on which we could write only one address at a time.
And I would now like to use this analogy
to illustrate a whole bunch of practical applications
for pointers in programs.
These different cases will be illustrated with C++ syntax
that you have not encountered yet and that will be presented
in the next video
which presents the more practical, technical aspects.
Still, we left them here
in case you might want to review this video
or review the slides once you will have learnt about this syntax.
It could be useful.
Let's start with the first case: declaring a pointer.
I told you that a pointer was a variable
and so we can declare a pointer.
What does it mean to declare a pointer?
It just means that we are adding a page to our address book.
But careful, adding a new page
does not mean that there is an address written on it.
Adding a new page is just adding a new page
without specifying what will be written on it.
There will likely be something written on it, but we don't know how to interpret it.
Second point: assigning a pointer.
What does it mean to assign a pointer?
It means giving an address,
writing an address on the page of our address book.
We sometimes speak of allocating a pointer.
What does it mean to allocate a pointer?
Allocating a pointer means, to go buy some land somewhere.
It is allocating memory and building a house in it.
That is, putting a value there and getting back the address of this house
to copy it onto the page of our address book.
That's what allocating a pointer means.
But note that here -- this is a common mistake --
the pointer is neither the land nor the house.
Those are two completely different things.
The pointer is just the page of the address book
which contains the address of the house.
It is not the house itself.
In other words, the pointer does not equal the value pointed by it.
Those are two completely different things.
In the case of the page of an address book
which contains the address of the land that we bought, that we allocated,
freeing a pointer simply means
that we authorize the memory to re-use this area,
that this area is no longer our property
and that it can be re-used freely somewhere else.
And so this area is no longer our property.
So careful, freeing a pointer is actually a misnomer,
it actually means freeing the land it points to,
it does not mean that we have erased the page of our address book.
The address is still there, but it is a common mistake
to reuse the address of a piece of land which does not belong to us anymore.
This is why, later on,
we will encourage you to erase the page of the address book
once you have freed the pointer.
Copying a pointer is what you do
when you give an address to someone you know.
When you give an address to friends,
they copy the address that you give them into a page of an address book.
They will not copy the house whose address it is.
You do not give them your house.
You give them only the address, you copy the address.
That's what it means to copy a pointer:
it is an exchange of addresses.
And finally, erasing or cancelling a pointer
simply means that you are erasing the page of your address book.
Note that if this address was the address of a piece of land
that we had allocated in memory,
erasing the pointer would not free this land.
The land is still occupied, it is still used.
So first, we have to destroy the pointer before erasing it.
There, I hope this analogy will help you to use pointers
more easily, to understand what is going on.
The next video will show you
how to apply this practically
by going into detail on how to use pointers.