ProgrammingMethodology-Lecture15.pdf
(
59 KB
)
Pobierz
Programming Methodology-Lecture15
Instructor (Mehran Sahami):
All right, welcome back to yet another fun-filled exciting
day of cs106a. This is the pivot – this is the turning point of the quarter. This is like the
pivot of the quarter. After today, it’s all downhill, because we’ve gone through just as
many days as we have left. Fairly exciting. As a matter of fact, they actually have fewer
days left, because we have like the last class is not happening and stuff.
But a few announcements before we delve into things today. The handouts from last time,
if you didn’t get the handouts from last time, namely, especially the practice midterm and
solutions to the practice midterm as well as assignment No. 4, if you didn’t get those,
they’re available in the back today. If you already got them, you don’t need to pick up
additional copies. There’s no additional handouts for today, but there are just copies of
the ones from last week.
So, again, with the practice midterm, I would very highly encourage you to take it and, if
you want, also, for the full effect, actually time yourself doing it so you get some notion
of what’s taking you longer or what’s taking less time. That will give you a diagnostic of
the kind of things you need to brush up on, so – and it’ll also give you a chance to see
what we’re really kind of expecting for the midterm, and there was a bunch of stuff on
there where it explains that the midterm is open book and open note, but closed
computer, and all the guidelines for the midterm are all explained on the first page of that
handout.
So the midterm is coming up. It’s next week, just a few days away. Tuesday, October
30th, 7:00 p.m. to 8:30 p.m. in Kresge Auditorium. If you don’t know where Kresge
Auditorium is, find out where Kresge Auditorium is. It’s your friend, it’s big, it’s bad, it’s
Kresge. It’s just a huge auditorium. If you’ve never been there before, it’s kind of
cavernous. You go in there and you go, my God, I didn’t know there was an auditorium
this big in Stanford, but there is, and it’s ours for that short period of time.
If you have a midterm conflict and you already sent me email – if you haven’t already
sent me email, you can go ahead and try, but I can’t accommodate any more requests. I
can hopefully just tell you when the alternate midterm is, but you will get an email from
me this weekend letting you know when and where the alternate midterm is, so read your
email this weekend. It’ll probably come some time in the wee hours of the night either on
Saturday or Sunday, which would be the weekend, strangely enough. So watch your
email for the alternate midterm.
If you’re an SAPD student, SAPD students have two options. If you’re a local SAPD
student, you can come in for the exam, so just come on down, 7:00 p.m. to 8:30 p.m.,
Kresge Auditorium, October 30th. Take it along with everyone else, introduce yourself,
come by, say hi. I always like to meet the SAPD students.
If you’re remote, because I know some of you are saying, not in the state of California,
and it would kind of be a stretch to ask you to fly here just to take the midterm exam, you
may take the exam remotely at your site. If you plan on taking the exam remotely at your
site, you need to send me email, and in that email you need to let me know your name as
well as your site administrator. Find out who your site administrator is if you do not
know, because your site administrator will be administering the exam to you, and I need
to know their email address.
So send me your name and email, which I’ll get when you send me email, but your site
administrator’s name and email so I can email them the exam and they can administer it
to you. And if you’re a local student, go ahead and send me email, and just say you’re
coming in locally, just so I know that you’re coming in locally and that way I can keep
track of everyone.
But extremely important for most students, if you’re a remote student and you don’t send
me email, so I have no way of contacting your site administrator, I can’t give you the
midterm, and that’s real bad times for just more reasons than you can shake a stick at, but
you’re certainly welcome to try.
And last but not least, same announcement as last time. There is a syllabus change, a very
minor syllabus change, based on just moving the discussion of arrays up by one day. So if
you’re sort of reading along in the book and you wanna know what to read for Monday,
Chapter 11 on arrays is what we’ll be covering on Monday, and if you’re not following
along in the book, you should be following along in the book, but now you know.
All right, so with that said, I wanna do just a tiny little bit of wrap up on what we talked
about, memory and this whole notion of pointers last time, and go into our next great
topic. All right? So time for our next great topic.
Well we need to do a little bit of pointers first. So the first example is something that we
talked about very briefly last time, but I wanna make sure everyone’s sort of on board
with this. So if I have our friend, the point, that we talked about last time, which is just a
class that stores two values, an x and a y that we pass into the constructor, when we do
something like this – I’ll draw it sort of over on this board.
Now I’m gonna draw it in a – actually, I’ll draw it over here, and I’ll draw it in a way that
we don’t need to worry about all the memory addresses and overhead and all that stuff,
because that sometimes makes the diagrams a little bit ugly, and so I’ll show you the very
simple, stylized version of keeping track of the stack and the heap.
So over here is the heap, over here is the stack, and what we’re gonna do is say, hey,
when we declare point p one before we have done this new, what we get is p one, which
is basically just some box on the stack. What does it hold right now? Well, until we do
this line, it holds nothing. It holds nothing that we know about, okay?
When we call this new, it says, hey, heap, get me some new memory for a point, and so
the heap kind of goes over and goes, boink, here’s some memory for a point, and it calls
the constructor and says, hey, constructor, put in the values one one. So if somewhere on
the heap we have the values one and one for our little private x and our private y
somewhere on the heap, and the value that new gives back, that we assigned to p one, is
the address of this thing.
Remember, we don’t really care about where in memory this thing’s actually stored, and
so sometimes, what we like to do is just keep track of addresses by pointer. So we know
that this is just some memory address over here, where this particularly object is actually
stored on the heap. Okay?
So now, if we do point p two – actually, let me not let – put the semicolon on yet. If we
do point p two, what we get is, on the stack, we get some new space set aside for p two
and, again, its beginning value is unknown.
Now we have a couple options. One thing we could do is say new point, and give it some
x y location, in which case we’ll get a new point on the heap and p two will point to that
new point. We could do something funky like say, hey, p two equals p one. Well all
that’s happened there is saying, take the value that’s in p one, which is a pointer over to
this location on the heap, and set p two equal to that. So if this happens to be a memory
location A A A E, this guy gets A A A E, which means, in our sort of stylistic
representation, it’s pointing to the same place. Okay? Are we sort of onboard with that? If
you’re onboard with that, nod your head. Excellente.
All right. So now if we do p two dot move, and we tell it to move by an amount like three
comma four, it says, well, in order to call the move method, I need to call the move
method using the object p two. Where does the object p two live? It looks it up through
the pointer and says, here, this is where you live. Call the move method with this
particular object. So a path to this pointer to this location in memory, and it says, move
three four, and as you saw with move last time, all move does is add those values to the
respective x and y. So p x becomes four, p y becomes five, and now that move method is
done. And, interestingly enough, what I’ve done is change not only p two, but also p one,
because they were pointing to the same place in memory. They were the same actually
point, and that’s the important thing to keep in mind.
If I set an object equal to another object like this, they are the same actual object. There’s
only one of those objects that lives somewhere, and I really have two things that are
pointing, or what we really like to refer to as referring, to that object. So often times we
refer to these things as references, okay?
Now what happens if I come along and say, point p three. I get p three over here on the
stack, right? And again – I’ll write the p three over here so as not to interfere with the
lines, and I’ll draw this line a little bit around p three so it kind of goes like that. It says, p
three, I don’t wanna interfere with you, I’m just gonna zigzag around.
P three, what value does it start with? I don’t know. It’s not pointing to an object yet,
because it hasn’t been initialized to point to an object. It hasn’t been initialized to point to
a new object. And so if I come along and say, hey, you know what p three? I just wanna
move you. Move to four comma five, because that’s where I hear the party’s at. That’s
where p one and p two are. Can you move yourself to four comma five? What happens
when I do this?
Bad times, that’s what happens. What I get – well first of all, I get a social, because a
whole bunch of people said it. But what I get is this thing. Who knows where it’s
pointing? As a matter of a fact, this pointer could, often times, be something we refer to
as a null, which means it’s not pointing to any particular object, in which case, when I try
to do this, I get what’s called a null d reference. Anyone happen to see something called a
null d reference while you were working on breakout? A few folks? Yeah, this is what
you were getting.
You had some object here that wasn’t actually pointing anywhere, and you were trying to
call a method on it that says, hey, I’m trying to call the move method on p three and p
three says, I got nothing for you, man. I got nothing here. You’re trying to move
something that doesn’t exist, so you’re trying to dereference, which means go through the
reference, it’s a funky name for go through, we say d reference, something that doesn’t
exist, or maybe this is just – has some random value that’s pointing off somewhere into
memory that you don’t know about, which isn’t actually an object. Okay?
So that’s what you wanna watch out for. This is real bad times if you actually happen to
call a method on some object which doesn’t really exist, okay?
Now the other thing that’s important about seeing these little pointers is the fact that –
remember when we talked about parameter passing? And in the days of yore, we talked
about parameter passing and we used to have this little thing where we said, hey, you
know what? If you have something like an integer, or a double, or a char, or a Boolean,
these are what we refer to as primitive types.
They were like hanging around, they’re, you know, lower on the evolutionary ladder.
They’re just what we referred to as the primitive types. When you pass primitive types as
parameters, what you are getting is a copy of the value of the primitive type, okay? What
does that mean?
It means, when I call some function, and I say, hey, function, I’m gonna pass to you a
three, like in my move method over here I passed in a three, which is an integer, what
actually happens when I make this method call is I pass a copy of the three, and so if in
this move method I try to change this value around to something else, all I’m doing is
changing the copy. I’m not changing the actual parameter that was passed in.
So if this wasn’t a value – let’s say I actually had some variables here like x and y, and up
here I had int x and y, when I make this method call, I get copies of whatever values are
in the boxes for x and y, and so if move tries to muck with x and y, all it’s changing is the
copy. It’s not changing the x and y out here, right? That’s what we talked about a long
time ago, and we talked about little Neron, who went off to France with his mom and the
Mona Lisa, and he wanted to take a hacksaw to the Mona Lisa, but the Mona Lisa, he just
got a copy, because they went to the gift shop. Yeah, primitive types, you go to the gift
shop.
Now, something funky happens though when you’re dealing with objects, okay? So when
you’re dealing with objects, they are not primitive types, they are object, and so when
you make a method call where you pass some object along, what you are actually doing
is you are passing the object – or, actually, what we refer to is the object reference. What
does that actually mean?
What that means is, let’s say I call – were to call some method over here, where I actually
pass in a point. So let’s say there’s some method over here like – well I’ll call it change
zero, which is supposed to set a point to be zero, and I pass it, p one – and this is just
some method that I wrote, and maybe the elements of a point are actually public, let’s
say, as opposed to private, so this function can actually access those things. What
happens when I pass p one?
Well what happens when I pass p one to this change zero method or this change zero
function is I’m passing where p one lives, so the parameter I actually pass is the address
that’s in here. So if the address that’s in here is, let’s say, the value a zero zero zero, I
pass, as my parameter, a zero zero zero, which means what I have is a reference, because
I have that pointer, to where p one really lives in memory, which means if this method
decides that it’s gonna muck around with p one and tell, hey, p one, move yourself to
some other location, the values that it’s gonna change when it makes method calls on the
parameter that’s passed in is at this place in memory, because it knows where this place
in memory is, right?
You wanna think about, when you’re passing objects, this is, as we talked about it before,
sort of like the mafia. When you pass around objects, you’re talking to the mafia, and
when you talk to the mafia, they know where you live, okay? They can come in and mess
with you however they want. They’re not getting a copy of you to talk. You go to the
mafia and you’re like, oh, I’m gonna send my clone along to the copy and – or my clone
along to the mafia and they’ll just talk with him. No. When you’re gonna go talk to the
like the Godfather or the Godmother, you’re not sending in your clone. You’re going in
yourself, right? And you’re like hey, Godfather, and he’s like, you come here on the day
of my daughter’s wedding. Anyway, that’s a – anyone seen the movie the Godfather? It’s
so good. If you haven’t seen it, go see it, but we won’t talk about that right now.
What we talk about is the fact that, if they know where you live, any changes you make
are not to a copy, and that’s a critical idea to know. This is what we refer to, up here, as
pass by value, because what you’re getting is just a copy of the value. You’re not getting
the reference. This is what we actually refer to as passing by object reference. So I’ll just
change the the here to a by, which means that when you pass objects around, you’re
passing a reference to the object, you know where the object lives.
Here’s a little way you can think about it. So remember our story about little Neron, who
goes to see the Mona Lisa? Think about it this way. When little Neron went to see the
Plik z chomika:
m_r_k
Inne pliki z tego folderu:
ProgrammingMethodology-Lecture01.html
(61 KB)
ProgrammingMethodology-Lecture01.pdf
(63 KB)
ProgrammingMethodology-Lecture11.html
(61 KB)
ProgrammingMethodology-Lecture04.pdf
(61 KB)
ProgrammingMethodology-Lecture03.pdf
(66 KB)
Inne foldery tego chomika:
Zgłoś jeśli
naruszono regulamin