DAVID MALAN: --here.
It seems to be broken still.
In fact, let me go ahead and open another example rather than type
this one out--
DOUG LLOYD: For many years I taught a section
for students who were less comfortable, and there was definitely
some anxiety when we came into this point in the course.
For some students I think pointers can be really intimidating,
and you hear the horror stories about how
they can cause you to crash your program if you don't handle them correctly.
I think that this example is a really good way to introduce why we need them
and what they do.
DAVID MALAN: I think so, and I don't know
where it is that I sort of latch onto this as the entry
point for our discussion of pointers, but I
like how we introduce a pretty reasonable goal,
let's make a function that just swaps two values
and see that it's broken, because all of a sudden
it sort of shatters the abstraction that has been our discussion of functions,
and programming, and what it can do, and you realize,
wait a minute this is not working as I expect it does,
and we have to go lower level and actually see
what's going on in the stack, and what's going on ultimately with memory.
And I think that's by far the most effective way to explain this.
Don't hide the fact anymore that there is a whole stack going on,
there's ultimately heap as well, and there are indeed
pointers by which we delete those chunks of memory.
Just show it to students, and actually dive into that layer.
And so this is indeed the sort of thing that I
think many teachers stray away from C because of, because
of these lower level details.
But I think this is where things really start to get interesting,
and starts to give us the vocabulary so to speak with which
we can start building and stitching together some really interesting data
structures.
DOUG LLOYD: Yeah, we can't to have that conversation about what
is a tree, or really even an effective hash table,
or a linked list until we can get past this point in the course.
DAVID MALAN: And I mean Java at the end of the day, which many students
experience in high school and APCS and so forth, I
mean it has references which effectively allow us to do these kinds of things,
but there is an abstraction.
There there's a good defense mechanism built
into them, which was certainly by design,
but now I mean we are really getting as close to the computer's RAM
or virtual memory as we can, by just using these lower level primitives.
DOUG LLOYD: I remember this being a turning point for me
as a student in CS50 when it finally sort of clicked for me.
It does really give me the power to get beyond the lower level stuff,
and granted we're still on a low level language,
but you can get into a slightly higher point.
DAVID MALAN: Yeah, and I forgot to do it this year,
last year you'll recall we had someone come up on stage
and she did the little switcheroo with the milk and water--
milk and orange juice--
As an aside-- you're not going to see here in this particular year--
but we use milk and orange juice because one, it's so colorfully different,
but also because then I can kind of, sort of,
get away with using oil as a fake substitute for orange juice,
and then you can do a switcheroo that just magically works,
much like the [INAUDIBLE] trick with which some of you might be familiar,
by which you can indeed swap two variables without the need
for a temporary storage variable.
Which is to say, can't offer to you in this year's CS50,
but there's a really good example in 2015s
wherein we use that as part of this demonstration.
Still doesn't address the fundamental problem
at hand, which is that swap does not work as intended here,
and that's why we have to introduce now the syntax
with which to declare these things as pointers.
And this, I will admit, this is my biggest regret with the design of C
syntactically, is that the star or asterisk
operator was used both to declare a pointer, but also to dereference it.
Because it's such a subtlety and I try to emphasize to students that when
the star comes immediately after a type and is immediately
prefixing a variable, that it's declaring the pointer,
but if there's no mention of that type later when you see the star,
it is dereferencing it.
I mean not to mention that star is also used for multiplication,
so I don't know what I would have picked otherwise,
but that conceptual difference.
DOUG LLOYD: It's a hang up.
It's a real hang up.
I think it just adds to the--
not allure-- but the drama that can sometimes surround pointers.
It's silly little things like this, the syntax trip ups
that, once you get your head around this concept,
like yeah, it's not that strange, but that
is just an unnecessary obstacle built into the language I think to get there.
DAVID MALAN: I agree.
I will say a perk of CS50 IDE this year and the debugger that's now built in,
thanks to Dan, and [? Kareem, ?] and others,
is the fact that you can now see these changes happening in real-time.
In the past if I were to use the command line version of GDP
it's not all that enlightening to be showing the code here,
then have to run the program, and then hit print,
or display the value of some variable.
Just seeing it in real-time I think it's pretty powerful, not to mention
having the big highlight over the line of code in which you're in,
you can really see the state that's being changed.
Not to mention the fact that the debugger also
shows you the call stack so you can see very clearly you're inside
of swaps frame, not inside of mains.
DOUG LLOYD: And main is just kind of waiting for you to finish your work.
DAVID MALAN: Exactly.
So a lot of features this year have come together
to hopefully paint a much clearer picture as to what's going on
and what isn't going on underneath the hood
Here.
Now A is about to become B.