In this episode, we will discuss a new data type, pointers. In this first video, we will go over the general aspects of pointers and how they are related to references. Then, in the next video, we will present in detail how to use pointers. Pointers have the unfortunate reputation of being considered hard, whereas this is really not the case. Actually, you already use pointers in everyday life, perhaps without even knowing it. For example, when you are interested in a web site, what do you do? You will certainly not copy the entire website onto your computer, will you? You do not copy the site. Instead, in your browser, you have a bookmark, a link which will point to the web site. This web site still exists independently of this. Well, a pointer is nothing more complicated than a bookmark in your web browser. So, in a program, whenever you want to make a link, a reference to an object which exists elsewhere, -- not a website this time, but a variable for example -- in your program, you will use a pointer or a reference. Any time you want to reference, to indicate, to make a link to another variable in your program. Pointers -- I am talking about pointers in the generic sense, that is, meaning both real pointers and references, we will distinguish them later. So pointers, in a program, serve essentially three purposes. First, they can serve as a reference, exactly like the example of a bookmark for a website. Here is an object drawn in red, which I will call a. Say, you want to reference it in several places of your program. For example, in the case of the website, a represents a website in your browser. In your browser, you have a link to this site, and in one of your friends' browser, there is another link to this site, and in a third browser belonging to someone else, there is also a link to this site. All of these are pointers, they refer to the object that we want to reference. This is the first use case of these pointers. The second use case of pointers is what I call using pointers for genericity. The drawing is kind of symmetric to the drawing in the reference case. Here, you have several objects which you don't necessarily know in the beginning, so for example an object a, an object b and an object c, and you want to describe these different objects in a generic way, at a given moment in your program. In this case, you will also use a pointer. The pointer will, at a certain moment, point for example to object a. At another moment, it can point for example to object b. Finally, at another point in your program, the same pointer can point to object c. Here's an everyday example of this: you have your friend's number, and you store it in an addressbook. At that moment, your friend has a certain number, but then, he changes operators, changing phone numbers. In your address book, you will change your friend's phone number to the new number. This is what I call using pointers for genericity. We have a generic pointer which describes one or another, or a third object, at different times in the program's execution. The third use case for pointers is a little technical. It relates to what is referred to technically as scope and lifespan. Scope, I will remind you, is the set of lines of code in which an object is defined. Lifespan is the time during which an object exists in memory during the execution of the program. Let's illustrate once again with a small drawing. If I represent, on the horizontal axis, the time during which the program is executed, then at a certain time, the program will execute a code fragment contained between curly braces. If, in this code fragment, I had for example declared an integer-type variable, named a, then the lifespan of this variable would be equal to the execution time of its scope. Its scope, remember, is the block in which it was defined. And if I look at the execution of the program with respect to time, the lifespan of this variable is thus equal to the execution time of its scope. The compiler will destroy this variable as soon as it doesn't need it anymore, i.e as soon as its scope is finished. Well, if you want to increase the lifespan of an object, -- we will see how to do it exactly later -- if you want an object to be available in memory from this moment here, but have a lifespan greater than its scope, then you will use what we call dynamic memory allocation. This is the third use case of pointers. So, to sum up, pointers are used essentially for three things. Either an object can be referenced from several places using pointers, or one single pointer will point to different objects at different times, or we will want to manage memory ourselves, to decide what lifespan to give these objects in memory, using what we call dynamic memory allocation. These three ways of using pointers are not exhaustive, and we can absolutely combine several, but what counts is to always keep in mind the reason, or the reasons, for which you want to use a pointer in your program. In C++, and especially since the new C++ 2011 standard, there are in fact three types of "pointers". There are references, which aren't strictly speaking real pointers and are actually fundamentally quite different from real pointers, in the sense that they are completely managed by the compiler, and that they are very secure objects which are not manipulated by the programmer himself but by the compiler. Then, we have, since the new C++ 2011 standard, intelligent pointers, so-called <i>smart pointers</i>. And finally we have the old pointers inherited from the C language, the ancestor of C++, which are very powerful, all-around pointers. These are the ones we simply call pointers when we don't explicitly specify which sort of "pointer" we are talking about. Before going into detail about each of these new types, the question we can ask ourselves is which ones to use in which situation. The general rule is to use references whenever we can and to use pointers when we must. Let's go over each of the use cases for pointers again. In case number one, where we want to use pointers as references, then we should of course use real references instead of pointers. We could also use C-style pointers, since these pointers are universal and can be used for everything. But as a beginning programmer, if you have the choice and you are in the use case of a reference, then simply use references, which we will go over in the next video. If you are in use case number two, that is, you want to use a generic pointer to describe several different objects which you don't necessarily already know of, then, what you should use is a C-style pointer. But there is another way of referring to these objects, which we want to describe generically. I called them a, b and c in the last example. So, suppose that these objects already exist, and suppose that they have been stored in an array. At this point, you don't even need a pointer anymore, you can simply use the indices, the positions in the array, to describe them in a generic way. So for example, using size_t i to refer to them. If i equals zero then it will describe a; if i equals 1, it will describe b; if i equals 2, it will describe c. So, it can be useful to keep in mind this way of doing things, which simply requires an extra data structure, an array in this case. If these objects already exist when we create the array, or if we add them as we go into a dynamic array using push_backs and pop_backs, then we can also refer to them generically by using an extra abstraction which is the index of the element in the array rather than the element itself. And if you really want to use pointers, so, if you are in use case number two, then you will need to use C-style pointers. In use case number three, dynamic memory allocation, then you will of course have to use pointers -- that is what they are meant for -- and in this case, I suggest you use C++ 2011 and smart pointers. Specifically, I suggest you use unique_ptr, which we will go over in a subsequent video. This concludes the different use cases of all sorts of "pointers". The following videos will go into detail about references, C-style pointers and finally smart pointers described by the C++ 2011 standard.