So far we've only been using one kind of memory which is stack memory.
When we create a variable in a function the variable only lives for the lifetime of that function.
When a function finishes all the memory of that function is destroyed stack memory is really a special
region of your computer's memory that stores temporary variables created by each function including
the main function.
We're not going to go over what the stack really is and how it operates.
It's not really all that relevant for this specific course.
Just know that memory for our variables gets destroyed or deallocated when the function finishes.
There's a different way we can create variables and then is through what's called the heap the heap
is essentially a bunch of memory that can be dynamically used by our program.
However it's lifetime is not tied to any functions lifetime it's deallocated when we the programmer
deallocated me potentially it can potentially live for as long as the program itself.
In other words though all the memory you allocate from the heap will be recovered by the OS once your
program actually finishes its execution.
This is called heap allocation or dynamically allocated memory.
It's very useful for when you want to be more flexible about the size of memory that you need.
For example arrays in our last section you had to specify how big the array was beforehand say we need
to hold a bunch of characters but we didn't know beforehand how many won't.
One way we could do is just to make an array that had more than enough characters that we needed say
a thousand.
But what if we only ever needed 100 in practice.
Well our array isn't very flexible and we'd be wasting a bunch of memory.
About 90 percent in this case.
Well instead we could allocate the proper amount each time from the heap.
So here's how we can dynamically allocate memory.
So it's always going to be tied to a pointer so we knew it's a pointer and we say new it can.
So what this did was to get an integer from the heap and said it to our new into pointer to do this
we use the new keyword new will actually fetch memory from the heap and return it as a pointer to that
new memory.
So in this case it fetched an integer from memory so about 4 bytes.
Then we can use either new integer just like we do with any other pointers so I could say new ints Winter
calls to 8 or something like that help put it.
So the value of mine you did for us you it's Peter this.
We run it.
We eat.
But like I said in the last lecture this memory won't go away after the end of the function.
So how do we get rid of the memory when we're finished so we're not actually getting rid of the memory
yet.
Although like I said when the program is finished executing all our memory will be deallocated for us
anyways but if we want to sort of do this a bit more properly what we do is we say delete new pointer
OK so the delete key word will put the memory that you got from the heap and it will return it to the
heap.
So how you read this is delete the memory pointed to by new into pointer.
OK so you should read this.
So this is always good practice to do.
You should always delete the memory you took from the heap when you're done with it.
So how to do this with arrays in the next lecture.