>> JASON HIRSCHHORN: Welcome to week four, every one.
We have a busy week ahead of us.
On Monday this week and on Wednesday last week, you've been introduced to
pointers, and so hopefully that has blown your mind, but hopefully not
made you too confused.
If it has, we will go over that in section today as well as we're going
to do pointers.
We're going to go over the stack, which we've talked about in lecture
already, and we're going to hint at things to come on Wednesday and beyond
in terms of memory management.
And finally, we're going to talk a bit about the Stanford Portable Language,
which is the library you guys are using this week to implement the
problem set.
>> As I showed you all last week--
I wanted to put this slide up again, because this is a list of the
resources that are available to you CS50.
These are what we give you out of the box, and these are incredibly
important for what the next slide will show.
But before we get to the next slide, I want to go over these again.
>> For every lecture, there are notes that are taken.
It's a digest of the lecture, but it also has code snippets and helpful
tips and tricks.
So if you want to review lecture-- hopefully you didn't miss lecture, but
if you did, you should rewatch it-- but when you want to review lecture,
definitely check out those lecture notes.
There's also source code online from every lecture.
>> cs50.net/shorts are short, 6 to 12 minute videos.
You guys have seen many of them.
There are also videos we don't assign to you but cover topics that are
covered in lecture in this course.
So if you want to refresh on those topics, do check out these shorts.
>> study.cs50.net has primers on a ton of topics.
It has videos.
It has practice problems.
It is a fantastic resource.
Come any questions, but also quiz time.
>> A couple more resources.
There is the man command in the terminal that gives you some
information on a particular function or command.
There's Google, my favorite resource-- one of my favorite resources.
My favorite resource is the last one, which we'll get to in a second.
There's cs50.net/discuss.
That's our classes forum.
There are office hours Mondays through Thursdays, 8:00 to 11:00.
There are also online office hours for extension students.
>> And finally, if you need anything, have any questions, comments, or
concerns, you have my contact information.
Get in touch with me, please.
>> So as I hinted at, next Wednesday, a week from tomorrow, is your first
quiz, quiz zero, because in computer science we start counting at zero.
More information will be provided in lecture tomorrow, and a hand that will
be posted eventually online with more specific details, in particular the
location of quiz zero.
But it'll take place next Wednesday instead of lecture.
There's no lecture next Monday because it's a holiday.
>> The quiz will cover all of the material from week zero to week five,
so through this week, through lecture tomorrow.
Material includes the lectures.
Material includes sections.
Material includes the problem sets, and especially those beginning parts
of problem sets where you're walked through some background information on
what you're about to cover.
Do look through those problem sets.
>> To help you all with the quiz, next Monday, even though it is a holiday,
the course will be hosting a course-wide review session.
Video of the course-wide review session will be posted online if you
aren't able to make it.
In addition, next Tuesday in section we're going to be doing a ton of quiz
review and hopefully playing some jeopardy as well, but we're going to
be going over answering all your questions, but I'm going to come with
a lot of helpful questions and information based
on 10 years of quizzes.
I have a pretty good idea of what's going to show up and how questions are
going to be asked, so we'll go over those in section next Tuesday.
>> In addition, I didn't put this slide up last week, but this is something we
did last week.
This is how we approached a lot of the coding problems we did.
I wanted to distill out our approach into some simple steps that can help
you with solving a coding problem.
>> Remember last week, we coded binary search, and the first thing we did
when we wanted to code binary search is we thought about in our
heads how it worked.
We didn't draw a picture in class, but I asked everybody to watch the short
video, which did have a picture representation of how
binary search worked.
The first and we really wanted to do to understand something is draw a
picture to understand its logic, to make sure we know
how this thing works.
>> The next thing we did you in section last week was we took our mental
model, and we turned it into some pseudocode.
Pseudocode, again, programming-like syntax in words, but not C code.
Not even specific to C. We just took our ideas and took the English we used
to express it, and wrote it out in some sort of program-looking way.
So that's just the logic part.
We took care of our logic first.
We broke up this hard programming problem into two parts, and we took
care of the logic part first.
>> And then we approached the syntax part.
Remember after we drew up the pseudocode, we picked out parts of the
pseudocode that we knew mapped onto specific elements in C. For example,
for loops, if conditions, or greater than or equal to operators.
And finally, the last thing we did, the very last thing we did, was
actually code this program in C.
>> We're going to go through this process again today in a relatively large
coding problem that will take up the bulk of the second half of section,
but I wanted to lay this framework out here for you guys.
It is how we've been approaching problems in class, and how probably
you've been approaching these problems on your own outside of section.
So I'm going to leave this up here, and we'll refer back to this when
we're coding later today and go systematically through these steps.
>> Finally, feedback.
If you have any feedback for me, the website is at top of this slide,
sayat.me/cs50.
Give me some feedback.
I got some fantastic feedback from you guys last week.
You said you really liked the pseudocoding and coding we did.
Also somebody gave me a link to a study that has shown that waiting
three to seven seconds after asking if anybody has questions is the optimal
time to wait.
So if you have your stopwatches out, today I'm going to try and wait seven
seconds after I ask if anybody has any questions before moving on.
>> So before we move on, does anybody have any questions so far?
>> Control room gave me seven seconds.
OK, nobody had any questions, but feel free to ask them if you do.
>> First, this week we are going to cover pointers, as we hinted at at the
agenda at the beginning of class.
Pointers you've seen in lecture.
They have probably melted your mind.
That's OK, that's what they've done to hundreds of CS50 students before you.
So over the next 10 to 30 minutes, we are going to go through these pointers
slowly and systematically.
If at any point you have any questions, please ask me.
I guarantee that your peers in this section and watching at home have the
same exact questions.
>> Let's talk generally first, before we dive into pointers, about variables
and data, which we've been using before.
In addition to having these things up on a slide, I'm going
to draw these out.
>> As this slide says, data in memory has both a value and an address, so we're
going to represent something in memory.
Remember last week or when you've seen arrays being drawn, we generally draw
them as series of boxes, so we'll just draw one variable as a box
somewhere in memory.
This box in this example, we'll call it x, and it has a value.
In this case, we're initializing the variable to a value of 50.
>> You'll also notice that in addition to just the value, this variable x has a
location in memory.
Indeed, as you are probably somewhat aware of, memory has a
finite amount of spaces.
There's only so many boxes inside your computer where you can store things.
And better yet, so you know where things are stored, each box has a
specific address, a specific number where you can find it.
Just like freshmen all have their mailing address in the Science Center,
every box in memory has a specific address.
When you go to that address, you can get what's located there.
>> I'm going to be totally arbitrary with the address of this variable, but I'm
going to say the address is 123.
Now, I didn't just write 1-2-3.
I wrote 0x123.
The 0x part means this is a hexadecimal number.
And we are not going to talk about hexadecimal a lot today, but note that
when we refer to memory addresses, we refer to them in hexadecimal.
So whenever I'm talking about an address, I'll put that 0x, and that's
just saying 123 is a hexadecimal number.
It's in base 16.
It's not in base 10.
>> Does anybody have any questions so far?
>> Perfect.
So that's one variable.
We've seen this before, but now we're starting to go under the hood and see
what's actually happening.
>> So, pointers.
The cool thing about pointers is that unlike a normal variable that have
some value, some integer, some character, maybe a series of
characters if we're talking about an array, the value that a pointer has is
an address.
Up on the screen, we have the int x equals 50 that we created before.
I've already drawn that box.
>> Let's also draw a box.
This is going to be-- oh, my goodness.
We need a new marker.
Not much better.
>> But we're going to call this box-- this is the variable called pointer.
Pointer also has an address.
It's in a different address.
It's a different variable.
It's not going to be on top of the first one.
You can't take put two things inside of a mailbox.
You can't overwrite the initial one, so we're going at
different address here.
We'll give it the address 456.
But pointer's value is going to be an address, and in this case, it's going
to be addressed of the x variable.
So inside pointer, we're storing the address 123.
>> And the syntax I use to write it-- we're going to go over the syntax in a
second, but know that the syntax on this slide is the syntax you would use
to set this situation up--
before we get to the syntax, though, I want to make clear to everybody and
make sure everybody understands that pointers as distinct from variables
have an address as their value, not an integer or a char.
>> So far so good?
>> Generally also, when we're drawing these things, I'm not just going to
draw the address in here.
I'm going to take an arrow from this box to the other variable that it's
pointing to to give the sense that if I were to look at the address 123, if
I were to use this pointer to find what it's pointing to, I would get
this box over here.
I would get the number 50.
>> Yes, question?
>> STUDENT: If pointer stores an address, then how do we choose arrays, and int
star, or like a char star, or whatever?
>> JASON HIRSCHHORN: Can you repeat the middle part?
I lost you.
>> STUDENT: How do you choose arrays, like an int star or char star, or
whatever, if it always stores an address?
>> JASON HIRSCHHORN: Good question.
How do we choose what it's actually pointing to if it
always stores an address?
If instead of writing int star right here, I wrote char star, it would be
pointing to--
that is what we would write if we wanted it to point to a character.
Deep, deep down in the ones and zeros world, that int, that char, doesn't
really matter.
But in terms of C, it does want you to be pretty specific in terms of the
type of thing you're pointing to.
At the level at which we're programming, we're going to write the
type of thing we're point to even though it's always an
address inside this box.
Did you have a question?
OK.
>> Now we're going to look at some syntax for pointers.
The first line on this slide--
int star, space, pointer--
initializes and creates--
not initializes, rather.
Doesn't set it equal to anything, but it creates this pointer.
So in terms of my diagram--
let's go to this side--
that first line, int star pointer, creates a box.
This box is the pointer variable, and it's at an address.
We'll give it a different address, 999.
>> That's all that line does.
We've seen this before.
That's like any time, if I were to just write int x there, it would do
the same exact thing, create a box called x.
In this case, we're giving it a different type.
So it's not going to be an int.
It's going to be an int star, and so this box will be a little bit
different than if it were to just be a char, for example.
>> Because chars, recall, are only one byte in memory.
Pointers are four bytes.
So this box will look a little bit different if we write int star as
opposed to char.
But other than that, we're creating a box, and it's at a specific address.
That's all that first line does.
>> The second line of code, int x equals 50, again creates another box x at a
different location.
We'll give dislocation 111.
And this is actually initializing the pointer in addition to
creating the box.
We're putting a 50 inside this box.
>> The third line of code, pointer equals address x.
That address operator means a very specific thing.
Whenever we put the ampersand in front of a variable name, we are saying give
me the address of that variable.
So when I write ampersand x, that's saying give me the address of x, in
this case, 111.
>> Yes?
>> STUDENT: Would it be acceptable if we happen to know the address of this
particular variable to just say pointer equals 0x111?
>> JASON HIRSCHHORN: So if you knew that this was at 111, you could write
pointer x equals this hexadecimal number, but I can't see a way that you
would ever know what it is, so that's why we use this syntax.
But again, that ampersand x, ampersand is saying give me the address of this
variable, and just like any equal sign does, that's storing that
value inside pointer.
>> So whenever you see that ampersand, think this is giving me the address of
this thing.
Ampersand x is giving me the address of this x box.
Again, if we did ampersand pointer, what would I get?
>> STUDENT: Address of the pointer.
>> JASON HIRSCHHORN: The address of the pointer.
What specifically?
>> STUDENT: 0x999.
>> JASON HIRSCHHORN: 999.
Ampersand pointer would give me 999 in this case.
Ampersand in front of any variable gives you the
address of that variable.
Of course, I don't really care about the address of this pointer.
I care much more about the address of this variable.
>> Finally, the last symbol we're going to see is actually the same symbol,
that asterisk.
But in this final line, it's used a bit differently.
Notice how in this last line it's next to the variable.
As before it was next to the type, it was part of this type, we're creating
this type integer pointer, now the star means something
a little bit different.
It means go treat the value--
int pointer--
treat the value in this variable like an address, go to that address, and
give me that value.
>> So the star means, again, treat the value and pointer as an address, go to
that address, and return that value.
In this case, that value is 50, so this line would printf 50.
Yes?
>> STUDENT: So if we tried to do that on something that wasn't declared as a
pointer, like on x, and the value is 50, would it try to go to address 50,
or would it just give us an error?
>> JASON HIRSCHHORN: If we did star x, and tried to printf that, what the
compiler would do, or what the program would do when it was running, is it
would treat 50 like an address and go to address number 50 in your program.
Generally, when you're going to these random addresses, you're not going to
go to an address where something is nice there.
You're going to go to a really bad address that you're not supposed to go
to, and your program is going to crash.
>> So yes, you're exactly right.
It would do the same exact thing, but when we try to go to addresses that we
haven't asked for or we haven't declared, or that are just random
addresses, we almost always get in trouble and our program crashes.
>> Did that answer your question?
>> STUDENT: Yeah, I guess I have another one.
So if you cold just do that and it would go to that address, what's the
point of declaring a pointer as a pointer if we could just say a pointer
equals ampersand thing?
And then it would be like a meaningful address, and we could go there, right?
>> JASON HIRSCHHORN: Can you repeat the question?
>> STUDENT: Why do we have to declare the pointer as an int star if it would
work to just declare it as like a regular int, because it would still
have a thing that could be then dereferenced, right?
>> JASON HIRSCHHORN: So why are we declaring it as an int star rather
than just like an integer and saving this number in it?
>> STUDENT: Yeah.
>> JASON HIRSCHHORN: Because when we use things--
great question--
when we use things, whenever we're using a pointer and we're going to
treat something like pointers, other things are going on that I'm not going
to go into now that it helps if the program knows that
this thing is a pointer.
It will treat it differently in some other ways that is irrelevant for us,
but it we are going to use it like a pointer, we should say we're going to
be using this as a pointer.
>> So that is basically how pointers work.
It's not much more complicated than that, but unfortunately, it becomes a
lot more complicated when we put it into practice.
>> Next, we are going to do something quite large.
Instead of just throwing a couple lines of code online, I put up 4, 5,
6, 7, 8 9, 10 lines of code.
And rather than coding these out on your computer or running this program
on your computer, I want you to take out a sheet of paper and a pencil or a
pen, and figure out what each of the lines of these codes do.
Start with the left column, working from top to bottom, which initializes
there's a lot of these variables, and then go through each line of code in
the right column and figure out what happens.
I'll give everybody three minutes to start this on your own, and then we'll
go over this as a group.
>> Let's get started going over this as a group.
It's OK if you haven't finished.
I'm going to start with the left hand side of the column, and then I'm going
to invite you guys up here, the brave you amongst you up here, to execute
one line of code on the right hand side the column, and then we'll work
our way through.
>> So first, that first line of code, int x equals 2 comma y equals 8 comma z
equals 12, creates three variables.
I did it all on one line.
That's a handy syntax for creating multiple things of the
same type on one line.
>> So we have three boxes.
Can everybody see those three, or is that too low?
OK.
>> We have x, y, z.
And in x, we get 2.
In y, we get 8.
and in z, we get 12.
Pretty standard so far.
>> Next we're creating another box, another variable.
This is an int pointer.
It's called pointer x.
We'll draw that over here.
>> And what are we storing inside pointer x?
>> STUDENT: The address of x.
>> JASON HIRSCHHORN: The address of x.
In this case, I don't know the address of x.
It could be anything, but I'm going to make something up now to make it
easier for us.
We'll put x at 111.
So this gets 0x111, and we'll give it a nice arrow to signify
it's pointing to x.
>> Likewise, I have pointer y and pointer z.
They get values.
We'll give this 0x222, 0x333.
>> That's our initial setup on the left hand side of the column.
We created three integers, and we created three pointers.
Inside each pointer, we save the address of one of the three initial
variables we created.
>> Now, on the right hand side of the column, we are using that asterisk
symbol in a number of different ways, not all of which are to use it to
dereference something or to go to a specific address and
return the value there.
>> Can someone tell me what the first line of code does?
Somebody raise their hand who hasn't spoken yet.
You?
>> STUDENT: Set z to 16.
>> JASON HIRSCHHORN: It's set z to 16.
So z equals x times y.
In that case, it's just multiplication we're doing.
So z equals x times y, 2 times 8 is 16.
Perfect.
So we'll update this.
z equal 16.
>> That second line of code x space star equals space y.
What does that line of code do?
Jeff.
>> STUDENT: x equals x times y?
>> JASON HIRSCHHORN: Exactly right.
That's shorthand notation for x equals x times y.
You've probably seen things like x plus equals y?
That's x equals x plus y.
In this case, we're doing something a bit different, x equals x times y.
>> Then what do I need to update here?
>> STUDENT: x is 16.
>> JASON HIRSCHHORN: x is 16.
>> Changes in green are what we have done so far.
Next, we have y equal star pointer x.
Can somebody walk me through what this line of code does?
What does this line of code do?
Walk me through from the beginning.
>> STUDENT: It takes whatever pointer x is pointing to, the value that's being
pointed by that, and it stores it in y.
>> JASON HIRSCHHORN: Excellent.
I'm going to repeat that again.
You did a fantastic job, but so we can emphasize it, that star means treat
the thing in that variable like an address and go to it.
So, treat the thing in pointer x like an address, go to it, take that value
16, and then we're just saving it in the variable y.
So now y gets overwritten, and it is now 16.
>> The next line of code, star pointer underscore x equals x times y.
What does that line of code do?
Again, if you're a little bit lost here, evaluate it from right to left.
So Mondo, what does the right hand side of that do?
>> STUDENT: The right hand side just multiplies whatever is in the
box is x and y.
>> JASON HIRSCHHORN: And that's 16 and 16, so what's that equal?
>> STUDENT: 256.
>> JASON HIRSCHHORN: 256.
And where are we saving 256?
>> STUDENT: You're saving it in whatever pointer x is pointing to,
which is the x box.
So now x becomes 256.
>> JASON HIRSCHHORN: So the star, again, means go to.
We go to what pointer x is pointing to.
It's pointing to x, and there we're saving the value 256.
>> Any questions about that specific one?
We haven't seen the star used on the left hand side, yet, of
an assignment operator.
>> So that's the fourth line of code.
The fifth line of code, can somebody who hasn't spoken yet give me the
fifth line of code, or walk through it with me?
Annie, what's it do?
>> STUDENT: Change the value of pointer x to the value of pointer y.
>> JASON HIRSCHHORN: That's exactly right.
The value of pointer y is this address right here, and we're storing that in
the variable pointer x.
So this becomes 222.
And I'm going to move the arrow, too, to signify that it now
has a different address.
>> So now pointer underscore x and pointer underscore y
have the same value.
That's what that line does.
So essentially, they're both pointing to y.
If we just had our pointers, and we didn't really know the variables, we
would have lost x.
But thankfully, we have x saved in this function, so we still
know where x is.
But we'll see that problem coming up later on, that if we lose a pointer to
something, we might not actually know where it is or be able to recover it.
But that's for the future.
So for right now, that's exactly right, Annie.
>> And finally, that last line of code.
Can somebody give me what the last line of code does?
Yes?
>> STUDENT: It multiplies what is in pointer y, so 16 times 16--
>> JASON HIRSCHHORN: 16 is not in pointer y.
>> STUDENT: Well, it goes to that address, finds it, and then multiplies
that by the same thing-- it does the same thing with pointer z, multiplies
the values, and stores it in x.
>> JASON HIRSCHHORN: Exactly right.
So what do I need to update?
What should x be?
>> STUDENT: The top left, 256.
>> JASON HIRSCHHORN: And what do I change it to?
Sorry, we'll go back up.
>> STUDENT: It should still be the same.
>> JASON HIRSCHHORN: Yes, it actually still equals 256.
But that's exactly right.
Let's break this down.
We'll see right here, this thing in parentheses, star pointer y, that
treats what's in pointer y as an address, goes to that
address, takes the 16.
So now we have a 16, and we're multiplying that by the
same thing in z.
Going to that address, taking a 16.
16 times 16 is 256, and we're storing that inside of x. x already happens to
equal 256, so we're not going to make change.
>> You don't have this file right now, but in the source code for this
section which will be posted online at cs50.net/sections, I have written all
of this up, exactly what we did.
And I actually included some other information right here, which we're
going to go through in a second.
>> We do these operations , as you can see right here, and then print out the
resulting value of the specific variable that has been changed.
This code's already been written.
I'm going to run this code.
>> And now let's walk through the output.
Let's start at the beginning.
Value of x equals is 2.
That should make sense.
That was what we initially initialized it to.
>> Address of x is this crazy thing.
Again, we see, however, even though it looks a little bit crazy upon first
inspection, we see that 0x right there.
This is a hexadecimal number.
>> And then we see all of this crazy stuff.
There's a b and an f, and also a c in there.
None of those are normal zero through nine digits, but because hexadecimal
is base 16, you actually need a couple of other symbols to
represent each digit.
So again, we're not going to talk about hexadecimal too much, but know
that when you're writing hexadecimal numbers, you'll see anything from zero
through 9 plus things from a through f.
So even though they're not all zero through nine numbers, this is a
number, this is an address.
And it could be expressed in base 10 if we wanted to.
>> The value of pointer x right here is also a hexadecimal number, and what is
it the same of?
What is this number the same as?
>> Address of x, right?
The line right above it, those two addresses are identical, and that's
what we expect.
The address of x is the value of pointer to x.
>> Next, we have the address of pointer x, and that is,
again, some random number.
If we're going to inspect it, we notice that it shares a lot of the
same numbers as the other ones.
It's different, but it shares a lot of the same numbers.
Recollect talking about buffer overflow errors?
When you can write over things that are close to you in memory, if you go
beyond the bounds of an array.
Again, we're not going to talk about that right now, but for those of you
curious, these things are actually pretty close to one another.
These boxes are pretty close to one another in memory.
>> And finally, pointer x points to the value, and of course, it's 2.
I did that star.
We followed the address in pointer x initially, and it
returned a value of x.
>> Finally, we see exactly what we just did.
First z is 16, then x is 16, then y is 16.
x eventually becomes 256 right here.
And again, x is not changed by that last line of code.
It remains a 256.
>> If you want to repeat this on your own once again, or go through it, or
change some of these values up, I've already written this code.
You can make the changes to the code and see how those changes actually
play out in real life.
>> Before we move on with pointers, does anybody have any questions?
>> OK.
Can everybody take one hand, and put it up in the air, and--
close your eyes--
put your right hand up in the air as a fist, and give me a thumbs up, thumbs
down, thumbs middle, how comfortable you are with pointers so far.
Close your eyes.
>> OK.
You can put your hands down and open your eyes.
Thank you.
>> We're going to move on.
We're going to keep talking about pointers, but we're going to talk
about them in a little bit of a different context.
We're going to talk about arrays and pointers.
And remember last week I blew your minds?
Somebody's brains, they left them here on the floor.
I was pretty sure blew that person's mind.
That was a bad joke.
>> [LAUGHTER]
>> JASON HIRSCHHORN: I'm going to work on that.
But anyway, strings we found out were arrays.
Arrays are actually pointers.
>> What do I mean by that?
Well, if we create an array of six characters, which this line of code
does right here--
char, array, open bracket, six, close bracket, semicolon-- again, creates an
array of six characters.
Fill it.
>> I have a picture of it right here.
These boxes, we see that filled it with some characters and the null
terminator at the end.
And down below, this is where I'd like you to direct your attention.
The very first box--
we've done this before.
We've actually done this in every problem set.
I can refer to the very first box in this array as array bracket zero.
We've done that a million times.
>> I can actually refer to the same box another way.
I can refer to it as star array.
Because array, if we were actually to print out what array equals, array is
an address, and it's an address of the first box in this array.
So that star operator treats what's stored in array as an address, goes to
that address, and returns the value there.
Star array will return f just like array bracket zero returns f.
>> Again, everywhere we've been writing array bracket zero, star array would
have done the exact same thing.
In fact, array bracket zero is just our sugar coating of
what's actually happened.
It's really star array is the more correct way, if there is one correct
way, to think of what's actually happening.
>> So that's all well and good, but here's where it gets fantastic.
We go to the second character here, this zero.
Should be an O, but we'll give it a zero.
We're referred to that before, the second box in an array, as array
bracket one whenever we've iterated through an array.
>> There's another way to refer to it, and that's down below.
That star, open paren, array plus 1, closed paren.
So because arrays are laid out-- these boxes are all right next to each other
in memory--
star array goes to the first one.
So if we're going to take array that address, add 1 to it, and then go to
that new address, that's going to be the thing right next to
the original address.
>> Yeah?
>> STUDENT: So if array was, instead of being chars, it would be ints, then
you'd have to increment by 4 every time?
>> JASON HIRSCHHORN: that's a great question.
Let me draw something first, then I'm going to answer your question.
>> To draw this up on the screen really quickly before getting to your
question, we have our array--
and it goes on, but I'm only going to focus on the first couple of ones.
In memory, there's a box that we created when we
initialized this array.
This is the array variable, and it is an address.
It is, in fact, the address of the first thing, the first box.
So this box is somewhere.
These six boxes are somewhere else.
>> When we do array bracket zero, we're going to get the first thing here.
And when we do star array, we're going to get the exact same thing, the value
that array is pointing to.
>> When we do array bracket 1, we're going to get the second box, but also,
because the pointer is outside those parentheses, first we're going to add
1 to this address, and then we're going to follow that and give us the
second box.
And so on and so forth.
>> Array plus 2 is going to add 2 to this address here, because again, addresses
are just numbers represented as hexadecimal, often.
So add 2 to it, and follow that to the third box, which happens to be, as you
can see, two spaces away from the first box.
>> Hopefully, that made it less confusing rather than more confusing.
But did that make it more confusing?
Again, I'm going to get to your question in one second.
Did that make it more confusing for anybody, or does anybody have any
questions about this idea of arrays as pointers?
>> Fantastic.
Also it'd be fantastic if you had questions.
But moving on to your question.
>> The question was integers in memory take up four bytes, so four boxes.
If we think of one boxes as one byte, integers take up four boxes.
>> So say these are integers now.
This is an int array right here.
I'm going to make up some random addresses.
So if this was an int array, the first one might be a 10.
The next one's four boxes over because integers are four boxes big.
There's be 10, 11, 12, 13, so this one would be a 14.
This one would be an 18.
This one would be a 22, and so on.
>> So again, chars are one byte.
They're right next to each other, so this map we were talking
before works perfectly.
>> But you just brought up the case of ints.
Well, if I added 1 to this, it would go to 11.
It'd be in the middle of this int if I followed it really
specifically like that.
I was actually going box by box in memory.
>> The cool thing about this type of pointer arithmetic is the program is
smart enough to know what you mean.
When you're adding 1 to a memory address that is pointing to an
integer, it's not actually going to add one block over.
It's going to add four blocks over.
It's going to scale that number for you buy the size.
>> And this goes back to Curtis's question, why do we do int star rather
than char star?
Was that your question?
Somebody asked that earlier, right?
Why we do int star rather than char star if it's going to address no
matter what?
And that's to help us with this type of pointer arithmetic.
If we declare something as a char star but it's really going through
integers, when we added 1, it would only move one box over rather than the
four we logically expect it to move over.
>> So again, in pointer arithmetic, when we're treating these things like
addresses, then adding or subtracting to them and then following them to
figure out what's going on in memory, the number we add to them gets scaled
by the size of the type of thing they're pointing to.
>> So if they're pointing to an integer--
I know I'm repeating myself--
but if they're an integer, you're actually going to move four
boxes over each time.
If they're a char, you're going to move one box.
If they are a long long, which is eight bytes, you're going to move
eight boxes over.
Whatever the size of the data type, who knows?
Doesn't really matter for our purposes.
All that we need to know is actually, this math right here, just adding 1
will get us to the second box regardless of what type of thing is in
that second box.
>> I hope there's some type of questions here, because that was a lot, and I
could not have done a perfect job at explaining that.
Really?
No questions?
For the audience at home-- there we go.
>> STUDENT: If you were to make an array in a function, and you want it to
return that array to something else, how would you return it?
>> JASON HIRSCHHORN: Excellent question.
So if you wanted to make an array in a function, and then return that array--
so we'll say we'll make an array in a function called by main.
Main calls this function, and this function creates the array, then
returns it to main.
>> Now we've seen something similar to this before.
Where have we seen something similar where main creates the array, and then
the function manipulates it?
What have we written before that's done that?
>> STUDENT: Sorting?
>> JASON HIRSCHHORN: Sorting has done that.
Game of 15 has also done that.
In sorting, we passed in the array, the haystack, and then we sorted it by
destructively updating different parts in that array.
So we took our original array-- remember, one of the parameters to the
sorting function was array--
>> STUDENT: Isn't that a global array?
>> JASON HIRSCHHORN: Right.
Yeah, we're going to get back there.
But had it been created in--
was it a global, or was it created in the function?
Let's look.
We can answer that.
>> It looks like it's not a global array.
It has created right here on this line an array of integers, and then it's
passed to our sorting function.
So it isn't a global array.
It's created in main, passed to our sorting function.
Our sorting function destructively updates it, and returns it.
As we can see up at top, sort--
oh, my goodness, sorry.
>> STUDENT: Sort just returns whether or not it found it?
>> JASON HIRSCHHORN: Right.
Sorry.
Sort actually-- let's open that up.
Sort is void.
Sort doesn't return anything because sort destructively updates the array,
the original array.
>> So that's what we've seen before, but your question was what if sort
actually creates the array?
>> STUDENT: Refer returns the updated array, or something like that.
>> JASON HIRSCHHORN: Another thing that could have been done was main called
the function.
That function's job is to create an array of something, an array of
integers, and then return that array it's created to main.
>> So as we've seen before when talking about the stack, which we're going to
talk about a bit later today, once a function returns, everything that was
created in that function, all of its local variables, go away.
We've seen that idea of scope before.
That's why we've created global variables sometime, to escape this
problem of things going away when their functions return.
>> What you would do in that case is the function would create the array not as
a local variable on the stack.
It would create it in a different part of memory that allows things to be
saved even when functions have returned and the function frame, the
stack frame, has left the stack.
>> I'm going to use some terminology here.
You're not going to hear about this until tomorrow, but that area of
memory is called the heap, and I don't want to go into that.
But what that function could do, again to oversimplify potentially, is create
that array in a different space in memory that actually stays there for
as long as you want it to stay there, and it'll pass back to main a pointer
to that array saved in that other part of memory.
So it makes some boxes somewhere else that don't get destroyed when the
function returns and passes back to main an address to that set of boxes.
>> That's probably not a sufficient enough answer to your question, but
that will be explored more tomorrow when you guys talk
about the heap in lecture.
>> Let us move on-- oh, my goodness!
That's a dolphin!
How adorable is that?
God!
They're my favorite animal, so they are indeed adorable.
I have three dolphin stuffed animals.
But enough about me.
>> You all have your computers open in front of you.
Here is your coding assignment.
We're going to start with pseudocode, though, so don't get too excited.
>> You need to write a program that takes one and only one command line
argument, and that's the number of dolphins to be trained.
That number should be greater than 0.
>> Next you're going to allow the trainer to enter an age for
each of these dolphins.
Each age, again, should be greater than 0.
And all of these ages should be stored in an array.
>> To get an age, however, you need to call a function with the following
prototype--
int star, space, getAge, parentheses, void, close parentheses, semicolon.
>> Finally, I want you to print out the age of the oldest dolphin.
So again, before you start writing in code, have a sheet of paper or have
gedit open if you want, and write out some pseudocode for this.
In three minutes, we're going to go over the pseudocode.
You'll have another three minutes to code it, and then we'll go over coding
it together.
>> And if you have any questions because this isn't clear, raise your hand and
I'll come around, and I'm happy to answer your question.
But to start, take three minutes to write pseudocode to make sure you
understand the logic of this problem.
>> Let's take a stab at the pseudocode as a group.
I'm going to write this on the board as you give me the pseudocode.
Who would like to give me the first line?
First line's maybe the easiest, so if you haven't
participated yet, start here.
What's the first line of pseudocode.
>> STUDENT: First line is to check for the number of arguments that you get.
>> JASON HIRSCHHORN: Exactly right.
Check number of arguments, first thing we need to do.
Next?
>> STUDENT: Check that that command, the first argument, is greater than 0?
>> JASON HIRSCHHORN: Check that the first argument is greater than 0, yes.
Next line, go ahead.
>> STUDENT: Then you'd probably want to store that command line argument into
a variable, or you could just go to the next step.
>> JASON HIRSCHHORN: Yes, again, we'll have a string here, but we want an
integer because it's a number, so we need to make the
string into an integer.
>> STUDENT: x, y.
>> JASON HIRSCHHORN: That's right.
So, we'll write that.
String-- whoa, look at that marker.
That's the second thing we need to do, take the string and
turn it into an int.
What's next?
>> STUDENT: Create an array of ints with the size of the first command line
argument that's not programmed?
>> JASON HIRSCHHORN: Sorry?
>> STUDENT: Just create an array of ints, and make it the size of the command
line argument that you got.
>> JASON HIRSCHHORN: Create array of size--
we'll do arg, whatever that argument was.
OK?
What's next?
>> So we have the array, allow the trainer to enter an
age for each dolphin.
To get an age, call a function with the following prototype.
So we have our array.
What do we need to do?
>> STUDENT: For n dolphins?
>> JASON HIRSCHHORN: OK.
I'm going to write for each.
For each dolphin, what do we need to do?
Go ahead?
>> STUDENT: Call the function.
>> JASON HIRSCHHORN: Call the function, OK.
>> STUDENT: In pseudocoding, should you define functions like in a separate
block of pseudocode?
>> JASON HIRSCHHORN: So, yeah.
Let's do a separate box of pseudocode over here, pseudocode for getAge.
So this function, for each dolphin we call getAge, and
getAge returns something.
So where should we put the thing we're returning?
It returns an int pointer, right?
That's the return type, according to that function prototype.
So where are we putting that int pointer.
>> STUDENT: Into the array.
>> JASON HIRSCHHORN: Into the array.
That sounds great.
Call getAge, put return value in array.
>> Let's pause getAge for a second.
We'll come back to that.
And I'll let you start me on that, Marcus.
But we've gotten each age.
We've put them all in the array.
What do we do at the end?
How do we finish main?
What's the last thing we need to do?
Alden?
>> STUDENT: We could sort them, and then print the largest find.
>> JASON HIRSCHHORN: We could sort them, yes.
So how do we sort?
>> STUDENT: We could just like--
>> JASON HIRSCHHORN: Do we need to sort here, though?
>> STUDENT: I suppose not.
>> JASON HIRSCHHORN: OK.
You did this before.
What was the first way you wrote search for?
>> STUDENT: We could just check each one against each other one.
>> JASON HIRSCHHORN: Let's do that.
Let's search through each one and just keep track of the oldest age.
Search for oldest, and then what do we need to do with the oldest?
>> STUDENT: Return it.
Print it out, sorry.
>> JASON HIRSCHHORN: There we go.
Exactly right.
Print out oldest.
>> OK.
Marcus?
>> STUDENT: Keep track of the oldest as you add them?
>> JASON HIRSCHHORN: OK, search for oldest.
So we could keep track of the oldest as we add them up there.
Call getAge, put return value in the array, and then see if this is the
current oldest one, maybe save it in a different variable?
That would work as well.
That might actually save us a trip, but this is fantastic.
I'm going to leave this up here for now.
But if you want to in your implementation take it that step
further, that would be great.
>> Marcus?
Lets do getAge over here.
>> MARCUS: OK.
Ask the user for an age.
>> JASON HIRSCHHORN: Ask the user for an age.
OK.
Can everybody see that?
Over here?
OK, ask user for an age.
Keep going, you got this.
>> MARCUS: I guess I don't really get why it's better to return an int star than
just an int, like then to just return the age to store it.
>> JASON HIRSCHHORN: It's not necessarily better to return.
It would work just as well returning in, but I want you to return int star.
>> MARCUS: OK.
I don't really know what--
>> JASON HIRSCHHORN: OK.
Does anybody have any thoughts?
We ask user for an age.
We're getting an integer.
>> OK.
So why don't we pause here.
We're going to go to our next step in the logic, map these things on to see,
and then you guys will code that first part, and well code all of that
together, but a particular getAge.
And we'll figure out what's going on with that int star.
>> But before we do that, before we jump into C code, can somebody point out
some things they see in our pseudocode that would map onto regular code?
In C?
So some things in pseudocode, some words you see, some indentation you
see, some things that give you some hints about how to code this in C?
>> STUDENT: For loop.
>> JASON HIRSCHHORN: Where?
>> STUDENT: For "for each "dolphin."
>> JASON HIRSCHHORN: OK.
There we go.
We've got a loop there.
What else?
Mondo?
>> STUDENT: The string to int thing would be a to i
>> JASON HIRSCHHORN: OK.
So we've seen that function before.
Other stuff we see?
>> STUDENT: Printf.
>> JASON HIRSCHHORN: Exactly.
Not obviously, but hopefully we have some familiarity now with printf.
What else?
Yeah?
>> STUDENT: Reading an array is just declaring an array?
>> JASON HIRSCHHORN: OK.
So, of size arg.
That's right.
I don't know what to write that as, but yes, declaring one.
Another word for creating it.
Any other things we see up here?
Structures we know?
What about that line "search for oldest?" What kind of
structure might that use?
Alden, you suggested it.
>> STUDENT: Just search.
so like just a loop.
>> JASON HIRSCHHORN: A loop.
So, they search.
You're probably talking about linear search right here, so it's probably
just using a standard loop going through everything, which is why
Curtis suggested we could put it in the previous loop.
>> Any other things we see?
Check number of args.
How do we check something?
>> STUDENT: If statements.
And
>> JASON HIRSCHHORN: That's an if condition.
>> So, why don't you get started coding this program.
You have the pseudocode up here.
Start writing and in C code.
Again, we're going to figure out what's going on with that int star and
how we can actually write this program so that it conforms to the
specification I've given you.
If you want to, however, start with just int get void.
It just returns an integer.
>> That helps you get going.
Just code that.
Code it as just the return type as an int, not as an int star.
But then we are going to turn it into int star again so we can conform to
the specification.
Also explore how one might actually get at what Jeff was
talking about earlier.
So take three to five minutes to code this.
>> We are going to go through this code together.
I apologize if there was a lack of clarity on some parts of this.
But what we're going to look at right now is I've already typed up all the
code for what it will look like if we're just returning an
integer with getAge.
And we'll go through that code, and then we'll figure out how we can
actually get it to align with what was asked of us to return an int star.
>> Here's the code.
And again, I apologize that we can't code this out as a group, but I want
to make sure we get through it.
>> For the top, we have our function prototype, getAge.
We've seen that before.
Return type first, then name, then parameters.
>> Up here, we're including three libraries.
Can somebody tell me what this line 12 does?
Sharp include standard live.h?
What does that line do?
If I were to ask you this on a quiz, and I asked you give me the answer in
two sentences, what would you say?
>> STUDENT: [INAUDIBLE]
allows you to use all the functions stored in that file.
>> JASON HIRSCHHORN: That's a good answer.
It's the header function that has the prototypes of a number of functions,
and if you're going to use those in your library, this is saying these
functions exist.
What else do I need to do if I'm including, say, the CS50 library?
What else do I need to do when I compile my program?
>> STUDENT: [INAUDIBLE] include it using the dash l.
So we need to link in those CS50 library with dash l standing for link.
dash l CS50 links in the actual ones and zeros, the actual implementation
of, say, get string or get int.
>> So then we're in main.
Ensure user entered one and only one command line argument.
Just like our pseudocode, we have that if condition up here.
Hopefully, you guys are pretty familiar with this line and able to
write this code pretty quickly, checking argc.
>> Next, we're doing that a to i line.
Again, you've seen that before.
That's converting a string to an integer.
>> Next thing I do is I have another if condition.
I'm making sure they entered a positive number of dolphins per the
specification, checking if dolphins is less than 1.
So again, this first part of the code, these three things--
check the number of args, n if it's greater than zero--
you've written this code a lot, validating user input.
You should be pretty familiar with writing this code and being able to
write it pretty quickly.
>> Next, we're initializing a new array.
What type of things are we putting in this array?
Integers per this first three characters.
>> Next, we're getting ages, and a normal for loop iterating through an array,
and we're calling this function getAge, storing the return value of
getAge in the array.
Again, you've written code like this a lot before, too.
One thing that hopefully you will glean from going through your past
problems for this quiz is that a lot of what you're going to continue to do
are things you've already done.
It's very important to see those patterns so that you're not going to
make your work super hard for you in the future by thinking I've never done
this before.
You've done all of this stuff before.
>> Next, our final lines of code.
We're keeping track of the oldest age, going through each age, just like you
suggested, Alden.
And if the age of the current thing we're iterating through is greater
than the oldest number we have stored, we update the oldest number.
>> And finally at the end, we print out the oldest number.
Again, you've done this before.
You've actually written linear search.
You've written that.
It was a little different.
You were looking for a specific number, not the greatest one, but
you've written this code before.
>> And finally, in int getAge, you've seen this before.
We have a do-while loop asking for user input until it
meets a certain condition.
And finally, we're just returning that integer.
>> So all of this code you have seen before and written before.
Nothing Is new here.
Of course, I've presented it in a different way, and I maybe was too
confusing with my directions, but you've seen this before.
This logic you're 100% capable of.
And if we run this program really quickly--
I've already made it, so we'll run ./trainer.
>> What happened?
>> STUDENT: [INAUDIBLE].
>> JASON HIRSCHHORN: I didn't give any arguments.
Let's train five dolphins.
That would be one two, three, four, five years of age.
And as you can see right here, it printed out the oldest age.
So let's go back into gedit.
>> But of course, this wasn't what the specification was asking for.
It was asking the prototype.
Not the returns in integer, but the returns in int star.
So if I'm going to change it up there in the prototype I better
change it down here.
>> And now let's work through this, right here.
Let's start at the bottom.
Age is not an int star.
Age is what type of variable?
>> STUDENT: An integer.
>> JASON HIRSCHHORN: So line 63, age should not be an integer.
It should be an int star.
How do I make it an int star?
>> STUDENT: [INAUDIBLE].
>> JASON HIRSCHHORN: Put a star there.
And now let's look down here.
67, getInt returns an integer, and we're storing that integer in age.
But if we're treating age as a pointer now, how do we need
to update this line?
>> STUDENT: Put a star.
>> JASON HIRSCHHORN: Put a star before age.
Is that what you said?
>> STUDENT: Yeah.
>> JASON HIRSCHHORN: Right.
Because getInt returns an integer.
We want to go to the location given to us in age and put an integer there.
If we did this line of code, we'd go back to what Curtis was talking about
earlier, saving an age, some crazy integer, maybe the integer 5.
Eventually we try to go to location 5 in memory, and we get in a lot of
trouble for that.
>> So we need that star there, and line 69?
Again, age, it's in memory address right here.
So how do we need to update this line?
Somebody else?
How do we need to update this line?
>> STUDENT: Put an asterisk there.
>> JASON HIRSCHHORN: Exactly right.
Add a little star right there.
Now we're getting the value pointed to by age rather than age.
>> So again, we saw this code go from integer to end point.
Pretty similar.
We need to look for all those places we had age, and we need to change them
a little bit because we don't want to change age's box so much as what age
is pointing to.
>> And returning age, is that correct?
That is correct, because age is an int pointer.
Yeah, question?
>> STUDENT: Can we keep it int age throughout, and then at the end, put
an ampersand in front of age?
>> JASON HIRSCHHORN: And return its address?
That's an excellent question.
You could do that, but let's get to that in a second and answer that
question in a second.
Fantastic question.
>> Up here, if we're turning an int pointer, what type of things is
dolphin ages storing in it?
If we're returning an int pointer right here, what type of thing is
dolphin ages storing in it?
Int pointers, so line 37 should be int star.
That's the type of this array.
It's not storing integers anymore.
It's storing int pointers.
>> And finally down here, dolphin ages bracket i, that's going to give us,
again, and int pointer.
We don't want an int pointer.
We want the value pointed to by that int.
So Hassan, how can we change this line?
>> STUDENT: Put a star?
>> JASON HIRSCHHORN: Exactly right, put a star right there.
That'll give us a value.
What about line 51?
>> STUDENT: Another star.
>> JASON HIRSCHHORN: Another star.
That's exactly right.
So we're treating, again now, these things as int pointers.
>> STUDENT: Is it a problem at all that in getAge, int star age is never given
e location?
>> JASON HIRSCHHORN: It is a problem that int star age is never given a
location, and it's going to be a similar problem to what you were
talking about with just returning ampersand age and
keeping it as an int.
But I want to run this code first and see what happens.
>> OK.
So variable age is unitialized when used here.
So it says, "initialize the variable 'age' to silence this warning," and it
suggests giving it null.
Let's give it null.
So we should get rid of tha warning.
We should be able to compile.
>> How old is the dolphin?
Let's say, 1.
Seg fault.
>> So I would go through GDB now, and I encourage you to go through GDB on
your own to figure out why this program just seg faulted.
I'm not going to leave this as a cliffhanger, though, and I'm going to
open up this function.
We're going to understand why this program seg faulted.
So as you all on the stack, when a function is called, a tray is added to
the proverbial stack where local variables are stored.
When a function returns, that tray goes away.
Those local variables go away as well.
>> Int star age equals null is a local variable saved in that stack frame.
When this function goes away, this variable goes away as well.
So we passed on the address of whatever.
We passed on age.
It's an address, we passed that on.
But ultimately, we--
>> Sorry, I'm getting ahead of myself.
Why this program really seg faulted in this case was because age was pointing
to null, which is a specific location, and null is a location you're not
allowed to touch.
And so when we tried to write this line of code, going to age and doing
something there, we got in trouble, and that's where we seg faulted.
Because star age, age again is pointing to null.
We want to null, which is a bad area.
We tried to getInt there, 1 in our case, and that's where we seg faulted.
>> But say that an opt-in case, say that we had given this a legitimate address
right here, like bf4ac, something we saw before.
Even so, when this program returned, we would run into a problem because
that address would have gone away.
The local variable address would have gone away.
Whatever age was pointed to wouldn't have existed anymore.
>> The way we fix that is something you've seen
before, the malloc function.
And you've been introduced to that malloc function before.
You've seen this?
Malloc gives you some memory that doesn't go away when the
stack frame goes away.
So writing this line of code--
and I'm going to give it to you for now--
malloc.
And since we're storing an int, we're going to malloc enough space.
We're going to say give me a box of size int.
Malloc returns an address, and we're saving that address in age.
>> Now when we run this program--
we're not going to seg fault because those blocks of memory we got through
malloc don't disappear when the stack frame disappears.
They live on until we want to get rid of them.
>> And this goes back to your question, Jeff, from earlier.
How do I create things inside of my functions that I want to use later on
in main or in different functions?
I use malloc, and I pass their addresses around.
>> That was a lot, admittedly, to cover just now.
So please, if you have any questions, ask away.
>> STUDENT: So malloc isn't on the stack anymore, then?
>> JASON HIRSCHHORN: That's exactly right.
The box malloc is giving you is not on the stack.
It's giving it to you in this other part of memory which we're going to
talk about in lecture on Wednesday.
The name is the heap, but I don't want to talk too much about it other than
it's this other place that doesn't get erased when the function goes away.
>> Any other questions?
>> STUDENT: I think if you didn't use a separate function and you put all this
code into main, would you still need to use malloc?
>> JASON HIRSCHHORN: Great question.
This is probably not the most efficient way of writing this program.
You could have easily done this do-while loop in main and saved
yourself a whole lot of trouble.
The reason why I wanted to do it like this is because I wanted everybody to
be able to see how in main, we can create an array of
pointers, int pointers.
We can initialize those pointers somewhere else, put them in this other
area of memory that stays for as long as we want it to stay, and then we can
access them later.
>> For example, here we searched through them.
We could have done other things to them.
We could have gotten them and found the average.
We could have done a lot of things.
The point being that you can keep track of some things in main through
an array, create those things somewhere else, and then still use
them later on.
>> And this idea, what we're doing now, is going to come up a lot in future
P-sets and problems you want to solve.
That's why we did it like this even though admittedly it's convoluted a
bit in this context.
>> STUDENT: [INAUDIBLE]
if you can keep all the int ages as actual values as opposed to pointers,
could you use the ampersand age now?
>> JASON HIRSCHHORN: Sorry?
>> STUDENT: If you took out the stars on all the ages, and just did return
ampersand age, at the end, would that work?
>> JASON HIRSCHHORN: So malloc returns an address.
>> STUDENT: OK, right.
>> JASON HIRSCHHORN: It returns an address, so age has to be an int
pointer, because it's going to be stored in an address.
>> We have six minutes left.
I'm going to skip talking about the stack for now.
I'm going to go into three minutes briefly on SPL, the most important
things I think you need to know for the problem set.
>> However, before I get here, because this is what we're going to finish on,
two final reminders.
One, admittedly we went through things pretty quickly in this section.
I apologize for that pace.
If you have any questions about something we went over, why something
worked the way it did, or something I do not explain as clearly as I could
have, please shoot me an email or talk to me after class, and I'll be happy
to explain it to you more in depth.
So please reach out if you have questions.
>> Again, the feedback website as well.
Take some time after class if you have it and give me some feedback.
I appreciate it a lot.
As you saw at the beginning of class, I read through them, respond to them
if you want a response, and I do you take that to heart.
That means a lot to me.
>> Let's finish class by talking about the library you're going to
be using this week.
You've probably seen code like what I have on the screen up before, GRect
paddle equals newGRect,(x, y, width, height).
You guys have probably through reading through the spec felt pretty
comfortable with this, but for those who were less comfortable, for you
guys as well, I want to re-go over this line and see how it's similar to
what we've used before.
>> Just like with any variable--
this is an object, but just like with any variable, we have the type first
and foremost right here, GRect.
>> Next we have the name of the object, and finally we have the function that
initializes it, gives us the paddle we want.
Sometimes we write int x equals 5, and we initialize something directly.
Sometimes we write string name equals get string.
We use a function to get something, and we take its return value, and
that's our new value.
>> So we've seen this idea before, where functions that may or may not need
input actually create the thing for us, rather than
us creating it directly.
So we've seen this code before.
Hopefully, this idea of an object, or at least creating an object, is not as
overwhelming if you can conceptualize it as just very similar to things
we've seen before.
>> However, the cool thing about objects as distinct from variables is that we
can do things with them.
You can't change the color of a variable, but you can indeed change
the color of an object, or at least in SPL you can change the
color of your objects.
>> You can't move a variable in a window, but you can move
an object in a window.
You can get an object's location.
You can't really get a variable's location.
That's the cool thing about objects in this library.
>> However, these functions can be kind of confusing.
We haven't really used functions or objects before, so I felt it was
helpful to understand a generic way to express or do things with objects, or
understand objects.
And as you'll notice, I have two functions written here, functions
you've seen before-- add, window comma paddle, and set color
paddle comma black.
In both these cases, we have a function that takes two arguments and
does something.
>> Now more generically, all of these functions you're going to write that
have to deal with objects take a where and a what.
A where is the object you want to do something to, in this
case, a window or panel.
And the what is what you want to do it or give to it.
>> When you're setting the color something, where are
you setting the color?
With the paddle object.
And what color are you setting?
Black.
With the add function, where are you doing something?
With the window object.
And what are you giving to it?
You're giving it the paddle.
>> You'll notice that the functions you use, set color, set location, et
cetera, et cetera, all follow this pattern.
They take something you want to do something to, the object you want to
do something to, and what you want to do with it, or what you want to have
changed, or what you want to have added, or the specific color
you want it to be.
Or if it's not filled, do you want to make it filled, et cetera, et cetera.
>> Admittedly, a very brief discussion of the Stanford--
what is it?
Stanford Programming Library?
Portable Library, that's right!
Thank you, class.
Stanford Portable Library.
But again, a lot more information is given in the problem set spec, but
hopefully this makes it a little bit easier to understand object,
especially as they've been relating to things we've seen and done before.
>> So with that, that is week four.
Enjoy learning about the heap tomorrow.
Good luck on your quiz next week, but of course, I will see before then,
hopefully in section next Tuesday.
But also if you have any questions, comments, concerns, please feel free
to reach out.
And after class, I'll step outside if you want to talk about your problem
set or have some questions.