 Hey everybody, this is Brian. This is our 18th C++ video. Today, we're going to be discussing the dreaded pointer. The pointer is something that made C++ rather famous, actually. It's manual memory management. What do I mean by that? We're gonna find out. First off, what is a pointer? Think of a pointer like a road sign. This sign is a variable, but it actually points to a different destination. So you have something that exists, but it's actually pointing to something else. For example, this is a city and we're pointing to that city. The city would be the actual variable in memory. Let's find out what I mean by that. You've seen a standard variable before. That's a standard variable that exists on what's called the stack. What is the stack? Well, when your program's executing, the current execution, this code block is the stack. There's also something called the heap or the free store, which is your memory in your computer. You can create a variable in the C++ that exists on the heap out in memory somewhere. And this is how you do that. You put a star in front of the variable name, and then you be sure to say new hint. Now what you've just done is you said we're gonna make a pointer that points to an integer type, and you create that pointer by saying new int. Now, what have we done? I want you to really think about this. We've created a pointer that points to another location. So if we say m number equals 2, what do you think's gonna happen here? Go back to the roadside analogy. Let's just run this. It says, oh, there's an error. It says, cannot convert from int to int star. What is int star mean? int star means int pointer. Cannot convert from an integer to a pointer. They're two different things. It looks like an int, but it's not an int. It's a pointer to an integer. The integer, remember, is out in memory somewhere. To prove that, let's actually do a cout, m number and an nl behind it. And you notice it printed this weird number. What is that? Now your first thought might be, well, we never initialized it, so that's the problem. No, let's initialize it. So now we've initialized it. We've got a pointer number equal to. See, it's got some funky number here still. That's the actual address in memory. So when you pop open the case to your computer and you get those memory sticks somewhere, and one of those sticks is the variable you just made. It's just floating out in memory. And then there is another one that is the value, the number two. So let's actually do this. The star means pointer two. So this is the number. This is that strange number you saw. And then we're going to point to that memory location. So when we do that, suddenly there's our two. So we have a weird number and then a two. And you notice this number changes every time we run this. What's happening? Well, every time you run this, it's grabbing a different part in memory on the heap and saying, okay, allocate memory for this int here. And remember an integer has a size, so it's saying allocate a certain amount of size. I know that sounds really confusing. You're probably not used to manual memory management, but that's what happens. Now we've made a few mistakes here. Do you know what they are? Well, we're saying new int. So out on the heap or the free store, we're creating a new integer placing it in memory and just leaving it there. Well, we haven't deleted it. That's called a memory leak. Every time you run this program, you're creating a new variable. Or I'm sorry, a new value, throwing it out in memory and leaving it there until the program executes. It's called a memory leak. To fix that, you delete that section of memory. And what you're doing is you're now telling it that you're no longer using that. If you don't do that, let's say you had a loop that just generated a bunch of pointers. Eventually you're going to run out of memory and your program's going to crash. That's a very, very difficult thing to debug and it just drives some people bonkers trying to figure it out. So that's a pointer. It's a road sign that points to something else. If you don't understand the difference, you should stop the video, rewind to the beginning and rewatch it because you need to understand that this is the memory location. This points to the memory location, meaning print out whatever values in that memory location. That's how you get the two. To kind of solidify this, we're going to say void test. We want a, whoops, int. We'll call it minum and let's just do some copy and paste magic here and we'll say whoops. It's my gaming mouse. Just hit the wrong button on there. Let's grab some of this real quick. And what we're going to do is we're going to pass this to this function. Now remember the signature of this function or the prototype says that it takes a pointer to. So if you just try to pass star two or sorry star number, what do you think's going to happen? Well, an error message. Why? The infamous cannot convert to pointer It's because you're trying to access or point to that memory location. It's asking for a pointer So you give it the memory address the pointer and then suddenly it works fine You see there's our first one. That's the memory address. There's the value at that address And then we enter the test function. There's minum and there's the address. You notice it's the same address And then the pointer to minum is actually two. That's the value you stored in the address And you see they're exactly the same Confusing topic. I know now. Why do you need this? That's something that always frustrated me about different books. Why do you need pointers to begin with? What use is it? We've already learned you can pass by value and reference. We don't need pointers Well, remember back to the stack and the heap conversation we just had When we get into object oriented programming You will make an object throw it out in memory somewhere And then Your code will jump into a different execution context or a different stack And what happens with that is you need to still reference that variable you threw out in memory I know that sounds really confusing, but we're going to cover this in the next couple tutorials So that is pointers I know it's confusing. I'm sorry It's one of those rites of passages you have to learn to progress with this language C++ is all about memory management And the reason why is because it is incredibly incredibly fast Um, certain languages like java, python, visual basic, c sharp They do um, it's called garbage collection meaning they still do this under the hood, but it's all automated for you The problem with that is they do what's called garbage collection, which means at some point They have part of their framework goes in memory and cleans it up And it's not perfect. Sometimes they'll delete things you're still using And it can slow your program down This is brian. I hope you found this video educational and entertaining and thank you for watching