POINTERS in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys hey what's up guys my name is the Cherno and welcome back to my say class class series so today possibly the most important episode in this entire series we're going to talk about pointers now fortunately pointers are an area that a lot of people struggle with so I just really want to say don't worry you don't over think hip pointers are actually really simple and I also want to mention that today we'll be talking about Raw pointers not smart pointers if you don't know what smart pointers are don't worry about that we'll get it back in the future computers deal with memory memory is everything to a computer if I had to name the single most important thing in programming it would probably be memory when you write an application and you launch it that entire application gets loaded into memory all of the instructions that tell the computer what to do in the code that you've written all of that gets loaded into memory that's how the CPU can actually access your program and start executing its instructions when you create a variable when you load in data from disk everything gets stored in memory there is nothing you can do if you do not have memory and pointers are extremely important for managing and manipulating that memory so what are pointers I'm only going to end up saying this multiple times throughout the course of this video a pointer is an integer a number which stores a memory address that is all that it is I don't wanna get too deep into how memory actually works in a computer I might say that for another video but simply put your memory inside your computer is just like one big blob it's like one big line that's all it is picture one is straight if if your entire city that you live in just had a single straight and there was a start and there was an end and then you had a bunch of houses there were no houses across the road let's just say there's just a street and there's like a row of houses that is what memory is in a computer it's just a linear one-dimensional line and every house on that street is going to have a number an address so bring that metaphor back to computers picture that every house on that street that has an address is a byte it is one byte of data we obviously need a way to be able to address all of those bytes all those houses on our read because for example let's say someone orders something online and wants to have it delivered it needs to be delivered into the right house or maybe someone's sending something they're sending something away from their house either way you need to be able to read and write from memory from those bytes of memory from those houses so a pointer is that address is the address that tells us where that house is where that specific byte of memory is and that's extremely important because pretty much everything we do in our code is going to be reading or writing from and to memory now of course it's perfectly possible for you and C++ to write an application that does not use pointers that's you can do that you don't necessarily need to use pointers however they are an extremely useful tool because as I just mentioned memory is probably the single most important thing you have the single most important resource that your computer can provide to you it's used for everything and being able to have more control over that memory is vital all right anyway to reiterate a pointer is just an address it is an integer which holds a memory address that is all that it is forget types right types have nothing to do with any of that types are just some kind of fiction that we've created to make our lives easier it doesn't matter if you have an int pointer or maybe you have an entity class and you have an entity pointer it doesn't matter types types are completely meaningless right a pointer for all types is just that integer that holds a memory address that's all that it is let's jump in and take a look at some examples ok so here in visual studio about an extremely simple project it's just one source file everything that you see on the screen is the complete code that I've got written so let's go ahead and create a point and we're going to create the purest of pointers because it's going to be something called a void pointer void basically means that it's completely typos remember remember I said that a pointer is just an address it's just an integer which holds an address in memory so it doesn't need a time if we give a pointer a type we're just saying that the data at that address is presumed to be the type that we give it right it doesn't mean anything apart from that it's just something that we can write to make our lives easier syntactically like in actual source code to make our lives easier we can use types with pointers but again a type does not change what a pointer is a pointer is just a memory address it's just an integer so a void pointer means that we do not care right now in our source code what type this actual data is because we just want to really hold an address let's go ahead I'll call it PTR or pointer for short and I'll set it equal to 0 so what is 0 we've given this pointer and memory address of a 0 what does that mean well 0 isn't actually a valid memory address memory addresses do not go all the way down to 0 because 0 is invalid and what that means is that this pointer is not valid being not valid is a perfectly acceptable state for a pointer but all I'm saying here is that 0 is not a valid memory address we can't read from or write to a memory address of C or if we try and do that our program will crash so 0 means null and we can actually also write it like this which is actually just a hash define if you look up what bad null actually as you can see it's just a hash to find for 0 so it's the same as if we write 0 all we can use this a puff-puff keyword called null pointer this was introduced in the Sabre plus 11 so awesome we've made our first pointer it is completely typeless and it has the memory address of 0 completely useless but it's a pointer and it's probably the simplest point you could write let's do something a little bit more useful let's create an integer so I'm going to create a variable I'm going to call it bar and I'm gonna set it equal to a it's going to be an integer now this is just a normal integer but of course every variable we create has a memory address because we need a place to store that variable so what if I want to find out what the memory address of that variable is so where are you in memory I can do that by using the ampersand operator so if I use an ampersand in front of an existing variable like this we're essentially asking this variable hey what is your memory address and then of course we're taking the memory address of that variable and in this case assigning it to a new variable called pointer and that's it we've now got the memory address of our variable and we're storing it in another variable which is a pointer I'm going to put a breakpoint on this line of code and hit f5 to run and debug my application so right now if I take a look at what we've got we've got a variable you can see by helping my mouse over it it has the value of and if I hover my mouse over pointer it has a slightly different value which is presented to us in hexadecimal format but it is as you can see it's still just a number it's an integer so what that point of variable is holding now is the memory address of that van variable and that's all it is that number that you see there a zero fb6 for that is where in memory we are storing our integer variable and in fact what I can do is I can grab this I can copy it I can hit debug Windows memory memory 1 so what this view is showing now is all of the memory inside our application I'm going to paste in that value I'm just going to get rid of that pointer at the beginning there and then I'm gonna hit enter and check this out we are taken to this memory address we know an integer is 4 bytes of data and look at that it has the value 8 we're looking at our computer's memory right now and we can see that add that memory address we have the value 8 because we created that variable and we set its value to 8 all right fantastic so that's really I mean on a basic level that's all there is to it everything else builds from this if you know this much and you're following me in your understanding that's there's no magic behind it that's how points were a pointer is a variable which holds an address an integer a pointer is like any other variable this is with instead of holding your values such as a it's holding a memory address which a memory address is also value it's also an integer now how big that integer is how big that pointer is it depends on a lot of things it could be a 32-bit integer it could be a 64-bit it could be a 16-bit integer doesn't matter right the point is it is an integer and that is all it's ever going to be if I go back to my code here and I change this to be an int pointer I'm not actually changing anything if I run my application again you'll see it's not going to be any different if I go ahead and I copy that value I'll copy the value I'll paste into my memory of you and I'll hit enter you can see it's still set to a I could go back here I could make this a completely different type such as a double of course it's going to give me an error this time because the compiled is going to warn against that I can just cast it to a Dumbledore doesn't matter I can I'm gonna run my code again I'm going to look at the value I'm going to copy the value I'm going to paste it over here and get rid of all this other junk at the end hit enter and look at that it takes me to that place in memory where I'm storing my daughter and there are four bytes there that have the value eight types do not matter it types it useful for manipulation of that memory so if I want to read and write to that memory types can help me out because the compiler will know for example that an integer is supposed to be four bytes so when I try and set a value there it will set four bytes of memory but ultimately types are completely meaningless and in future videos we're going to take a look at that even more deeply but for now just don't worry about bugs all right so let's go back to having our void pointer right why wouldn't you always use the void pointer well suppose that I wanted to get access to my data I've got a pointer which is pointing to that data however now I want to actually write to or read from that data so that variable is holding the value eight I want to change that how do I change that how do I I've got I know where the data is but how do I get access to it that's where the referencing comes in so we've gone from a var to a pointer to var but how do I come back to that bar well you can do that by sticking an asterisk at the front of your pointer so in other words if I write this I'm actually dereferencing that pointer which means that I'm now accessing the data I can either read from or write to that data so I could for example write the value 10 in here however if I try and do that you'll see that I'm actually getting an error why because we've said that point this pointer is a void pointer which means that I mean how how does computer possibly going to write the value 10 into a void point it doesn't know what it is is that 10 a short which is a 2 byte integer is it an int which is a 4 byte integer is it a long long which is an 8 byte integer we don't know how many how many bytes of data should this right right we've just said it's 10 but 10 could mean anything really that's what types come in we need to tell the compiler actually know this is an integer so I expect you to write 4 bytes in please let's just go ahead and change this to int and now of course we've told the compiler this is an integer we've told the compiler the compiler hasn't been like yeah this is correct we've told the compiler if we make mistake if we say this is actually a double we could be trouble if I then output the value of var which as you can note is my original variable you'll see that the value that I get is 10 okay so we've successfully changed it from 8 to 10 and if we go ahead and we set another breakpoint over here and run our program I'm going to come down here and to watch and actually put in that points up and then I'm just going to drag the value all the way up here so that we go to that spot of memory you'll see that at that memory we're storing 8 if i hit f10 to advance my program 1 line you'll see that now is changed to 0 a a of course is hexadecimal for 10 so you can see that by dereferencing a pointer and writing to it I'm basically accessing that data in this case I'm writing to the data at that memory address all right cool so you should now know how pointers work that's really all there is to them a pointer just points to a location in memory some people say that it points to a block of memory although this isn't really accurate because we don't know how big that block of memory is in this case it's four bytes short because we've credit an integer and an integer is 4 bytes of memory so we do have a pointer that is pointing to essentially a blocker for bias however there's nothing in the actual pointer which says how big the memory is when we create arrays and all that it does kind of keep track of the size I'm not going to talk about that but simply put we don't know how big the pointer is we don't know how much data it's holding because it's not holding data right a pointer is just an integer which is a memory address that's it now so far we've been directly creating data on the stack if I create a variable like this we're creating it in the stack portion of our memory I'm going to be talking about the stack and the Cape and all that in a later video there might be like an annotation or something on the screen if I've already made that video so go ahead and check that out if you want more details but what I could do is create a variable on the heap or in other words I could ask our computer hey I want you to allocate some memory for me and I want it to be a certain size so what I could do for now is use a char star now we know we know that a char is one byte right so when I ask something like this like I'll call it a buffer I'll set it equal to a new char of size eight what I'm really asking for is eight bytes of memory this has allocated 8 bytes of memory for us and is returning a pointer to the beginning of that block of memory I could then use a function called mem set which basically fills a block of memory with data that we specify it takes in a pointer which is going to be the pointer to the beginning of the block of memory it's going to take in a value such as zero and then it's going to take an aside so how many bytes should fill in this case we know that we have eight bytes let's run this program I'm going to take this now you'll see that this has the memory address over here now you already know how memory does work and all that so I'm just going to type the actual variable name buffer up here and hit enter and you'll see that look at this we've got eight bytes of memory in a row of course that are all set to zero because that's what we've done with our memset in this case because we use the new keyword and this data is actually heap allocated we should also delete the data when we're done with it and we can do that just by typing and delete we know it's an array we use the array operator to allocator so we should use the delete keyword with the array operator and then delete that buffer if we were being super nice however a program does finish yet anyway the point of this example was just again to reiterate that this is a pointer we've said here that we want to allocate 8 chars of chars one byte so thus we've allocated 8 bytes and we're storing a pointer to the beginning of that data one more thing that I want to mention is the pointers themselves are just variables and those pointers those variables are also stored in memory that's where we can get things like double pointers or triple pointers are basically pointers to pointers how does all that work right well you just go down a level and you're basically just saying I now have a pointer which points to my pointer so I now have a variable which is storing the memory address that's pointing to another variable which is storing a memory address simple right so in this example with my buffer I could create a double pointer right this means it's a pointer to a pointer I'm going to call it PTR and I'm going to set it equal to the memory address of buffer so now this is where it gets interesting I'm just going to move this breakpoint up here because I don't want to be deleting the data just yet let's take a look at this slowly this is the value of pointer I'm gonna take that I'm gonna copy it in here right now you can see that over here we've got b8 f10 - and zero four bytes I know that this pointer is going to be four bytes of memory because I'm running a 32-bit application and in a 32-bit application a memory address is 32 bits more on that in another video now because the endianness of this computer is actually in reverse order so if I paste this in here we actually have to rearrange it so that it says 0 0 0 to f1 and then B 8 at the end like that if I hit enter look at that I'm taken to the memory which stores this buffer of zeros ok that's it pointers to pointers sorted right really simple stuff so that's it I could keep talking about this all day but really I'm just going to come back to saying the pointers are just integers which store memory address that is although they are get that into your head I'm going to be making a lot more videos on this topic and dealing with things like pointer arithmetic and more advanced kind of pointer operations so if you're new to this channel feel free to subscribe as you begin to use pointers and as we begin to use pointers throughout this series you'll get a lot more examples and you'll see just how powerful they are on what we can do with them but this is the gist of how they work don't overthink it they're really simple thanks for watching I hope you enjoyed this video if you did please hit that like button you can also follow me on Twitter and Instagram and if you really enjoyed this video and you want to see more videos like this you can support me on patreon back home for that to show no next time we're gonna be talking about references and I'll see you guys in the next video goodbye [Music] [Music]
Info
Channel: The Cherno
Views: 394,493
Rating: 4.955627 out of 5
Keywords: c++ pointers, pointers, c++, programming, game development, gamedev, software development, tutorial, memory, computers, coding, games, video game, explanation, explained, how pointers work, how c++ pointers work
Id: DTxHyVn0ODg
Channel Id: undefined
Length: 16min 58sec (1018 seconds)
Published: Sun Jun 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.