Hello and welcome guys.
We're now going to discuss memory functions and see so first include the memory dot Hage --.
And this will give us access to the memory functions in C..
So I want you to make a shop of a wave a char array with 20 bytes.
And then I want you to go print half percent see and then to your next argument and do f 0.
So this whole output the first value in the buffer.
But we haven't set the buffer to anything so what's going to happen.
Well it's going to output the memory anyway by doing this.
It adjusts the stack pointer given us 20 bytes to use in the stack.
Right.
But that memory is in an unfinished slide state which means it could be anything we need to actually
set that memory which is why I'm discussing memory functions now.
So if we go to a problem just so I show the show you this do JCC made up C dash or may run that you
see a plus.
Now we didn't put a plus in there.
That's just happens to be the data that that is there when we run that.
It could be anything.
It could be what whatever was used on all in that stack memory last before us you know the So you're
calling a few functions it could be data leftover in the stack from another function call is it's an
it's in an unfinished line state which makes it dangerous to use.
So what you can do you can change this memory so men set both a zero size of both.
So what is this to them.
Well they set the entire array to zero so I will go index zero equals zero index one equals zero and
so on until the entire array is set to zero.
So this size of as I said it returns the size of elements and at compile time.
So during compile nation this is replaced with 20.
Right.
So what's going to happen here.
Size of gets the size of our array which is 20 bytes and then we set the entire thing to zero.
That is what is happening.
So if we now compile that again
we can see nothing because zero is a no Terminator.
Remember I said that if you want to change this to quote a because this is this is for one by the time.
Right.
And if we then compile that we see a and this entire array is now a.
So if we do that for five
we see eight.
So Mama said is great for initializing arrays basically setting them all to zero is is a common practice.
Mainly for safety especially when load loading in strings here.
Because if a null terminated isn't included then and you go to print out the message then you're going
to overflow the buffer right.
Because your print f when you pack light if I do print f s and I pass in the buffer here it's going
to try and read the entire buffer until it finds a zero which is a null terminator which tells the program
that the string has ended.
But if there is no zero in here until the end of the buffer we're going to overflow it and just to demonstrate
that if we put an A here now so percent asks will output the buffer as a string what percent s says
is go through the buffer until we find a decimal zero remember the ascii table not a number zero a decimal
zero an actual binary zero eight zeros in binary one it finds that it'll stop trying to output the string
because the strings ended right but because we haven't provided a null terminated here because we've
set our entire buffer to a right it will never end.
So if we try that now you can see it does I put our right but output some other data as well.
That's because it passed.
That's because it overflows our buffer for a few bytes until it sees a zero.
But the zeros outside of our buffer.
So we essentially have a buffer overflow there.
So if we was to do something like buffer 19 which is the last bytes of our rate by the way equal 0 0
0 which is hex decimal 0 you could even do decimal 0 if you wanted.
That's fine.
Equal zero.
If you compiler it now now only outputs the data it was supposed to because we set the last byte of
our buffer to a decimal 0 right by here on line 8.
So that is some useful advice.
So there's some other functions I want to show you.
There's a man copy which is quite an impressive function.
So before we do anything further let's set this to zero again.
OK.
And now what I want you to do is I want you to do men men copy and the destination is going to be b
you f the source is going to be.
Hello.
Well it's just going to provide the string right there and the size is going to be how many bytes is
that one two three four five five bytes so all this does this copies Hello.
Into our buffer and it copies five bytes.
So now let's explain this a little further.
Remember I said the one you provide strings like this one it gets compiled it puts this elsewhere in
your program and this becomes a pointer this becomes a value to add value to the address.
So that's important to note that.
Right.
So if we now compile this and we go man you see Hello.
So what this does.
It copies Hello into our buffer here.
Nice handy feature.
So let's try and be a little more clever about this then at the top here.
Let's go char.
My message equals hello world.
And now we're going to go my message and I'm going to cause a problem on purpose so you can see this.
I'm gonna go size off my message and what we're now gonna do we're gonna compile it again.
And what's this.
We can only see hell.
It doesn't say hello world.
Well this is indeed help because if you ever made this mistake and I'm showing you this because it's
a common beginner's mistake size of we're using size of on my on my message variable but you're forgetting
the size of his calculator compile time and secondly my message is a pointer.
It is not.
Hello world.
It points to Hello world.
So my message is a point to guys.
So a pointer is four bytes.
So size of returns fall.
So we only see how now what you can do is SGI Len which is a function in C.
I would just pass my message there.
So all this will do this will get the length of this string.
How does it do that.
It goes through every single bite until it finds our null terminated.
And remember what I said when you do these double quotes compile time it automatically puts the null
terminator in for you when when it corrects your XY file.
So if we now run that again
you can see hello world.
So that worked perfectly.
So what's happening here.
We set our entire buffer to zero.
We copy my message into our buffer.
And my message is a pointer pointing to the address of hello world.
Because remember hello world is in store here.
It's stored somewhere in our program as a series of bytes.
It's a compile time is replaced with the value to the address.
So we copy hello world into our buffer.
We output it.
So that's that's how you copy memory.
So now let me discuss memory compare functions.
So the memory compare function allows you to compare two pieces of memory and see how close they are
to a match or if they do match.
So let's now do that then.
So what I want to do is I want you to go in search results equals men compare at M E M C MP and ask
for buffer 1 and buffer 2.
So let's just do Hello World and then do.
Hello world here as well.
Make sure that the same guys so I can demonstrate this.
And now just do SETI Len.
Hello world.
Okay.
So that's going to do that's going to compare the halo will memory with Hello World.
And it's the length of hello world.
So compare all of that and it should be zero.
But anyway let's test that the second part of the percent I hear and put results in here and let's compiler
and you can see zero.
So men compare function f returns zero if the two pieces of memory match.
So this is the first address.
This is the second address and this is the length that we're going to be comparing.
Okay see you now know that many men compare returns 0 1 they both match.
Now what if they differ.
Well let's try that shall we.
I want to put in a buy here and I want to put a B by here and then I want you to compile that and run
that and you see a minus one.
Now the reason you see a minus 1 is because what the men compare function does the first bite that does
not match.
It checks to see which one is greater.
The one in point to one or the one and pointed to.
And it returns minus one here because B has a greater decimal value than a.
Because if you remember the ascii table you have decimal numbers and in characters next to it.
Well if you look at the ascii table you'll see a is before b the uh the decimal equivalent the binary
so a and b differ.
And because B is greater than a we get a minus one returned.
Now if you change this to C but keep this is below.
And if you then compiler you see 1 so then that has the opposite effect because C is greater than B
so returns 1.
So once again if they match it returns zero.
So I've just wrote this code quickly just to demonstrate this a little further.
Here we say if the buffer and my message match like the values match the values that pointing at matches
based on this length then output they match.
So essentially because the buffer has hello world because we copy that here on line 9 and my message
has hello world then they are both a match so that so it will output this message without men copy here
it will not output it because buff no longer contains hello world.