OK.
How's it going, everybody?
So we are coming back to a little bit of what we talked about in the last slideshow lecture, we went
over quite a bit of what may have seemed like a complex topic, so we were looking into the virtual
process memory.
So we talked a little bit about that in general.
And then we were talking about those pointers and dynamic memory allocation.
And of course, she didn't think that I was going to leave you hanging with that.
That's kind of a confusing topic.
So don't worry, we are going to slowly familiarize ourself with that stuff, and I'm going to make
a few lectures where we go into this ED and we actually look into dynamic allocated dynamically allocated
memory and pointers in general, whether they be on the stack or the heap and just kind of get some
practice.
So you all feel comfortable with it.
So that's what we're doing in this lecture.
I'm going to go ahead and make a few pointers, both on the stack and on the heap, so they will be
dynamically allocated ones and not dynamically allocated ones.
Like, I don't mean that the pointers are being dynamically allocated, I just mean, we'll make some
dynamically allocated memory and we'll of course, use pointers to point to that and then we'll have
some pointers to just point to things.
But that's, you know, the variables that we're trying to have point to will be on the stack.
So let's go ahead and mess around with some of that, so I'm going to go ahead and just use integers
to start out with.
So what I'm going to do is I'm just going to say and and then a little star for the pointer like we
were talking about.
This is the multiplication symbol, right?
But when I use it like, like this?
Im starting to make a new pointer variable here that is most likely.
We're saying that it's going to be pointing to an integer.
But this part right here is what's important.
So this is actually saying that this is going to be a pointer and then here we have the variable name
of the pointer.
So I just declare this pointer right?
And then I'm going to make another point here, but I'm going to call this my dynamic pointer and I'm
going to actually say equals new end.
And so remember to back to the last lecture.
The thing that makes it dynamically allocated is the fact that we're using this new keyword right here.
So we've done this pretty much the same thing on this side of the equal sign.
We're just making a variable that's a pointer, but then over here we're saying, OK, set aside some
space on the heap so we can have some dynamically allocated memory that we can point to now.
And we're saying that that's going to be an integer type, integer day to day.
So what I want to do now, as before we do anything else, I want to show you what the pointer is and
what the pointer is pointing to at this point in time.
So many points.
So I am going to simply point out my pointer.
And what I what I'll do is I put a little label here, so I'll just say my pointer equals and then I'll
just say my pointer, and then I'm also going to say, what do you want from my dynamic corner?
So I say my dynamic pointer equals and then I'll say my dynamic pointer.
And then we will say, actually, you know what?
I put this in between because I'd like to couple the variables together.
So I'll say my pointer value equals and then what we will do is we will use the star again with my pointer.
Accept what we are doing now is we are d referencing the pointer, which just means that we are accessing
the thing that the pointer is pointing to, which is our integer right?
Except we have not initialized this variable yet, we've only declared it, so that's why I wanted to
print this out to kind of show you that there will be a garbage value stored in here.
So I go ahead and I'm going to print that out.
I'm going to do the exact same thing for this, except I'm just going to copy paste it down here.
So
we'll do this, but we'll say my dynamic pointer value equals and then we'll just reference my dynamic
pointer as well.
So syntactically you noticed that this all this stuff is very similar.
The only difference was when we initially allocated the space for this variable in the heap.
For our dynamically allocated one, we had to use this new and.
Right, but everything is subsequent is pretty much the same, we did reference both of them the same
way by putting the asterisk in front of it.
Right, so we're printing this out now.
I'm going to go ahead and just save that program right now and then run it just so we can see what we're
dealing with so far.
So I'm going to go ahead and compile this and run it.
All right.
So let's check out what what we have here.
So I'm going to make some more space here in the console.
Hopefully, I can scroll up without going too far.
OK, so look what we see here.
It says my pointer equals and then we have this hex number.
So this is the hex address I was talking about.
Remember, we were talking about all those parking spots in memory or all of those houses, whatever
the analogy that fit best for you.
That's what we're looking at now, we're looking at the address because remember, the pointer itself
is just a number, it's just holding an address.
Right?
But there's something stored at that address that we care about.
And so when we're pointing to something, it just has the address.
But when we do reference it, look down here, it says my pointer value.
Here, when we put the asterisk in front of it, when we're printed it out, we're saying, OK, I don't
want the address, I want you to now use that address.
Go to that address and show me what is stored at that address.
So that is the difference here, we're getting the actual value that stored at the address here, when
we print out the pointer itself, we noticed that the pointer is nothing but a no.
It's just an address, right?
So a very important difference.
A lot of people are confused by foreigners who don't really understand what they are, especially when
they see data types in front of the point there, like an integer pulling a string pointer, a flow
pointer.
These are all different things.
They're not different things.
All, no matter what data type you put in front of the pointer, it is not any different than any of
the other data types because technically it's just storing an address.
The pointer is always just going to be an address.
The thing that is stored at that address might be a different thing and different data type.
But the pointer itself is always going to be this highlighted thing right here.
It's going to be some sort of address and the default way that you're going to view these addresses
is in hexadecimal format, right?
So pretty cool.
Let's go ahead and continue on instead of having these values.
Yeah, I should probably talk a little bit about that.
There's some huge numbers in here, right?
And we've seen this before.
This is, you know, these things can totally change.
Like, let's say I compile this again, I run this again.
Let's see, we might have the same ones, actually.
Dynamic point or no, it's actually.
So the first one is the same, but the second one is different.
Right?
So look at the dynamic pointer value here and look at the dynamic pointer value here.
We have a different number here.
When I compile it and ran it again.
So these are just random garbage values.
They're being, you know.
It's not really I don't want to say, stored so far, but we're just the things that are at these addresses
and memory are just whatever, whatever was there.
You know, and we we don't really know what's there.
It's just kind of a mystery because we haven't initialized these to what we want to yet.
So some random number is just sitting in there.
We just have random data hanging out in these un initialized variables, right?
And I keep referring to these variables.
Remember, these are the pointers, but the things that they're pointing to, the things that lie at
these addresses, for example.
This address, if you go to this highlighted address, this is stored there.
The first time we run it, when we went to this highlighted address notice, it's another address.
It's not necessarily going to be the same every time.
You know, this is just how the operating system works.
It's just doing its thing, making these addresses.
And you noticed there was a different value stored there, so we're just going to get these different
random garbage values until we actually specifically reference these addresses and put something in
there or store something in there.
So let's go ahead and do that right now.
So now I'm going to just d reference my my point here.
So this is the one that's just on the stack, right?
This is not dynamically allocated, you know, we're in the main function, so it's going to be on the
stack for the main function.
So we're going to now store something here.
Someone put a five for this and then I'll also d reference my dynamic pointer and I will store.
The value seven at that address, so five is being stored at this address.
The pointer is pointing to this variable.
So for example.
The first time in my scrawl, rough, so.
This first time right here, Neal, this was the address, of course, when I compile this and read
it again.
It could be a different address and most likely will be a different address.
Here it was the same, you know, but you know, it could be different, it might not be, you know,
it's this address was different from my dynamic corner.
So I'm going to go ahead and now print this stuff out again so we can see how it's changed.
So what I'm going to do is just copy only these two right here, because these are the lines where I
print out, Oops, not those two, it'll be this one and this one right here.
So let me do this one first, actually.
All right.
So I'm going to print this out and then I am going to I'm sorry, it's so slow here.
I still have that wrist injury, so it's a little hard to do things quickly.
All right.
So we're going to go ahead and save this.
I'm actually going to clear and compile and run again.
All right, so now you notice that we actually have these values stored in their.
So originally we had some.
Some different numbers stored in here, you know, like these are just these garbage values.
And it looks like our one on the stack is actually the same as the previous previous times that we compiled
and ran it.
But here we have ah, you know, the point is, is that both of them have these garbage values.
And then we notice down here that it is actually now storing the things that we put in there.
And that is that happened right here on Line 13 or 14.
So we referenced my pointer, which gave us access to the memory at the address, right?
So this address six one, you know, Hex six one two five zero.
We were now able to do reference the memory at that address and take this five and store it there at
that location.
And same for the dynamic memory allocation, one on the heap, we were able to take this seven and store
it at this address, and they are no longer these values.
They are now these values, right?
So pretty cool.
You know, pointers.
Hopefully, it's clearing up a bit.
They're not that complicated.
One important thing, though, about the dynamically allocated memory pointer is that we have to explicitly
delete that memory, which means allocate that memory.
And why do we have to do that?
Well, in the last lecture, I briefly mentioned it towards the end of the lecture that if we do not
allocate that memory, it kind of remains taken up.
You know, it's it's reserved.
I think I use the word reserved so that memory is stays reserved.
It's kind of like.
Think of it like a restaurant or something, maybe you can use this analogy so, you know, someone
reserves a table and you're going to put these people at that table.
It's a location, right?
That table is a specific location.
You put people in food at that table, then you know you're done using that table.
The restaurant is done, the people leave and you know you.
But what happens is that you leave all the food on the table and in fact, you leave the the reserved
placard or something on the table.
So the table still says Reserve, but the table isn't reserved.
But now you can't use that, and let's say you're getting like a massive influx of people into this
restaurant, like you're just having to suddenly really busy.
And let's think of the think of.
You know, maybe you did this multiple times, like.
You didn't only forget to clean the table and take the reserved sign off of one table.
What if you did that for like 20 tables or something like that and now you're having tons of people
coming in.
All of those tables are still taken up and you have very little space.
So you can imagine that if you leave a lot of memory on the heap allocated, of course, it's a really
big area of memory.
But if you have enough data that you need to now store, you might not have enough space, right?
That is a potential thing.
You're running out of space and you're limiting yourself.
So that's kind of the main reason why it's not good to have memory leaks.
And a memory leak is when you do not allocate the dynamically allocated memory, which you did right
here, right?
We did it right here.
We made some dynamically allocated memory right here and have a pointer to it.
If we do not delete this, it remains reserved.
And if you can imagine if you multiply this by a very large number of times, you could potentially
not have enough space for your program.
And this is really only in the kind of realm of you running your program, right?
So when you run your program, your program gets loaded into memory and then you start having these
chunks of space taken up, right?
So once you're done running your program, these memory leaks are no longer really a problem.
Like it's not like they remain on your system like forever or something like that.
So don't worry about that.
But if you are running a program continuously, then these memory resources become quite valuable and
they can run out of space if you were to have a ton and ton of memory leaks.
Right?
So that's kind of the main point.
That's the bad thing about memory leaks.
So let's go ahead and do that.
Let's make sure that we did allocate that.
It's going to be as simple as using this word delete.
So I say delete highlights there, and I just say delete my dynamic pointer.
And that's all you have to do in C++, and it will d allocate this memory that was set aside right here,
you noticed that my dynamic pointer was just an address, right?
But fortunately, C++ knows what to do with this delete keyword.
So it sees this my dynamic pointer.
And even though it's an address, it's not going to just delete the address.
It's not just going to delete the pointer, it's going to actually go and delete and clear up the memory
at that point it right at that address.
That the pointer is pointed to, that's what I mean.
So pretty cool.
I think that I'm going to kind of leave this first lecture at this, so I don't stuff too much into
it, but in the next few lectures, we're going to continue on looking at some pointers and their use
cases.
We will use some functions.
And stuff like that.
Look at returning pointers.
I will use some other data types, like a string, just to kind of show you that it's the same way we'll
look at, you know, in another way to initialize dynamic memory dynamically allocated memory pointers.
And we will also talk about using dynamic memory with arrays as well.
So future topics.
But hopefully this is enough to just help you understand what pointers really are as far as you writing
code for them and the difference between a pointer that might have memory, that's that's on the stack
compared to a pointer that is pointing to something on the heap.
All right.
So with that, I will see you in the next election.