OK.
Welcome back to yet another lecture on dynamic memory and pointers in this lecture, we are going to
go over something that I actually kind of foreshadowed before that we were going to go over and that
was in the final video slash lecture that was on while loops.
It was the one where we did an example where we're using an array at the beginning of the video.
I mentioned something that was not good programming practice, and that was to make something like this
where we had a variable called size.
We see in to the size with user input.
And then we were making an array like this where the size was defined by this variable that we ran into.
And the reason that this is a problem.
Is because although I said it works on some compilers, it doesn't really work for the C++ language
in general, it's not supported.
And the reason it's not supported is because this actually needs to be known at compile time.
The array size, and this is not something that is going to happen at compile time, right?
It's going to see this.
But size is not known until at runtime.
The user enters the size, right?
That's problematic.
So we cannot actually do something like this and by cannot.
I mean, you still can do it because of + + compiler extensions.
So many GW compiler that we installed.
Seems like it actually works for something like this, but you should not do this.
It is not good programming practice.
However, there is a way that we can safely do something like this and like you might be able to guess
it is going to be using dynamic memory on the heap, which is something called a dynamic array when
we're doing it in form of an array.
So let's go ahead and see how we can do that.
So we don't want to make this on the busy stack, right?
Because that becomes an issue.
C++ doesn't like, you know, it wants to know the size of it because the stack is a busy place and
we can't have these like arrays that can be really big, that aren't known on the stack.
We need to put them in a more free area, which is over there in the heap.
So I'm going to leave this part right here, but I'm going to change this to be an array on the heap,
since it's on the heap, we know that we're going to need to make a point, right?
I'm just actually going to call this.
I'm going to call this like my RR for like array.
So it's going to be int my R for my array and it's going to have to be a pointer.
Like I said, when I say new equals new int.
And then I'm putting in square brackets and I'm putting size like this.
And that's all we have to do to make a dynamic array that it's going to be on the heap.
So now we can totally read into this, so I'm going to go ahead and do that, I'll do a for loop, I
say into a zero.
I is less than size I was plus so it would go as many times as the user enters, and then I'm going
to just sit out, I'll say into a number and I'll see an end to my air.
And I do not need to reference it with the asterisk because this operator right here, the subscript
operator, actually references memory as it is.
So this highlighted thing right here, which is that subscript indexing operator.
That's the thing that's referencing.
So we do not need to put in an additional star in front of this because it's going to be handled by
this Ferrara.
OK, so pretty cool.
We can actually put AI in here and we will.
Then read in to that location in our.
So once we read in everything into the array, I can then print it out, so I'll just do another loop
here and I zero Eilis size eight plus and I'll just say see out and it will be like my array I.
And then I'll go ahead and put a little camera in a space like that.
And we should be good to go.
So one question that you might have been thinking about now is, well, we've allocated memory, so
we need to delete it, right?
We've allocated heap memory.
I'm sure that even though it's an array, we must need to delete it.
Right?
And you are correct.
We do need to delete it.
But you might be thinking, Well, how do we delete an entire array?
Because if you know, really, this pointer here is just pointing to the starting byte of the array.
This array of integers and memory and continuous memory.
Normally we would just delete, you know, like my aunt or something, if it was a dynamically allocated
single integer, but to delete an array of values to make sure we delete everything, we actually need
to use a slightly different syntax instead of just saying delete my ah, we are going to say delete
with some square brackets and then say my error.
So this right here is the syntax that will delete everything in the array.
So we don't have to worry about only deleting like the first item that's being pointed to in that contiguous
memory section.
So this should be good to go.
Just going to run it for kind of example purposes, so we can kind of see that this works out.
I go ahead and I can pilot.
Of course, I'm using the same program.
You know, you probably should make a new program for all of this and save it and store it in an organized
fashion.
Unlike I have been doing with everything just stepped in here in the first program, but nevertheless,
I'm going to run this.
So I go ahead and I run this.
It doesn't really say anything because it's just asking me for a size without saying anything, so I'm
going to go ahead and say for then enter a number.
I'm going to go ahead and just put one two three four, right?
Then of course, that all prints out right here.
One two three four.
And we're good to go.
This thing got deleted because of this.
So everything is all good now.
So.
What you've kind of learned now is how to make a dynamic array.
You've learned that it is necessary to make a dynamic array if you want to do something like this where
you have an unknown size, at least the size is not known immediately at compile time.
If you want to make an array on the stack, if you're going to make a variable, it has to be a constant
variable or you have to put a like hardcoded value in there so the size can be known at the compile
time.
All right.
So I think that this was probably also enough for a full lecture.
I'm not going to go into too much more because I think this is enough already with just a how to make
a dynamic array scene that it's the same to read into it.
You don't need to reference it to do that.
This D references it here, and this is how you would delete that dynamic array.
All right, so with that, I will see you in the next lecture.