All right,
let's get started. Well,
welcome back, everyone.
To another exciting day of cs 107.
And of week 2, here.
Which is.
I don't know if that's moving quickly or slowly for you,
but whatever the case may be,
hopefully it's all.
It's all.
It's all going.
A couple announcements before we get into lecture today.
The first one,
the first couple are things that have already happened.
The first being that labs,
hopefully all happened this week,
hopefully you all enjoyed that.
That process of working through c strings,
a little bit of gdp.
Just seeing. Generally,
how to work with.
See and work with with pointers.
The other thing I just want to mention briefly is that.
Assignment 0 graves went out a couple of nights ago.
So we made a post about that,
with the medians.
If you want to review your grades at any point throughout the quarter,
you can go to the grade book link on the course website that will show you your assignment scores,
but also your lab attendance.
Note that the lab attendants will not show up.
Until the end of the week will probably posted around Saturday. So.
You don't worry if you just want to laugh and you like,
oh gosh,
it's not showing up.
Don't worry if if the the rule of them is if the grade,
if there is no line for that assignment or lab in the grade book,
that means we haven't posted anything yet.
So don't don't panic if you don't see a recent assignment or lab.
Big thing to note,
of course,
coming up is that assignment.
1 is due.
On Monday,
this coming Monday,
that is the normal deadline for that assignment.
Please check out our late policy if you have any questions about how our deadlines and hard deadlines work.
I realized that the policy is maybe a little different than what you might be used to.
From other.
C F classes.
So just.
Just watch that. And. Be,
be aware of that.
All right. Ok, let's.
Let's get into it.
Here we have on deck for today's lecture.
So last time we ended with a discussion about arrays,
we saw how to allocate arrays on the heap.
We talked a little bit about how to allocate them on the stack,
and then we saw lots more with arrays,
and in particular,
with c strings.
In lab this week.
So we're going to pick up where we left off from that.
And we're going to go back to this question,
which has come up a few times in piazza and an office hours already,
which is, when,
exactly do we use.
The stack versus the heap.
Why should I prefer.
One form of memory allocation over the other.
They're not,
will look at.
Somewhat briefly,
at another.
Common example of how cleaners can be used,
which is to pass variables by reference.
So you may recall from c plus plus that there was some special syntax that allowed you to say that a variable was passed by reference.
In c,
we don't have that syntax.
So what do we do instead?
And in the main part of the of the class today is going to be working through some pointer code.
Examples will be.
Mostly on the terminal,
but then all switch back to the slides of the end to draw a couple more pointer diagrams.
And the intention here is we work through a bunch of stuff.
On Monday,
we saw some good examples of how to help pointers were.
Some of the operations work.
We saw the pictures for that.
And now our goal is, well,
how does this actually translate to the code that we're writing?
How do we actually.
How does it,
how did these.
For example,
how did memory errors,
and sort of the common issues that we were discussing last time show up in our code,
whether that be in our output or.
How do we debug it?
How do we look at.
How do we use something like gdp or valgrind to make sense of.
Something that is going wrong in our memory,
so will switch between.
Code and slides as a way to.
To to work some of that out.
All right. Okay,
so let's get into it.
With a. Somewhat.
Condensed discussion of static versus heap.
We've certainly talked about this on.
On on pizza,
and there's some stuff in the assignment,
1 hand out an advice page about it to look into it.
But here's the run down.
So we did.
We saw two different ways to allocate.
In array and focusing on arrays,
because those are by far the most common cases where we need to think about using the standard of the heap.
We saw two ways to allocate an array.
One of them was to say,
tray bracket 5 wished,
declares the memory on the stack.
One of them was to declare an in star and have it.
Point to a Malick. Right.
With the stack.
There are some pretty compelling advantages,
and by far the biggest of them is that the memory is automatically cleaned up for us.
We don't need to call free,
we don't need to remember to.
Clean anything up once the function returns.
The memory associated with a rr is just.
Is just taken care of and program continues to run.
No memory leaks,
new memory errors.
That's that's really nice.
Another advantage that we started to talk a little bit about in assignment 1 is that the stack.
Is considered really efficient.
What this is saying is, anyway,
you may be thinking from when you think about efficiency from the perspective of. Cs,
one of six,
for example,
and thinking of like,
big o run time and asking, well, wait.
Are you saying that the heap runs in something that's not constant?
I like is the heat running,
and in some crazy,
linear time,
what's the factor?
It's it's not quite that.
Both the start allocating on the stack and the heap are largely constant time,
but the constants are very,
very different.
So if we were to. Have,
for instance,
a Malek.
Inside of a loop that was running for 100000 a million 100 million iterations.
We start to notice that the program would put down a little bit,
whereas if we had been doing exactly the same loop with a stack. Array. Way,
way faster.
So in general,
the rule of thumb is used the stack when you can. If,
if there is.
A good like,
you don't need a good reason to use the stack actually sort of be your default.
Go to.
But there are a couple of reasons that you might need to use the heap.
We already saw a couple in. Throughout,
which is that the big one,
of course,
being controlling.
We need to control the run,
the lifetime of the variable that we allocate.
So for instance.
In read frag in the starter code,
first assignment. 1.
We can read the the fragment into.
Into memory,
but then we need the fragment to stick around until after we have resembled everything.
So it would be a huge problem if the memory word allocated.
At the end of read. Fg. Therefore,
after use the heap.
Another advantage that we will talk too much about in lecture is that the memory can be resized.
So there is this function called relic.
I encourage you to take a take a look at it should be in K And R.
You can look at the managers.
But essentially,
it lets me say, actually,
I know I asked for 5 inch worth of space,
but I actually need 10 or actually need,
and we can use that structure to.
Build these kind of automatically expanding.
Blocks of memory,
in contrast,
if I say into a bracket 5,
that's it.
You can't say, oh,
just kidding,
I need five more,
can't do it,
you will be able to extend this array. Okay.
Questions about stack versus heap. Yeah.
So when you have a stock gray,
only if the thoughtful review of variable at the idc,
it's impossible to say.
It is possible use of a variable at the length of us that see,
what do you use,
the variable does that like in the that was like 5.
And then I wanted to give our. Oh,
so you're asking whether it's possible to declare an array,
and instead of putting five in here,
you want to put the name of variable in here. Yes,
this is a somewhat new,
and when we say somewhat new,
and see with me and 99,
which is not at all someone new in something.
But it was an addition to the language after after sea happened,
which is that we.
And declare an array with a variable length here.
That doesn't mean we can resize it right,
even if the variable changes,
the array will not resize.
It'll be whatever size it was when you declared it. Well,
actually see an example of this showing up later in today's lecture.
So yes,
that is possible. Entails. Okay. Great.
So let's get into the actual actual coding part to do to do the first part of this.
The code,
as less as it was last time,
is in the.
Is in the class directory.
If you want to follow along,
that's fine.
If you want to.
Just watch me do it.
That's also fine.
I have my normal 2 terminal sign up here.
So let me just let me just get into it.
We have two programs that we will be working on today.
One of them is a pretty short program called rev doc.
Which I'm going to use to show off our reference.
And then the bulk of the lecture will be in the eastern doc file.
So let's start off with redraft on sea.
I'll come over here.
I'll put it up in the editor.
And you can see.
What's happening in this code?
Let me walk you through briefly what I'm trying to show here.
We have a function called change. And.
What we'd like this function to do is that we would like it to take a parameter into x.
And we would like it to change the value of x.
So that.
When we come back to Maine?
The value of,
in this case.
You know,
the value has been increased by 10.
In this case,
we start with variable numb,
which starts with the value,
100 of 7.
And we call change of numb.
And we were.
Who are hoping that after calling change,
the value of numbed down here will be 1 17.
So that's what we'd like to have it. Uh.
You may.
Get a sense from how I am presenting this,
that is not what will actually happen.
So I've already made,
as it turns out,
so I'll just go ahead and run it.
But what all you can really make again,
just in case.
It also studies up to date,
which means that I did already make it.
So that's great.
And sure enough,
we see that it didn't work.
Not the way we wanted.
So inside of change,
after we call after we say x plus equals 10.
We do not get,
we do get the updated value.
But then once change returns.
The value has gone back to 107.
You probably saw this.
It's got to go out.
There are definitely some pizza posts talking about pass by reference versus passed by value.
And that's exactly what we're getting at here,
which is that in c,
just like I can see the source in java,
at least by default.
Variables are passed by value.
What does it mean to pass a variable by value?
It means that when I call change of numb down here.
The value of num.
107 is copied into x.
So that changes 2 x.
Will have no effect.
On the.
On the calling functions. Variable. No.
So what do we do? Well,
like you said,
and see,
people's plus,
we have some special syntax for this,
no such luck.
And see.
We've got to do it. With. Pointers.
So how does that look?
And I'll just kind of show you what the changes are.
And we can kind of talk about.
Why this,
what this is,
what we're getting at.
So far,
with me.
Rather than cold change of numb,
which will pass the actual value,
1 0 7 2.
The change function.
I want to give the change function.
The address I want to say,
rather than here's the number,
go do something with it.
I want to say,
here is a location where one of my variables is.
You can read the number out of that location.
Or you can.
Go to that location and change that value.
And the way I'll do that is.
Through an percent on it.
So change of address,
of numb.
Will now give.
The change function.
A pointer 2.
So this passes and address,
which means that the parameter is of type in star,
it's a pointer to the,
I'm going to rename it to pee,
to remind myself that this is not just some. Variable,
some integer variable anymore,
this really is a pointer.
And now I can use this pointer to read and write.
The value of the end.
Now in c plus, plus,
that might have been it,
we just changed the prototype.
And that was.
We were done.
Not quite anymore in c.
We have to actually be explicit about the fact that we have changed.
With the fact that we have changed from taking in an integer to taking an instore.
Which means that everywhere in the code for change,
I need to.
P reference, P.
In order to read or write the value,
so here I was a star.
P plus equals 10.
Which means,
follow this pointer.
Go to the integer that's at the other end of that arrow. And.
Change that number to be 10 larger. Likewise.
When I print.
Same rule applies.
Start p. Save.
I'll make.
Only going to make ref,
I guess.
It's a matter. Yeah,
I only make ref because I don't want one of those sister right now. Um.
I can run it,
and we see that it did work.
So just an example of how we can use pointers to do something that c plus plus gave us syntax for also just a good opportunity to review stars and ampersands. Any.
If it's about this,
any questions about.
These changes. Right.
So this might come up in assignment, 1,
in case you found yourself in a situation where you said, oh,
you know,
I'd really love to.
I really love to have a function that returns two different values.
Maybe I want a function that returns an int,
but also updates another another variable.
This is,
how are you going to do it?
You can pass the foyer. Right. Go.
In that case,
let's move on to the to the bigger example for today.
I'll pull up sea stern, Dsi. Okay. This.
Welcome code is there's quite a lot of code going on in here.
It's probably one of the biggest examples that we've looked at.
So I'll go through a piece by piece.
I'll talk you through what,
what the core ideas are,
and then will explore each of these functions. In. Turn.
So first,
let me go over the the main function. Okay.
You can see here,
so what we're going to do,
a bunch of stuff with these strings. Hopefully, you.
You are feeling all right about c strings from lab,
if you're not feeling totally.
On on board,
yet that's ok.
There's plenty of time to build in those filling those gaps today.
But also just.
So some more time doing,
doing some string examples.
So here you can see,
I've declared three different. Strings.
And I've declared them in different ways.
I have one that is declared on the stack.
Cleverly named stack,
I've won those declared,
with malec on heap cleverly named.
And I have one which I initialize down here to be a string,
constant or string literal.
They probably going to use them interchangeably.
And so this is, yeah. And.
Our goal for this function.
For this program is that we would like to take each of these strings,
which each have kind of wacky capitalization.
And I would like to.
Explore different ways of converting the string.
To lower case.
So I've got six different variants of lower case.
That we can look at here.
And so what we're going to do is we're going to call lower case on each of these.
Each of these strings,
and then we're going to print out the value to see how it goes. Yeah.
So let me go up the orkids 1.
Don't play,
I want to start. Scrolling,
and I left all the good comments in here explaining what each of these functions does,
in case you want to look back in this code later,
I'll talk you through some of the some of the bugs and stuff,
but we'll talk through it all together. Today.
Here's the first variant.
This is lower case. One. Well,
what are we doing here? Is. Well,
here's the simplest way we could possibly convert a string to lower case.
You got a care star. Ok.
Go through all the characters,
changed them. All.
The lower case.
This is.
Pretty nice,
there's no memory allocation,
there's no copying.
There's no,
it's a super efficient. Process. And.
It has the.
Up or downside,
depending on.
What your situation is of changing the string in place.
So whatever the original string was.
It will.
That string will now be lower. Case. Okay. Sure,
the code you have.
Oh good question, yeah. So,
so you're asking if it's necessary to return to S T R here.
Seeing as we didn't change it,
it is not, however,
because all the some of the leaner ones do you need to make a coffee?
That's that's kind of why that's there.
There are a couple of, yeah,
so there are a couple of places where the return is pretty much unimportant.
I'm just keeping the prototypes of the same.
But that's a great point that,
in this case,
the return value is totally pointless.
And they also want a code.
So click thing to draw your attention to.
We've got the full loop from I equals 0 up to,
but not including Stirling,
pretty standard string loop.
Maybe there's some. Yeah. Okay.
All right,
let's try it.
So I'm running this function on stack,
on heap and on constant. Right.
That's all we had down there,
in name.
Remind you of that.
I want to keep jumping around too much. But. Stack,
he constant call goes. Right,
so I don't have to,
I don't think I have to make.
Because I think I've already done great.
I'm going to run, see, Sir. All. Ok.
So we get a say fault. Ha, now,
I mean,
I wrote this 100 100 line piece of code,
like what I do. Well.
Maybe your first reaction,
which would be a pretty good one if this was your first reaction.
Should be proud of yourself is dropping gnv,
and see if we can get gene b to tell us where exactly we sifted. So,
here we are.
Ng B I can run the program.
No arguments,
and we see that we stated inside of lower case,
one that seems okay.
Sure on the string, junior. Now,
if you can't remember.
What the string junior was,
whether that was stack heat or constant,
we could use.
So we're in lower case, one,
if I do a back trace.
We can see that.
Lower case,
one was called by name.
So if I want to go back and look at what line of vein called lowercase 1,
I can use the up command.
To bring me up here and here,
I see that the problem was in calling lower case,
one on constant.
So presumably we got through the stack, one,
we got through the heap, one,
and now we're stuck at this one.
The reason being string constants cannot be changed.
So you had care.
Our constant equals equals junior.
And that will work.
And we can use. Constant,
just like any other string,
except we cannot change the characters inside of it. Okay.
And the reason for that is,
you might ask,
where is it stored?
Is a store in the sack,
is a story of the heap,
because I thought I could change both of those.
It's not starting either room,
it stored somewhere else called read only data.
That just happens to be the place where you put string constants.
And that place happens to be read only because. Because.
You probably don't want to be changing these strings.
Ostensibly anyway,
because that could actually have weird impacts on the rest of your program. So.
The operating system just says no.
Forget it. Now,
maybe a quick note here about the seal.
Up until now we've been talking about sex. Volt,
as if it were this, like, oh,
you access memory that didn't exist.
You do reference, null.
You do referenced garbage. Memory, initialized, variable.
This spot is a slightly different one.
Which is that.
You'll notice that the string is totally valid.
The memory for scr is entirely there,
and we can be reading from it.
The secret is happening, actually,
because of the equal sign,
because we're trying to assign back to it.
So as I need not just be invalid memory,
it could also be.
The memory is not writable,
but you tried to write to it.
And no,
you can't really tell the difference.
Questions that is so far.
All right,
so I will comment out.
I'll comment out result 3,
so it doesn't say vault,
just so I can show you the rest of it. Obviously,
all three equals just so I can have something in print,
out empty string.
Or make.
There are a couple of warnings.
You'll notice these warnings are happening in lower case,
2 and 3.
So I will get to those warnings in a moment.
But our run sister,
and we can see that without the constant thing.
So I've done nothing for the constant.
But without that one,
the stack and the heap.
Both work.
Very important thing to note from this.
This line, though.
Stack and heap themselves have both been changed.
So it's not just that the result is lower case,
but also the original strings as well.
This should not surprise you.
We talked about.
This being our goal of modifying the strings in place.
Questions about lower case. One. Alright. Ok,
let me switch two lower case too,
and I just have to remember to go all the way down and do this.
Or I can use clever tricks. But. Okay.
So if.
I'll go down to.
To access characters in the constant state by seeing,
like constant bracket,
one or two, like,
is it the same in that? Yes,
it is absolutely so you're asking.
So the question is,
can we use by the scp is asked me to repeat the question.
So that's why I'm doing that,
not because of anything else. The.
You can't absolutely down here say constant bracket I,
it works exactly the same way as any other care star.
In fact,
looking at the declarations for heat and constant,
if you know.
I wouldn't be able to tell the difference,
they both work like care stars.
They both have array indexing.
It's just that you cannot say constant bracket.
1 equals some character and expected to work.
All right,
so here's the case too.
I've pretty much point out the buggy in, ok,
so we decide,
all right. Well.
Then loris, 1,
that was pretty nice to work for stack and to work for heat,
but I would kind of like it to work for constant.
I also,
maybe don't want to overwrite the original variable.
I don't want to modify the string in place.
So here is an example.
Here is a tenth number one.
At allocating.
A new string.
And writing our characters to it.
What's the problem,
will we see her right there?
We did not initialize that pointer.
Now we've gone over the initializing of the pointer,
the uninitiated pointer,
a couple of times already.
Maybe we can start to expect what's happening here,
but I did a lot of it on the slides.
I want to actually show you what that looks like in reality,
and how we might drill down into this.
One thing I do want to know about lower case 2 that is different,
about lower case.
One is noticed.
The four loop.
We're not going less than sterling anymore,
we're going less than or equal.
Can anyone of you explain why anyone have. Inside.
Life now that we are copying the string out,
we need to go less than or equal to 0,
but that we need to talk to you as well.
We need to copy the, yeah,
the no terminator. Exactly.
So in it.
So imagine I'm working with the string. Leland.
Which has stirred line of 6.
I would like to copy the L E A L A N G.
And then there is this other.
The terminate the null terminator,
in my string,
the backslash. 0.
Which indicates that.
The string is.
Either that we have reached the end of the string when we need to remember to copy that as well.
So a very easy mistake to forget this kind of off by one, Stirling,
off by one errors in our 4 loop or later you'll see in our allocations.
Watch that.
So in this case,
less than or equal. Okay.
So what's going to happen? Well,
I won't,
I won't,
I won't.
Built suspense for too much longer.
Since we did,
we have already seen this.
I did change to lower case 2,
so that's nice. Ok,
we even get the warning.
So now I'll point out the warning that, hey,
coffee is used on initialized.
Totally unequivocal message here that this is definitely not going to work out.
We can run, Sir.
We say, fault.
Not too surprising, right.
Identifies variable garbage memory.
Been there.
I can run it.
And I can see that I'm kind of stuck in here.
In just trying to work with Leland. And.
I can print out. I.
And we can see that we weren't even able to assign copy. Eye.
Equals to lower their. I.
A quick note about working with g.
Db here.
Is that you might think,
oh well,
I wonder what copy is actually right now.
And you might try to print out copy.
And you get this kind of annoying message that says that the value is optimized out.
That has to do with how we are compiling our programs this quarter.
The general rule of thumb,
I can give you here is that if the compiler prints out a warning especially that, hey,
you're doing something wrong.
Then there's also kind of a good chance that gdp might not be able to follow through on everything that.
You try to print out,
because once the compiler realizes that that variable is uninitiated,
it kind of gets to do whatever it wants.
But let's say you couldn't tell,
let's say you had no idea that the variables on initialize,
you're still not seeing it.
You have this big block of code, and,
you know,
and now we're getting this optimized out message. One,
the world would do,
what do we do? Well,
when it comes to memory issues,
when it comes to something like a segfault or just crazy random garbage being printed out,
or anything that suggests that it might be memory related.
Another really awesome tool that we can use as Val gin.
And you've already played with vga,
Simon 0,
but now we're using it,
looking at it on the,
I can't text date on the developer side,
on the programmer side. So,
I'll run.
Thou grinder.
Does our Easter.
And here we get.
The error that we expect,
it actually downgrade itself actually tells us that the program is going to say fault, but.
Here we get a very nice message.
So a lucky through this report,
and we'll show a couple more just to make sure we're all kind of on the same page.
How about ground reports are read?
Dogen tells us that we have an invalid right of size 1.
So we tried to write online 32.
We tried to write something.
Let me look at 32.
Real quick.
My 32,
we try to write to copy bracket eye. Right.
So we have an invalid right of size. 1.
Now where were we trying to write you?
We were trying to write to address 0 x 0.
You'll start to recognize 0 x 0 as being no.
Did it have to be, no. No,
of course not,
it was not initialized.
It could have been.
It could have been any variable.
Or any address.
But it happened to be xerox 0.
And vulgar and conveniently tells us that, hey,
guess what?
Zyxel isn't allocated anywhere.
So that's just not going to work out.
And therefore,
the program would.
Questions about.
Var message orcas, too. So, ok,
I guess it's pretty.
Pretty obvious that, hey,
I have an initialized variable.
We've done it a few times,
we're just going to keep harping on the same point that initialize variable bad.
All right.
It's got a lucky three. Yep,
go for it. Delivered.
If we didn't want you.
Fellow of the specific. I.
What movie.
No argument from orkids 2,
we got poppy right,
and our goal is to make a copy of the string ups. Spoilers,
I guess our goal is to make a copy of that string. Unfortunately,
here we've only declared a pointer.
We haven't allocated any memory for the copy of that string. So.
We're going to need to allocate memories somehow.
Either that's going to come on a stack,
or it's going to come on a heap. But.
I guess I could,
you know,
or I can initialize this pointer to something else. But.
My intention was to actually make a new copy of.
The string.
Then that's going to have to come from some kind of an allocation.
I'm going to need to use a stack array,
and we need to use a heap array,
and we'll see both of those.
In the next couple of examples. Okay. Yep.
Harm do so briefly mentioned in the comment.
But essentially.
What we are saying,
from pretty much from here on out,
is that we would like.
Is that we are basically promising the color of our function that we will not be changing?
The characters in S T R.
So by saying cons care star,
this is a pretty minor thing.
We're not going to make a big deal out of it,
but every so often you'll see a warning that tells you that your discarding.
Which basically means that if your function.
Has no intention of changing the characters.
In S T R.
You should declare sdr to be conned.
Care star.
To say I will not change it.
Don't worry.
You can pass me a constant.
So yeah,
cool else. Five.
I thought,
but that kind of business is like a really reason why you have to do that,
because it seems like it's ready, static, right?
You can't change.
What do you like it? Well,
you can't change your thinking about the variable confidence here.
So yeah,
I should be clear that.
I could,
so first of all, yes, 4,
a string constant for an actual string content.
Putting contest will help will get the compiler to help us out.
I won't do it because it generates some warnings, but.
If I actually wrote constant in here,
then it will get the compiler's attention.
When I try to pass something like constant to lowercase one,
which actually does try to change the character. Then,
rather than sifting,
the compiler will say,
you pass me a can't scare star and you meant to.
And in dysfunction wants to care,
start and wants to change it.
How is just be a warning?
But that's better.
The other aspect is that cost applies not just to these string literal,
it applies not just to junior.
So in this case we're passing stack and heap also,
as cons care star,
which is merely telling us as we write main,
that lowercase two will not change the characters in stack.
And it will not change the characters in heap.
So the concept is still relevant for just kind of almost a documentation purposes,
of saying that we won't change that. Ok.
All right.
Let's go,
lis 3.
And then,
by the way,
I'm using control v for visual mode. Or.
If there was a missing what you take away,
the ones in front of care. Oh,
it wasn't there before.
And I added a sort of to make a point,
but I guess the problem is, ok,
I can put it in,
and maybe I will.
Problem is,
I haven't.
Oh no,
I'm not. Stop. Sorry.
If I put the constant,
there are a couple of functions.
There are a couple of key functions that do want to change the characters.
And if I put the there,
then I'll start getting warnings that say you have got a constant. Or.
So I'm not going to put it in,
just so I don't get flooded with warnings,
but you are welcome to try it,
just to see what the warnings look like because there's a really good chance you will run into them on at least one of your assignments.
So just just just a heads up about the concept. Yep.
For lower case one,
the return was redundant. Because.
For lower case one,
you'll notice that all we did was return. Tr.
And scr was just the thing that was passed in.
So we're returning exactly the same pointer as we.
As we took in,
we saw when we ran it,
and I guess I've changed it a little too much by now,
but we saw,
we read it. That.
Stack and result,
one were both of the lower case string that heap and result.
Two were both York string.
So in that case,
we didn't really need to look at the return value since this function,
lowercase one was just going to change the characters.
Directly inside of S T R. Anyway. Right,
but for lower case 2 and onward,
it is no longer redundant.
It now doesn't matter,
because we are returning a copy. Okay. Everybody, everywhere.
Anything else. Cool,
let's go, lt. 3.
Here I have declared,
so this is one of the few ways I could allocate memory for.
My copy,
so I realized, oh,
on initialized pointer,
bad news.
K sounds good. Well,
Michael said whenever there was,
whenever I could use the sack,
I should use the stack.
So here we go.
I'll declare this copy should be a variable on the stack,
in array on the stack I've allocated it to be Sir line,
plus one to have enough space.
And I've copied all the characters into it all that should be super legit.
But what happens when I return?
What happens to this variable? Well,
the array goes.
Goes away, quote, unquote.
All I mean.
Let's let me let me turn this into a kind of an interactive thing.
Let's take to stadium.
Around talk to the person next to you,
if you,
if you can guess,
you're all kind of spread out,
but not the person next to you.
What do you think is going to happen if you are going long,
don't run it,
you know,
just make a prediction first.
And then if you want to run it because you can't,
you can't hold your sense for that long.
That's fine,
but make a prediction first.
What's going to happen?
You take ten more seconds.
While you guys stop yourself a 10 seconds,
that's amazing. Okay,
let's regroup.
That's pretty awesome, actually.
Most of you did actually stop yourself with a 10 seconds ago.
Good times, any, any,
any suggestions?
What's going to happen?
Just fill out. Hello,
it's my thing,
but if you just go,
I think it's probably going to work because the stuff you role of, actually.
Over the information that we strike the stone.
So you think if we got from Bali and watching everything you think it's going to work,
just basically by sheer luck. Great, okay.
I'm not sure there was somebody back there.
I'm thinking about all the other predictions.
You don't think you have.
For those. Ok, ok,
so you just,
you just don't think it's going to get lucky,
you think?
Maybe it could have worked,
but no.
But no deal.
Anything else. Sure,
free snacks for her. Sorry. Yes. Up. Ah, good, good.
So we're returning a character array,
but you're saying,
we've heard or returning to carrera,
but we promised to return to care. Star.
Arrays and pointers.
Kind of work the same way.
And even though this arrays on the stack,
it actually still works like a pointer. Anyway,
so we're actually, yeah,
so that's a good question.
We're actually not going to get any kind of warnings,
or we're going to get a type mismatch on that.
We can treat this carreras if it were a care star.
And therefore,
we can return it that way. Okay.
It is still. Yes,
we're returning a pointer to the first character of the string,
but we all,
that's always what a string is.
A care star is always a pointer to the first character.
And I mean,
so I guess it was kind of printing that out before,
but ok. Yeah,
so it's possible that something weird will happen when we try to assign the values,
and then maybe we lose characters,
or just like we only.
Managed to keep one of them, but,
but certainly in terms of the actual printing,
we will print out the entire will,
print out as many characters as we can read from that. Kershaw,
because we are,
because that's what printer with percent s means,
is we're just going to go go until he reached the terminator.
Any other predictions. So,
since so far.
Great thing is being done in may malfunction. Yet, look,
history in the country was communicated this back up when,
when for the French they go to die,
like I started to die and then resolve any hundred segments.
It's going to print. Whatever.
It departs stuff. Ok, yeah, so,
so your comment is that you think that once the once the function returns,
the number is kind of gone.
So we'll be pointing to some memory that's maybe not valid or something,
but part of the question,
so what do you think is going to be in Emory?
Are you saying we have no idea which should be garbage? Ok,
so you think you maybe some garbage.
All right,
just right.
I fit right through all good.
I'll go here or remember to make. No,
we do get the morning.
Continue to draw your attention to the warning.
I'm not fixing them deliberately in our example,
but we do get a warning that says, hey, gosh,
you're running for local variable.
There's kind of weird,
I don't think that's going to work for what you wanted to.
This is very interesting, right?
So we've got. Result,
one is junior results,
his junior is all three,
his junior. Ha, ok,
time for some gdv.
Let's run.
So now,
if I just run,
and of course,
it's just going to go all the way through.
There was no seg fault,
there was no crash.
The interactive be garbage.
Even so,
now I'm going to need to put a break point to,
where shall I put a break point? Well,
let me take a quick look at my code.
I'll go ahead and put a break point on this line. Actually,
going to be 1 0 6. Now,
just a quick note about gdb debugging,
something that maybe catch you off guard when you see a line.
Ng db that says.
1 o six, blah, blah.
This line has not executed yet.
So I cannot look at what is.
In result, one.
It has not been initialized.
Only after I go next.
Will it run that particular line? Okay.
So let's just go and try for your product.
Or all one.
It worked. Right,
it's too fine.
We went, yeah, ok, cool.
Let's go next.
For now, result. Do.
Hunting or two.
A nice.
So I don't know,
was pronto freaking out. Ferrall, one, again.
And now we see the problem.
And here,
in fact,
so the way this output goes is that this number is the address.
From our result,
one result,
two and 3 are all care stars.
So their pointers,
two characters.
This is the address is actually from the diagram in the box.
Of the pointer.
Noticed they are exactly the same. So.
I think you were all on the right track when looking at that stack memory and saying, hey,
it's going to be.
The allocated,
I won't go all the way through because you can predict what's going to happen with the next one.
But we saw what's going to happen.
You were all pretty much on the right track with this idea that this memory,
the memory for copy,
will be de allocated at the end of this function.
Or I'm sorry,
it will be.
It won't be technically our memory anymore. But.
What does that mean for the actual memory?
Does it get zeroed out? No.
Does it magically turn into garbage, no.
Are we getting lucky that it's still kind of containing stuff?
I mean,
kind of. Like,
it's certainly.
If we did add more stuff to this program,
we might not even be able to get the Leland and the Stanford after a couple more calls. But.
The issue is that so imagine maybe going to have to just kind of gesture about this.
So imagine we ve got this function lower case 3,
and it's got some space for the array.
And we call it with Leland,
and it says, ok,
I'll fill that array with the orchestrating Leland then.
Function goes away,
lower case returns.
We say, great.
You don't need this member anymore.
Now we call lower case story with Stanford.
You start up lowercase 3.
And it says, hey,
I need some memory.
Or you think it's going to get the memory,
probably from the same place.
Because that.
Old memory isn't being used anymore.
Because it's.
Not valid number anymore,
so here have that memory again. Right,
but now we still kept a pointer in Maine. Namely, result.
One was pointing to.
That copy.
And the contents that that copy has now changed. Yep.
It sounds like if we've run once a port,
like if we had just run at one time.
That memory,
like you said,
would have been not safe,
but stuck with.
So be there. But.
If we read a different function,
wouldn't go back to that memory and rewrite that,
or depending on where you are in the code,
would it go to different rape? Yes.
So you're saying, ok.
So if I ran this thing on only one variable,
and let's say,
on only stack,
it'll work.
You're right.
But then,
if we did something between calling lower case 3 and the print have.
We called some other function that also had an array.
Then it is very much possible,
in fact,
extremely likely,
that other function will use exactly that same space.
That we are using for copy.
And so then when you go and print out the copy,
or when you go in for an out result, one,
it'll still not work.
So yes we are.
We are extremely fragile here.
The fact that we're getting any result of all that is useful is extremely dependent on the particular order of calls. Yep,
so coincidence. That.
The same address for one of them,
is it a coincidence?
Will find out in 5 weeks.
At the answer is,
absolutely not.
It is because this function happens to know exactly how much memory it wants to allocate,
and it will always allocate that same amount of memory. And.
Since all of all three calls are happening in Maine.
Just that the whole call sequence look so similar. That.
They just. Inevitably,
line up. But. So,
I mean,
it's not,
it's not,
I guess it's not coincidental in the sense that it's absolutely possible that with a couple of different calls from a couple of,
for example,
of how using. Sorry,
I love.
For this set of calls for this kind of method,
we're not getting lucky that the machine happened to lay it out in that way.
We can actually look at the code and see it being laid out over each other. Sorry,
because I sense. Yeah.
So the author,
I'm sorry,
anything else. Ok. So,
all right.
This is a classic case of not being able to use.
The stack.
We see that we cannot return.
A pointer to our stack allocated memory.
And in particular,
the key point from the slide at the beginning,
for why we need to use the heap is that we need to control when this memory is allocated and de allocated.
Or specifically, dealing.
So guess we got to use the heap.
All right, fine,
here's our character.
Our coffee will maleate will copy stuff in or returning.
Little bug here.
We forgot the plus one.
On the R line, very,
very common. Bug.
And up here we got Stirling,
plus one,
when we allocated the array on the stack down here.
We do not.
We do not say a sterile line.
Plus one. So, ok. It.
Make a prediction,
what's going to happen?
And it will throw in something else with that,
which is all right.
What is it actually going to print out?
And then is like, yeah,
so if you think it works for you,
think it doesn't work like,
how are we going to find this error?
Doesn't take a minute.
I'll come back to this code shows that I'm clicking.
85 more seconds,
and was seconds.
All righty already, let's.
Let's come back here. Ok,
what do we think?
What is going to happen when it,
when we do it? Later.
There's space, right?
So you're thinking of the vagrant. Yeah,
the vulgar,
maybe blue starts talking an invalid, right?
What do you think the programs are you going to do? So.
I see that you just don't.
We try to write down. Truly,
for an element of unpredictability here.
And any other guesses.
I'm going to give. Hello,
redo the qubits into the computer.
Is I won't get over beers,
but when you get to the friends that function,
they'll try to read them.
They will do the first fail.
Read the characters such allocated correctly.
But then since there's no no term measure,
go beyond that,
and we'll try to read random memory. Ok. So, yeah.
So that's that's that's neat.
You're saying, ok,
it's going to print out the characters.
Those are fine because of the memory was allocated for those correctly,
but then maybe something's going to happen to that. Null, terminator.
And if we lose the old terminators and principal,
just keep going.
And I will keep printed out garbage.
Gave me everything else.
They want to guess,
this is a work.
You ran in.
I think,
because the. I. Yeah,
it's just going to go over.
One array off of coffee, yes, coffee, yes.
But there.
Because results.
Once you return it.
You appointed a receives companies not going to know how many.
Our ourselves coffee was close to be.
Any of Chicago reader told the normal flip and.
So you're thinking,
so you're saying, well,
it's going to copy the old terminator.
And so when we return, result,
I mean,
print out is just going to keep reading terminator.
We herded copy one there,
and you think it's going to work.
Tell me,
does it bother you that you went off one on the one off the end so you could have,
like deleted something?
But if we didn't,
we got lucky.
So you're saying, oh,
you know,
it's possible to that.
Zero was like,
I was super important piece of memory that we just overwrote.
If it wasn't.
Then maybe we get away with it. Cool, great.
Let's try.
I will actually get out of where I'm like, oh,
I will get out of duty.
Because I don't think I need it here. Make. Okay.
And we'll run it. Works.
Work great. Meet. Okay,
we're done. Fine,
wrote out one off the end.
Who cares. Now,
let's not do that.
All right,
so we are not seeing the symptom.
We're not seeing any any bug here.
We know that there are some memory issue going on there,
and we're not seeing it.
And exactly,
we wrote,
we did right,
the old terminator, like.
We are absolutely writing.
00:55:52,900 -->
That no terminator. 2,
1301
1 off the end of this allocated array.
And the right.
Went through,
there is certainly a chance that the right wool go through that there wasn't actually any memory at that point,
and therefore we would stay fault or something.
That does not,
that's pretty rare.
That going one of the end will really cause that problem.
But sometimes,
what happen.
You do this a few times with a few different strings for strings.
It'll you'll definitely hit it once.
In this case,
we got lucky,
rotted and null,
terminator to,
I hope he didn't need that memory,
and now.
We're we're able to get away with it.
On the terminal.
What you,
what Belgrade has to say.
In a response to us just trampling over random memory in the heap.
And sure enough,
we've got lots of errors.
We also get leaks,
the leaks.
Of course,
the errors are a lot more important than memory leaks.
When we look at vine.
So certainly if you see these errors,
don't think,
oh well,
I also have leaks,
radiation leaks. First, no, no.
Let's look at these errors and try to understand them first.
And sure enough,
we see,
I'll go all the way to the top.
And sure enough,
we do see this invalid right of size 1.
Where is it?
It's in lurches that see,
you can bet that's the coffee bracket eye line.
Here is the part that is. Actually, super,
super useful at about grams can give us.
That even something like gdd.
Will not.
Vaughn is telling us you made an invalid,
right of size 1.
Which means one character.
Where did you try to write you?
You tried to write to this address.
I don't know what that address means, but.
That address is 0 B after a block of size 6 allocated.
This bottom section is telling us where that memory was allocated,
so it's saying the memory was allocated by a call to Malek.
Which happened in loris 4?
So how do we interpret this whole thing?
We tried to write to some memory.
The memory.
There was a block of memory that was allocated in case for using Malek.
To be size 6 and makes sense,
that's a And D or L E L A D.
And you wrote 0 B after it,
meaning immediately after it.
So we get this memory error.
It tells us.
And Vaughan will let us get away with it,
which is really,
really darn nice. Because.
Now we can fix this issue.
We can make sure that our code.
Runs cleanly,
and we will not.
Accidentally tripped that random seg fault.
In a few thousand cases. Yep.
Your advice after.
Funny is that the no.
From me,
that's a good question. Yeah,
the zero bites does not mean the null terminator.
What we're saying is this is.
How far after the block of 6?
So if I have a block of 6,
and then I've got,
and let's say so after the block of 6,
I skipped four.
And I write over here.
Then it'll say you went 4 B after the block of 6.
That makes sense,
but I wrote immediately after the block of 6,
so I'm 0 B after this block.
Why not? By.
Because we count from 0,
I guess. Yeah,
there's not a good,
maybe you do like,
I think it feels like this is a bite after, yeah,
you'll just have to maybe get used to the way the numbering kind of works out.
0 B after means.
Immediately after.
But that's great.
Great question.
And else. First,
how klopp can actually over it so that our biggest.
If possible.
As you get more and more so so the question is, is,
it can be erased,
or like something weird happened were like,
part of the memory still there.
And part of it's not. Yeah,
like absolutely anything can happen when you write out of bounds, right? If,
for example,
I called Malick. Again,
after I.
You know,
kept doing these rights,
one or two or three bites after,
and I just kept calling Malick and and writing out of bounds.
I'm probably going to crash malec eventually. Eventually,
I'll stump over something that Malick was using.
And I'll be Like, Yeah, Yeah,
buck stops here.
Like you're going to strike fault,
you're going to get some crazy error message.
So yes,
all variety of things can happen with these array out of bounds,
which is why algorithm is so helpful,
because you might run your program,
and you like to see either no output or rewrite some complete garbage.
So that example of the suggestion of the print f with garbage.
Coming out after the the string,
that's also possible.
And how would you even debug that?
What do you do when you see, right, like,
what does this even mean? Well, background. Grandis,
tell you. Alternator,
and what would happen with the program has to be.
So so you're saying it's the string habitat then, yeah.
So in that example,
the Alterman area gets overwritten by something else. Then, well,
all string functions,
including print,
are just going to,
I mean,
they don't know how long the string is,
otherwise they're just going to keep reading characters and printing out until they hit a null.
Which are annual terminator?
So they're just going to keep going when they see a back slash 0.
Where are we going to see you,
actually cereal? Well,
we sure hope there is one in memory somewhere to sell us. Eventually.
And the probably is.
Access real happens to be the integer zero.
We'll see that later.
So there are zeros all over the memory.
So what's actually.
The thing is,
we did put it back,
slash 0 in the four loop.
So the four leaf did go from,
I less than or equal to,
R land.
So this very last iteration of the loop,
where I,
in the case of leyland,
where I equals,
equals 6.
We're copying scr bracket. Eye.
So scr bracket 6 when scr is Leland is going to be the back slash 0.
Calling two lower on backslashes 0 gives us back,
slash 0 so we will write the backlash.
0 two copy.
It's just that.
We were writing it out of bounds,
and it's happening not to get overwritten.
That's correct. Yeah,
I mean this doesn't.
Nubian right like this?
Is it the end?
We can't just look at this code from this line and see that we went out of bounds, right?
Neither can print.
It's just going to keep reading from your pointer,
because that's what you told it to do.
So since we have a blast on this case,
after ever cried,
do you keep running like the well before the crash?
The question is,
will it ever crash the risk for the crash is if.
Copy bracket 6.
Happen to not have any memory associated with that.
We haven't hit the very,
very end of our heap.
That it might crash.
In the right,
but the print will never crash.
Assuming that the right work. Yep.
Size of care is better. Because.
Good question you're asking,
why for alec,
are we not using size of care when all the other malai was using size of something.
Care Inc is defined to always have size,
1 size of care will always, always,
always be value one,
therefore it's largely redundant to write time,
size of care here.
And so we're just going to leave it off when allocating character arrays,
but enough for any other array. Yeah.
You already ran this step until it wrote the alternator at that point of memories.
Just now quit this program and you run it again without the forum,
like if you change the form to make it just less than yeah.
So it doesn't write the determinator. Yeah.
We were just like,
oh well,
I can just keep reading,
but if you've already run the program and put a little like, oh,
remember that. Yeah.
So the question is,
will the state of memory deep kind of preserved across runs?
The answer is no.
Every time I rerun the program,
like the memory gets initialized.
From scratch, again.
And he else. Ok. There,
remember leaks here.
So I could fix this,
but rather, actually,
I can fix the plus one here you'll see that in case 5,
I want to.
But I wanted to do something a little bit different,
so I'll leave that.
I'll leave that as a bit of an exercise I mean at the plus one.
There are also leaks here for freeing the result variables. Again,
these aren't super mission critical to fix,
but they certainly would be good to fix.
So I will also leave that as something for you to try out.
Maybe I'll make a separate commit to the lecture code,
showing you how to free the memory if,
if I get a chance.
But I want to switch gears a little bit.
And talk about a different way that we could do this.
A different way we could do this.
And I want to do to do that. Slides. Okay.
So let me talk you through. Actually,
I should show you the code, right?
I should show you the code for what I'm going to try to do in slides.
And then I'm going to.
And then I went to move away from cold for a second.
So here's his five.
You can see that we did fix the plus one here.
And everything else about this coat is fine. Except.
I'm going to focus on this line for the next 15 minutes.
Which is that? What.
In addition to making this copy of memory in the heap and writing it and returning it,
so this function will totally work to that extent.
It will.
If we got rid of this line,
like the function will behave as expected,
it will give us the result.
Values have the lower case and they will be correctly allocated.
We won't have any memory errors,
we will still have some weeks that we should clean up.
But I want to focus on this.
Which is.
Maybe what we actually want to do?
Is go back and set Ssr,
the parameter that we received.
To actually point to the copy as well.
So that,
for example,
when we go back into Maine,
are variable heat.
And are variable, constant.
Are actually also pointing at the new lower case string. Okay. So.
I guess.
I'll spoil it for you now,
but you can run it on your own if you'd like.
It's not going to work that way.
And I'd like to actually draw out why it won't work that way.
The first thing I need to do is skip a bunch all slides because I have an example in here.
A worked example of lord case, 1,
I was using it as comparison,
but seeing as so,
feel free to go back to the slides again.
I will post these.
Remember to place these right after lecture.
I've worked example of how lower case one of these memory. But.
Jump straight to the orcas. 5.
So here we have the code.
I'm not having this function return the care star in the situation.
Because I don't care about that part,
I am more interested in this update line,
are more interested in is trying to make stir equals copy.
And I would like to have.
So here you can see the memory diagram.
Hopefully the pattern looks so much familiar from Monday here.
This is the local variable for Maine.
I'm only doing heap because it's just going to keep things simpler.
And what I would actually like to do is I'd like,
when this call to lower case,
5 returns that.
He will be,
will not be pointing at this Stanford string,
but would be pointing at a lower case version of that.
What about.
Diagramme things.
Contempt to here. Okay.
So we'll walk through it.
The line in blue is the line that I'm currently talking about,
whether it's I'm not super consistent about whether it's happening before or after. But. Anyway.
So here I've started to enter the lower case,
5 function.
And inside orcas, 5,
we take the parameter as tr.
And in Maine,
we pass. Keep.
So what does that mean? Well,
it means we look inside the box for heap,
and we copy it down to the to the variable S T R.
So we've got both.
Scr and heat pointing at the same block of memory here. Yeah. Ok. Sudan.
I allocate this copy of variable.
I have a couple sizes do this. Okay?
So then allocated this copy variable.
With a malec,
here's the memory for that.
The copying of the four loop should be ok, right?
We go through.
And we start filling it. Oops.
We start filling it. Everybody,
your head up to this point.
So we've gone through the entire for loop.
We have this copy variable that is pointing at this new block of memory.
Which now stores the lowercase value?
Everybody who helped this point.
Hand going a little fast.
Now we get to store equals copy.
What does this do? Well,
there's no d reference.
This is exactly like the exercises we were doing on Monday.
Stir equals copy means that we look at.
We look at,
we sorry,
we started a coffee,
we look at copy,
we say,
what's in the box, 2050. Ok,
take the stick it in that box.
All right.
So now stir and copy are both pointing at this new string.
But oh shoot,
what happens when we return?
And he did not change.
So this is not.
A successful way. Two, two.
Update the pointer heap.
Questions about lowercase. 5.
Ah good question. So, well,
so well they're being an error.
Will anything go wrong?
Because we didn't. Freedom, memory,
you can clearly see it right there.
Oh my gosh,
it's not freed.
That's a memory leak.
So yes, Val,
rain will tell us that there is a leak. That's.
That's a bummer.
It's especially a bummer,
because we didn't even use this memory. But.
It's not going to crash,
it's not going to be a memory error.
Just kind of be like, hey, hey,
by the way.
By the way.
Other questions.
Around your own,
over time.
Book to the fact that there is a god to you first. Oh, sorry. Okay,
good story. Fair, yeah. Sorry. So. Here,
let me go back to 2.
Here I'm going to start from just kind of around here. Ok.
So I got this variable. Called, heh, yeah, ok,
sorry about that.
So this is a local heat is a local variable in Maine,
which is pointing at.
This heap copy of.
The String Stanford. Right?
And then.
Now I've got these two variables.
In lower case,
5 S.
Tr and copy.
And here I have S T R pointing to the same block of memory that this other,
this variable up here,
is pointing to.
And copy is pointing to this new one. Okay. So.
The filling of the four loop shouldn't not be super surprising,
how that,
how that goes down.
Go into scr bracket eye,
so that means follow this pointer.
From scr into this block,
and then we start,
we go two lower,
and we copy down here.
And then when I get to,
as tour equals copy.
We're really only looking at these two variables down here. Where.
I make scr.
Point to where coffee is point point two,
where a copy is.
Is pointing so they go down there.
But ando has no effect.
Once I've returned,
this local variable is called heap is still pointing at the original string.
All right.
Let me show you how to fix this.
We already talked about.
The case of passing.
An inch by reference,
by adding a star.
And you might think, hey,
this is already a care star.
I already have my stars.
I shouldn't need to add any more. Stars,
I shouldn't need to do anything special now and can't I just pass by reference.
And that's part of the reason that I have the diagram for lower case one as a comparison to this next one.
So so check that out if you get a chance.
But here.
I'm going to go to lower to 6.
This is in the in the code.
And like I said,
the slides are up. But.
What we need to do in order to make this work.
Is we need to pass?
A pointer to what we want to have happen is we want this box to be changed.
What we want to,
what we wanted,
this box to point to. Yeah.
So in order to change this box.
In order to change this variable,
we need to pass the address of this variable.
Which is why,
down here,
in lower case, 6,
we passed the address.
Of the heat variable.
So what does that look like?
Up here we enter lower case 6.
I have a slightly different name for it,
because I want to remind myself that this is a kind of double star and not a care star.
It's stir p and what stir he is going to do is it's going to be assigned a value ampersand.
Of heap.
So what is the address that this box is located at? Well,
it is 9100. Right.
So we'll stick 9100 in this box.
And draw the arrow. Now,
do you see the care double star?
We've got,
we start in this box,
we can follow the arrow up to here.
To get to our care. Star.
And then we can follow another arrow coming out of that.
To get to our actual characters. Okay. Questions.
All right,
so now I do the copy part.
That's all the same.
I fill in Stanford.
You'll notice that I have to.
Remember to keep dee referencing,
stir throughout my entire block of code.
So just watch that,
because now I can't just use stir peas.
A pointer to some characters.
It's not.
This thing is not pointing this.
I have to follow that arrow at least once,
and then I can use the bracket notation to read stuff out of this array.
So now what happens now I've got starter pee equals. Copy.
And so what we do is, ok,
let's let's walk through this,
just like we did for on Monday.
The right hand side is copy,
what is copy.
It's the value. 2050. Okay.
It's pointing at.
This thing,
so I take this two.
Don't follow any arrows because there's no star on the right hand side.
I take this 2, 50.
And where do I put it? Well,
I put it in star.
Stir Pete. Okay. So,
I started therapy.
I follow the arrow one time.
Because there is A D reference.
And I put the 2050 here.
So now. That.
This local variable in main,
points to.
The new memory,
so when this stuff goes away,
after the function returns,
we have successfully updated.
It's the variable. Russians.
What do you mean by linking.
Ah no,
so you're asking if we can put you cannot put an ampersand up here in a parameter line that's only c plus plus.
We need to use care.
Star star.
Anything else.
He lowered his five.
If I say, ah,
good question.
If I say in Murphy's 5 star,
S T R equals,
I won't go back to the slide.
I'm sorry,
but a star as your equal star copy.
We want to be very careful what the two types are.
Start copy here.
You can see it.
Least that copy is a care star. So,
star copy as a single character. Right.
S T R in loris 5 was also a care star,
so star sr is also a single character.
So start scr equal star coffee.
Will only copy one character. Okay,
couple things before we go,
let me get your attention for just a little bit.
Little longer. This.
Function will largely work up until this point.
There is a problem which is that we did not free this memory.
There is a bigger problem,
which I will,
I will put in the report,
which is that you cannot.
Do this.
Line ampersand, of.
A variable on a state,
just maybe,
write this down or something.
We'll come back to it in lab,
you cannot take ampersand of a stack array.
Ampersand stack does not give us a care star star.
So there is no way to get lower. Case,
6 to work. Sack.
We can only get it to work with heap.
And with constant.
So general conclusion,
there is no good lowercase function that will solve all of our problems. Loris,
1 almost totally worked.
A variant of lortie,
4 or five kind of between there would have sort of worked for making a new copy.
Murky light makes sense if we know exactly where our memory is is coming from,
and we know that we can take ampersand of it,
and that we know that we want to change the pointer. But.
Um but that sort of,
you know,
that's kind of the nature of it.
In C.
Hopefully got some experience with Gd b.
You got some stuff with more pointers.
Will come back to all of the stuff next week.
And again in lab.
Until then,
it we'll see you.
On Monday.
