So we're now going to discuss memory and see now.
Currently we've been dealing with variables on the stack.
Essentially when you declare a variable here it is on the stack.
And the problem with stack memory is is very limited.
But it's also quicker to create than heap memory which we're about to discuss now.
So what is heap memory.
The operating system has a heap has what's known as a heap which is basically an extremely large amount
of data and we can ask the operating system to access that data so we can say to the operating system
I want 1024 bytes of data.
Can you please give it to me.
It will then respond with zero if it won't.
That means something went wrong or it will respond with a pointer to that data that is just allocated
for you.
So if I for example if I'm loading a movie file this change of megabytes in size I could say to the
operating system please give me change of megabytes of data.
The operating system could then respond with a pointer to that data and then I can then access that
data as if I allocated it here like this.
So we create a we create an array here with 1024 bytes.
Well I could ask the operating system for 1024 bytes and it would give that to me as well.
So the difference here is stack memory.
This is stack memory right now.
It is limited in size.
That is the cons the pros are it's quicker than ask in the operating system for memory.
OK.
So let's begin let's first include LCD lipped Hage which will give us access to the memory functions
we need I now need you to do char ask Iris PTA equals Matlock 1024 so you can see here Malik returns
a void pointer and it takes a size the size is how much data we want from them how many bytes we want
from them.
So by June 1 0 2 4 we're basically requests in the operating system for 1024 bytes of data for us to
use now because it's a void pointer we need to cast this into a child point because we want to access
this as a char so we do char asterisk right there.
As you learned in the casting tutorial.
So if we know a semicolon.
We should be able to point to zero equals a point to 1 equals B and I should be at it now output that
like this.
So we output points to zero less now compile that GCSE main Delta C dash show me and we should see a
which we do now if you put one here we should see B which we do so we can safely access 1024 bytes if
we go over that will crash our program because we've only asked the operating system for 1024 bytes
we tried to access and an index that takes us out of those bounds the program will crash at best or
undefined behavior at worst.
So that is how you can allocate memory on the heap and it's very useful.
Now there's one problem with this.
We need to free this data when we're done.
So you do that by doing free PCR.
It's as simple as that.
So while this does this allocates 1024 bytes from the operating system we now have access to that data.
So we set the first index to a the second index to be we output a we hope to be sorry point to one and
now we tell the operating system with free that we're done with this memory now we don't need it anymore
recycle it and give it to another program that might need it.
That's what we're basically saying here.
Without this you have what is known as a memory leak.
Memory leak is where you ask the operating system for data but never give it back.
Have you ever had a program that gets really really slow over time.
While a lot of the time this is because of memory leaks.
You have programs running that are not free in memory.
They've asked for and this can lead to a slower computer that is run in those programs because you never
give the the data back to the operating system even if you don't need the data anymore if you don't
need the data anymore you should pass the data back to the operating system by doing free PCR.
It then can give that data to someone else who needs it.
So that is basically how you allocate memory on the heap.
This could also be integers so I could do some like integer pointer.
Cast that integer.
And then I can use the size of keyword which gives us the size of a type.
So if we do size of in it it will return for.
And this is executed at compile time.
By the way.
So the compiler will replace this with for as it is compiling.
So if we compiler now may we see nothing because I have an output of it.
But now if we do print f I and then we do asterisk PCR.
Remember we used the asterisk to say we want the value of what asked pointing at if we now run that
again.
We see 6 8 8 6 8 0 8.
Now why do we see that.
Because we haven't set the memory.
If you don't set the memory is basically a load of garbage or what.
Or whatever was left in memory from the last person who used it.
So to do that we do punch to asterisk PTA equals 50 and after you now compile that again we see 50.
So you can see my log could basically get you any amount of size of data and then you have a pointer
that you can access however you want.