In this video, we're going to do something slightly different. The entirety of this video is all going to be looking at code and we're going to examine what programs do, kind of puzzly different programs when we use stack memory, heap memory and other types of memory. So the reason we do this is to really get an understanding of how pointers work and really challenge ourselves and making sure that we have a deep understanding of how the code interacts with memory, so that when we see examples like this that happened or have an interview question like this when it happens or quiz question, we'll have no problem. So our very first puzzle, create six variables right off the top. So here we have six variables int i, j, k and p, q, r, where p q and r are pointers. Notice the new keyword is not used at all. So because the new keyword is never found, we know that all of this memory has to be stack memory. So I'm going to go ahead and draw the boxes for i, j, and k as well as p, q, and r, and stick their initial values in it so that we can keep track of it. So, i has the value of two, j has the value of four, k has the value of eight. P is a pointer and points to i, q is a pointer and points to j and r is a pointer that points to k. Now on line 17, we say k is equal to i. So k is going to take on the value that's an i. So k is going to become two. Now we're going to print out i j k dereference p, dereference q and dereference r. So i contains a value of two or k contains value two, de-reference p contains two, dereference q contains four, dereference r contains two. So we expect the first output to be 242 242. Next we see p is equal to q. So p takes on the value of q. The p no longer points to i, but p points to the same place as q, so p is now pointing to j. So i still has the value of two, j still has a value of four, k still has a value of two. The dereference value of p is now four, the dereference value of q is four, the dereference value of r is two. The second set of output we expect to be 242 442. Last thing is the dereference i of q takes on the dereference value of r. The dereference value of q is j, dereference value of r is k. So star q takes on star r, so four takes on two. So now everything should have the value of two. This is what I got when I ran through this code. Let's see what the computer gets. Going into CPP heap puzzles, running make to compile all of our puzzles, running dot-slash puzzle one. First row 242242 exactly what we expect. Then 242442 exactly what we expect and I'll choose for the third row. So we've solved this puzzle and we found that the data we expected is exactly equal to the data we saw on the computer. Awesome. Let's look at another puzzle. Here in the second puzzle, we can see that we have the new keyword. So we know that some of this data is going to be on the heap. So, I'm going to go ahead and have a section of memory that is stack and have a section of memory that is the heap. So on the stack, we have an integer pointer x and that's going to point to the new memory on the heap which is a new integer. We have on the stack int ampersand y. So here we have something we'd never seen before. Int ampersand y is giving us something called a reference variable, and a reference variable is something we're going to dive into a lot more next week but I'm going to give you a little bit of a preview of it. All that reference variable means, is it's going to alias another piece of memory which allows us to give a name to a piece of memory. So y is going to alias the dereference value of x, what that means is we can say that this heap memory address is called y. So now we have int ampersand y (int& y), so reference variable y, is going to alias the memory in x and then we're going to say y is equal to four which allows us to alias the memory exactly. If we ask what the memory address of x is, the memory address of x is equal to the memory address of a value on the stack, so we expect this to be a large number. The contents of x is going to be the pointer itself that points to the heap so we expect the contents of x to be a fairly small number because its value is a pointer to heap memory, and the dereference contents of x we expect to be four because we set the dereference value of x which is the variable y equal to four. Next thing we can ask is, what is the memory address of y? While the memory address of y is this memory in the heap so we expect this to be the same small number we saw up here. The value in containing y is four, and what is the dereference value of a non pointer? We can't do that. In fact, if we uncomment this line of code is going to cause this compiler error. So we see the five lines that we expect to print out, it's going to be a large memory address, followed by small memory address, followed by number four, followed by the small memory address again followed by number four. So we want to verify that our program does the exact same thing and then we're going to see that the sixth line is not going to pronounce and we'll look at the source code, we'll uncomment out and kinda see the area that you're going to get. So let's go ahead and look at this code on the console. Running dot-slash puzzle two, we see a large memory address, followed by small memory address, followed by four, followed by the same small memory address, followed by number four again and you again see there's only five lines. So launching the Visual Studio Code IDE, I'm going to look at puzzle2.cpp, and puzzle2.cpp I'd commented out this line 24. I'm going to uncomment it, save it and let's run make again and see what happens. You'll see that we get an error that says the indirection requires a pointer operand. So we can't take an indirection of an integer itself. It's just int y. Because it's not a pointer, we can't dereference a non pointer. So the only way this code works is had we comment it out that last line of code. Awesome. Let's go ahead and look at a third puzzle. Here on this third puzzle, there's no new concepts. So it's something you should be able to work through yourself. I'd recommend working through this puzzle and then checking if your logic agrees with mine. I'm going to go ahead and get started. So here, I'm going to create my stack memory, this is an int pointer p and a second int pointer and stack that is q, p is then going to be equal to a new int, so new heap memory is going to be pointed to by p, q is going to take on the value of p. So it's going to point to the exact same memory and then the value dereferenced by q is going to be eight so we follow q pointer and put eight here in this memory. Then we're going to see how the dereference value of p. So dereference value of p following the pointer is eight. So the first output we expect to see is eight. Now we're going to say q takes on the value of a new integer. So q no longer points to the memory pointed before but points to a new integer in heap memory, the value of dereference q is nine and then we "cout" the value of dereference p is eight, dereference q is nine. So we expect the answer to this puzzle to be 889. Let's see if puzzle three gets us that result. Running dot-slash puzzle three, we see 889 exactly what we expect to see and if you go by that, we're starting an awesome understanding exactly of how pointers work and how memory interacts. The very last puzzle adds another little bit of syntax is C plus plus. That is the use of arrays inside a heap memory. Let's look at this code. So here we have int star x on the stack, so we have an int star x and this points to somewhere we don't know where yet, the next thing is we have an int size here on the stack and it has the value of three and we say x is equal to the new integer array. So x is now equal to a new integer of size three. So this new int with the array brackets denotes that we are allocating a sequence of integers. So we have, in this case, three integers sequentially in memory stored as an array. We can go ahead and go through this and say i equals zero, i is less than size so it's standard four loop that goes through and says the contents of x of i, so when i is equal to zero, zero plus three is three, when i is one, one plus three is four, when i is two, two plus three is five. So the contents of this array is going to be 345 and then at the very end, because we did a new square brackets, we have do a delete array as well. So the big thing to take away from this is we may see the syntax where we have an array of memory instead of just a single value of heap memory. We're going to see this very rarely but I wanted to go ahead and mention it here in this video because it's something that as you look at other code, you may see lots and lots of people make use of an array inside of heap memory and that use of new int with the array brackets and delete array brackets is a key idea in C plus plus. So if you follow this, you're really a master of C plus plus memory and you're ready to really get started on kind of understand the other part of C plus plus that makes C plus plus awesome, which is the use of classes inside the C plus plus language. So, I'll see you next time.