WEBVTT

00:00.750 --> 00:06.390
So far we've only been using one kind of memory which is stack memory.

00:06.660 --> 00:13.130
When we create a variable in a function the variable only lives for the lifetime of that function.

00:13.910 --> 00:21.200
When a function finishes all the memory of that function is destroyed stack memory is really a special

00:21.200 --> 00:28.970
region of your computer's memory that stores temporary variables created by each function including

00:28.970 --> 00:30.820
the main function.

00:30.840 --> 00:36.580
We're not going to go over what the stack really is and how it operates.

00:36.600 --> 00:41.160
It's not really all that relevant for this specific course.

00:41.690 --> 00:51.930
Just know that memory for our variables gets destroyed or deallocated when the function finishes.

00:51.930 --> 00:59.260
There's a different way we can create variables and then is through what's called the heap the heap

00:59.270 --> 01:07.540
is essentially a bunch of memory that can be dynamically used by our program.

01:07.710 --> 01:15.290
However it's lifetime is not tied to any functions lifetime it's deallocated when we the programmer

01:15.330 --> 01:23.430
deallocated me potentially it can potentially live for as long as the program itself.

01:23.430 --> 01:30.780
In other words though all the memory you allocate from the heap will be recovered by the OS once your

01:30.780 --> 01:33.850
program actually finishes its execution.

01:34.880 --> 01:40.050
This is called heap allocation or dynamically allocated memory.

01:40.190 --> 01:47.990
It's very useful for when you want to be more flexible about the size of memory that you need.

01:48.050 --> 01:56.840
For example arrays in our last section you had to specify how big the array was beforehand say we need

01:56.840 --> 02:02.490
to hold a bunch of characters but we didn't know beforehand how many won't.

02:02.540 --> 02:08.810
One way we could do is just to make an array that had more than enough characters that we needed say

02:08.810 --> 02:10.200
a thousand.

02:10.340 --> 02:14.950
But what if we only ever needed 100 in practice.

02:14.960 --> 02:19.730
Well our array isn't very flexible and we'd be wasting a bunch of memory.

02:19.820 --> 02:22.120
About 90 percent in this case.

02:22.580 --> 02:28.120
Well instead we could allocate the proper amount each time from the heap.

02:28.130 --> 02:32.830
So here's how we can dynamically allocate memory.

02:32.940 --> 02:50.070
So it's always going to be tied to a pointer so we knew it's a pointer and we say new it can.

02:50.260 --> 02:59.810
So what this did was to get an integer from the heap and said it to our new into pointer to do this

02:59.840 --> 03:08.180
we use the new keyword new will actually fetch memory from the heap and return it as a pointer to that

03:08.180 --> 03:09.890
new memory.

03:09.890 --> 03:15.100
So in this case it fetched an integer from memory so about 4 bytes.

03:15.500 --> 03:24.410
Then we can use either new integer just like we do with any other pointers so I could say new ints Winter

03:24.910 --> 03:30.830
calls to 8 or something like that help put it.

03:30.950 --> 03:44.160
So the value of mine you did for us you it's Peter this.

03:44.280 --> 03:45.340
We run it.

03:46.920 --> 03:48.510
We eat.

03:49.110 --> 03:56.720
But like I said in the last lecture this memory won't go away after the end of the function.

03:58.090 --> 04:03.940
So how do we get rid of the memory when we're finished so we're not actually getting rid of the memory

04:04.000 --> 04:04.430
yet.

04:04.450 --> 04:11.740
Although like I said when the program is finished executing all our memory will be deallocated for us

04:11.740 --> 04:25.540
anyways but if we want to sort of do this a bit more properly what we do is we say delete new pointer

04:27.280 --> 04:36.750
OK so the delete key word will put the memory that you got from the heap and it will return it to the

04:36.750 --> 04:37.190
heap.

04:37.200 --> 04:44.300
So how you read this is delete the memory pointed to by new into pointer.

04:44.480 --> 04:47.270
OK so you should read this.

04:47.340 --> 04:50.510
So this is always good practice to do.

04:50.520 --> 04:56.020
You should always delete the memory you took from the heap when you're done with it.

04:56.080 --> 05:00.000
So how to do this with arrays in the next lecture.
