Placeholder Image

字幕列表 影片播放

  • hello and welcome to something a little bit different from a I'm going to be talking about a specific feature of the C plus plus language pointers on.

  • I felt there was a need to do this video because UN discord and on YouTube and very male, I've been asked quite a number of questions about people's code that doesn't work on dhe.

  • Invariably, the problem seems to lie with a misunderstanding of how to use pointers in the first place.

  • So this is a little video I can use to excuse the pun point people in the right direction if they do have any problems with pointers in my video output to date, I've never really targeted a specific audience, and I'm trying not to with this video, either.

  • So it doesn't really matter if you're a beginner or intermediate.

  • Maybe there'll be something in this video that will be novel to you, and even for the non c++ program is out there that still really recommend watching it.

  • Because point is a fundamental regardless of which language you're using.

  • It just could be that other languages are hiding the fact that pointers are being used, So let's get started by asking the question.

  • What are pointers?

  • Unfortunately, there is a very, very simple answer to this question.

  • A pointer is just a variable that holds a memory address.

  • I repeat, a pointer is just a variable that holds a memory address.

  • If you like this video, give me that.

  • No, I'm only kidding.

  • There's actually a lot more to it, but fundamentally, it is really that simple.

  • I'll start by just introducing the basic syntax, but fundamentally, it's important to know that whenever woodworking with pointers were working with memory, and so here I've drawn some random access memory.

  • Some ram on I've got a column, which represents the addresses.

  • So this is the location in RAM on a column.

  • Represents the value at that specific location, have not populated any values just yet.

  • So let's assume in our program we have something along the lines of int A equals five.

  • We've asked the compiler to make sure that our program will have enough memory to store variable called a and give it the value.

  • Five.

  • Now we know that variables of different types of different sizes will not worry about that too much here, and so just to calm down the masses.

  • Let's assume that all the values are 32 bit when we write something like this in our program, we don't really have any control over where it goes in memory.

  • And we don't care either, because what we care about is that we can represent that location in memory through this variable name A.

  • Let's say the compiler has gone unused.

  • Memory address location 102 which will stay, is a and it's given it the value.

  • Five so loosely in our program, wherever we see the variable name, eh?

  • What we really mean is go to somewhere in Ram and look up address 102 which corresponds to a and return the value five.

  • So, to all intents and purposes, the variable name disappears on.

  • This situation is known as direct.

  • Addressing where we take a symbol on it directly corresponds to a location in the Ram.

  • Now, as discussed, a pointer is just a variable that holds a memory address.

  • So in our simple system here, let's create a variable type pointer on.

  • We'll call it be just a zit did with the interview.

  • The compiler will go on somehow structure our memory so that be stored somewhere in it.

  • So let's say here, be it started address 104 We've had no control over that.

  • It is where it is, but the value be holds is going to be a memory address so we could declare it explicitly.

  • B equals 100 for example.

  • So the value goes to 100.

  • What I'm trying to demonstrate here is there's absolutely no difference between the two things.

  • A pointer is just a variable that holds an address in memory.

  • It's very rare that will actually set the value of a pointer explicitly and numerically like this.

  • Instead, it's more useful to use the location of an existing variable in the program as the value that we give to the pointer on DSI in C++ provide something which is the ampersand symbol which behaves like get the address off a variable.

  • So here we've said our pointer be We want to set its value to be the address of wherever a stored.

  • So in this case we can see a store that address 102 So the value stored in point B is one oath two.

  • Now that we have a location in memory that stores a value, which represents a memory address to a different location.

  • We've implemented something called indirect addressing.

  • And so whereas we had an operator that allows us to get the address of a variable, we can have an operator that allows us to get the value at that address.

  • So let's create 1/3 variable into sea.

  • But I want to get the value from the location pointed to buy B and in seeing c++, that's the ass tricks operator confusingly not multiply in this case.

  • But this means get to the value at, so we want to get the value.

  • That's the location pointed to buy be.

  • So how will this be interpreted?

  • Well, the compiler will have created the symbol.

  • See, somewhere in our memory, for what we're saying is, please get the value at the pointed to location of our point of B.

  • So we'll look at the value of B, which is 102 in this instance, and we'll use the value of B as an address off the actual value we're interested in.

  • So this 102 points to a effectively, which is what we've set up here on, we'll get the value.

  • Five.

  • We have indirectly set the value of C to a using a pointer, be and that really is all there is to pointers, as I've stated, and I will state many times in this video that just a variable that holds a memory address.

  • But even in this simple example, we've started to see some confusing syntax we've introduced to new symbols now the address off symbol for enough.

  • But we've started to use the multiply ass tricks to represent Get the value off, and I think it's this usage of additional symbols that makes point is quite confusing to those that haven't seen them before.

  • And in fact it gets even more confusing because there is no variable type called Pointer.

  • As we'll see in the following practical examples, it's important that the compiler on the program and know what type of information the pointer is pointing to.

  • So we have to include the type somewhere in our point of definition.

  • But clearly we need to include something else because this is now saying that be is just a type interject, no difference A and C well in c n C plus plus to say that this is a pointer to an interview.

  • We include the ass trick simple again So we can read.

  • This is saying, Please create a variable that holds a memory address to another variable that is an interview on.

  • Sometimes you'll see it written out like this, and sometimes you'll see it written out like this, both of valid But Tut, tut, tut C and C plus plus were using the same symbol again to represent something else.

  • So now it represents.

  • Please declare Mia's a pointer and hear it represents.

  • Please get the value pointed to buy this pointer on.

  • When it's not in the context of pointers, it means multiply.

  • Fortunately, as long as you get into the habit of thinking methodically about how Pointers Air constructed, he won't get yourself into too much trouble.

  • It's quite important that Pointers know what type of data they're pointing to, and this is why.

  • So let's assume we're now working with one bite per location in our memory address.

  • So eight bits we know that in a C and C plus plus program, more or less all the time and in it is going to be a 32 bit value, so I compile will go on, allocate some memory for Variable A, but it'll take up four locations.

  • So let's assume that sticks it here at Location 100 our 32 bit variable will take up four locations in Ram.

  • That's just to know Jesus a zero a one a two on a three If I create a pointer again, so int star b equals the address off a on this time, let's assume that Bay has been created somewhere else in memory.

  • It's given the value of 100.

  • If I were to increment the pointer, so B equals B plus one.

  • Giving me the value 101 is not that useful, because now I'm 1/4 of the way through an ent.

  • What is typically more useful to the programmer is to move in a hole into step, so incriminating the pointer by one like this is really the same as saying.

  • B equals B plus the size off in fights often incident, which is the same as saying B equals B plus four on our system.

  • So our original be value pointed to the memory that represents this interview if we increment it once, we then start to look at the memory that represents the next interview, and this is quite useful.

  • But before we get ahead of ourselves, let's look at some practical examples I've created here, an absolute bird bones program to help explain the basics of pointers.

  • I'm going to introduce pointers by first talking about a raise.

  • So here I've created an array of 10 introduce called Some Array.

  • I'm not even bothering to initialize it, and the reason I've chosen to use an array is that in this situation, the compiler will create an array which is contiguous, and this means that the memory that will be used for this array will be complete and whole will be no gaps in it.

  • Nor will there be any obstructions.

  • There'll be a single solid block of memory allocated to implementing this array.

  • In this case, it's 10 inter jizz on.

  • We know that inter jizz of four bites, so we would expect 40 bites of memory uninterrupted to represent this array.

  • Here I've created two pointers.

  • We can see the interview on the ass trick symbol indicating that I'm creating a pointer of type interview I call this one location six on Dhe.

  • I'm going to point this pointer at Element six of the array that we've created.

  • So standard array notation here grabbing the six location in the array on.

  • I want the address of it.

  • So there's the address off operate on.

  • I've done exactly the same.

  • Another pointer, this time going to Location zero, which is the start of the array.

  • I'm not going to do something little strange.

  • I'm going to display the value of the two pointers on also the difference between them.

  • Let's take a look.

  • Well, here we've got two numbers on the difference between them is 24.

  • Location six is 24 larger than location zero, but the difference is just six.

  • Will this make sense if we've got six positions in contiguous memory apart from each other on each one of those positions represents a four bite thing in this case, an interview.

  • Then we've got 24 bites difference between the two locations, and this is exactly what we saw is the difference between the two numbers.

  • But when we subtract location zero from Location six as pointers, it returned.

  • The difference in this case in whole Inter Jizz, which were six, which is fair enough.

  • Well done it again.

  • This time we'll see the numbers are different, but they still differ by 24.

  • And the only reason I'm showing this is to emphasize that you don't really have that much control over where things go in memory when it one last time again.

  • Different numbers on the differences 24.

  • So the fundamental distinction to make here is that the memory addresses work on a per bite basis.

  • But the point is themselves well, they work in a space defined by the type of object that they're pointing to.

  • In this case, an interview.

  • We see that we declare a pointer by including the ass tricks at the start, and we've used the address off operator to get the address off something.

  • In this case, it happens to be Location six in this array.

  • Now, I noticed some of you that may have gone well over your head, but persevere because I'm going to show some other examples.

  • I'm also going to show situations that can catch out the unwary programmer.

  • And I hope by the end of this video it will all sort of gel together and you'll start to get the bigger picture.

  • I'm not going to give our race, um, values like giving it 10 values, which is just multiples of three.

  • And I want to demonstrate different ways of accessing the same data.

  • I'll get rid of this here.

  • We're using the address off operator off a location in the array.

  • In this case, it's the zeroth location.

  • We want the very beginning of the array.

  • Well, a raise don't have any pre cursory information attached to them.

  • So the zeroth Elements is really the start off the memory that was allocated for that array, which means, in fact, don't even need to do this.

  • We can simply say that's our location.

  • P Location zero equals some array because some array is, in fact, a pointer in disguise and you see no red lines.

  • The compiler is quite happy to accept this over.

  • I'll just put that back for clarity.

  • Let's display the contents of the array just to prove that it works.

  • But let's look at different ways of doing the same thing.

  • Now we know a thing or two about pointers.

  • We know that some array is just a pointer.

  • It's P location zero.

  • So the shit out put a memory address 10 times.

  • Since we know some array is the same as the zeroth location of the array.

  • We can use pointer arithmetic to add an offset to it.

  • So in this case, I'm going to add the I value.

  • So we'll take the zeroth location of our Ray, pointed to buy some array, and we'll add 01234 to it.

  • Let's take a look.

  • Well, we can look at the numbers this time when we see that they differ by four each time, conveniently the size of an interview.

  • So if that's our memory address, we should also display the value.

  • So I'm going to put in just some friendly text.

  • But this time we want to get the value at that location.

  • So summer?

  • A precisely.

  • But if you remember, the value off operator is the ass tricks.

  • Let's take a look, and this time we can see it is now displaying the contents of memory at that location, Yet we've not indexed it like an array at all.

  • Don't forget.

  • A pointer is just a variable that holds a memory location.

  • So here is that variable on.

  • We're increment ing that variable on displaying the memory location, but we're using the value off operator to return the value stored at that memory location.

  • We can do similar things in a slightly different way.

  • We know P location is the start of our array, so let's output that value and we'll also output the data stored at that location as we've just done so here.

  • We've got the address on here.

  • We're using the value of operator to return the value, but this time I'm going to increment are pointer each.

  • It's aeration of the loop.

  • Let's take a look.

  • And, as expected, we see the memory address is on the values at that memory address.

  • I'm just trying to demonstrate the flexibility you have with pointers on point arithmetic.

  • Let's just briefly examine why pointers may have a reputation for being more complicated than they are.

  • Let's consider the same example is we've just seen.

  • But this time, instead of it being an integer, I'm going to use a type char instead of an interview, right?

  • It's ah, well, it's an array of chance.

  • It's a string.

  • Hello, Well, then the type change we can see.

  • Everything is just the same on as we did in the very first example.

  • I'm taking two pointers to two locations and I'm going to display them.

  • Let's take a look.

  • So we're printing out what this string is.

  • We've got to memory addresses in this case, the only three apart, and it says the difference is three.

  • And that's because a single char is a single bite.

  • That makes sense.

  • But let's see what happens when we take these casts out off the location values.

  • Well, we start to see something a bit weird.

  • We see the two strings, but we see fragments off them.

  • In fact, location three starts from the third element of the string on location zero clearly starts from the beginning of the string on.

  • This is very confusing.

  • Let's say we didn't actually initialize it as a string.

  • We just initialized 10 bites of charm and now we run it.

  • You know, things are just getting worse here.

  • What's going on?

  • And that's because when you see Charles on pointers of Charles, it's very common that you're actually inferring a string, and so there's quite a lot of operators, amongst many functions across C and C plus plus that expect to see pointers of type char representing strings.

  • And so they do string specific things.

  • So in this case, this char star location three is being treated just like a regular string.

  • That's why we're not seeing the value of the address here unless we explicitly cast it to an integer.

  • So just a little bit of a trap for new place.

  • Some functions are expecting.

  • Pointers on will behave differently, depending on the types of pointers they receive.

  • Right now, for something a bit different, I'm going to create an object.

  • Some object.

  • The object has two fields x and Y on.

  • I've declared that X and Y be initialized with these values.

  • A three a two, a one, a zero in hex.

  • I don't really care what the numeric value is here.

  • I'm more concerned about the hex symbols being used because we're going to use the de booger toe.

  • Look at how this object has moved around in memory.

  • If we wanted to create one of these objects in our program, the easiest ways to simply declare it, and if we wanted to create an array of them.

  • Well, we'd stick of value at the end.

  • This is really basic stuff, and when we do this, we have actually created this memory on what's called the stack.

  • It's known that compile time what is required here so the compiler can go and deal with it all for you.

  • Let's just take a quick look at that running, but I'm going to use the de booger, so I've run to the line on what I can see.

  • I may try and zoom these windows so they're a bit more clear for you.

  • But when we create the object some objects we can see that the points of value has become populated on.

  • We can take the pointer object and drag it onto a memory window and actually have a look at the memory.

  • And so we can see here that are simple.

  • Structure of two types of vintages is a zero a one a two a three B zero b one, b two b three, and we'll see that repeated 10 times.

  • It's exactly what we needed.

  • I got a little curious at this point and decided to ride in a constructor because I wanted to test.

  • Was the compiler actually going to call the constructor of the object?

  • Take a look, so I run the code.