1 00:00:00,000 --> 00:00:00,187 2 00:00:00,187 --> 00:00:01,020 DAVID MALAN: --here. 3 00:00:01,020 --> 00:00:03,611 It seems to be broken still. 4 00:00:03,611 --> 00:00:06,360 In fact, let me go ahead and open another example rather than type 5 00:00:06,360 --> 00:00:07,260 this one out-- 6 00:00:07,260 --> 00:00:09,540 DOUG LLOYD: For many years I taught a section 7 00:00:09,540 --> 00:00:13,530 for students who were less comfortable, and there was definitely 8 00:00:13,530 --> 00:00:16,260 some anxiety when we came into this point in the course. 9 00:00:16,260 --> 00:00:20,730 For some students I think pointers can be really intimidating, 10 00:00:20,730 --> 00:00:23,010 and you hear the horror stories about how 11 00:00:23,010 --> 00:00:27,570 they can cause you to crash your program if you don't handle them correctly. 12 00:00:27,570 --> 00:00:31,380 I think that this example is a really good way to introduce why we need them 13 00:00:31,380 --> 00:00:32,100 and what they do. 14 00:00:32,100 --> 00:00:33,808 DAVID MALAN: I think so, and I don't know 15 00:00:33,808 --> 00:00:38,040 where it is that I sort of latch onto this as the entry 16 00:00:38,040 --> 00:00:40,620 point for our discussion of pointers, but I 17 00:00:40,620 --> 00:00:42,940 like how we introduce a pretty reasonable goal, 18 00:00:42,940 --> 00:00:44,940 let's make a function that just swaps two values 19 00:00:44,940 --> 00:00:47,370 and see that it's broken, because all of a sudden 20 00:00:47,370 --> 00:00:52,402 it sort of shatters the abstraction that has been our discussion of functions, 21 00:00:52,402 --> 00:00:54,610 and programming, and what it can do, and you realize, 22 00:00:54,610 --> 00:00:56,910 wait a minute this is not working as I expect it does, 23 00:00:56,910 --> 00:00:59,329 and we have to go lower level and actually see 24 00:00:59,329 --> 00:01:02,370 what's going on in the stack, and what's going on ultimately with memory. 25 00:01:02,370 --> 00:01:05,340 And I think that's by far the most effective way to explain this. 26 00:01:05,340 --> 00:01:09,220 Don't hide the fact anymore that there is a whole stack going on, 27 00:01:09,220 --> 00:01:11,550 there's ultimately heap as well, and there are indeed 28 00:01:11,550 --> 00:01:15,390 pointers by which we delete those chunks of memory. 29 00:01:15,390 --> 00:01:18,090 Just show it to students, and actually dive into that layer. 30 00:01:18,090 --> 00:01:20,220 And so this is indeed the sort of thing that I 31 00:01:20,220 --> 00:01:24,102 think many teachers stray away from C because of, because 32 00:01:24,102 --> 00:01:25,310 of these lower level details. 33 00:01:25,310 --> 00:01:27,930 But I think this is where things really start to get interesting, 34 00:01:27,930 --> 00:01:30,060 and starts to give us the vocabulary so to speak with which 35 00:01:30,060 --> 00:01:33,130 we can start building and stitching together some really interesting data 36 00:01:33,130 --> 00:01:33,630 structures. 37 00:01:33,630 --> 00:01:36,255 DOUG LLOYD: Yeah, we can't to have that conversation about what 38 00:01:36,255 --> 00:01:38,760 is a tree, or really even an effective hash table, 39 00:01:38,760 --> 00:01:41,427 or a linked list until we can get past this point in the course. 40 00:01:41,427 --> 00:01:44,385 DAVID MALAN: And I mean Java at the end of the day, which many students 41 00:01:44,385 --> 00:01:46,620 experience in high school and APCS and so forth, I 42 00:01:46,620 --> 00:01:50,800 mean it has references which effectively allow us to do these kinds of things, 43 00:01:50,800 --> 00:01:52,140 but there is an abstraction. 44 00:01:52,140 --> 00:01:54,060 There there's a good defense mechanism built 45 00:01:54,060 --> 00:01:56,670 into them, which was certainly by design, 46 00:01:56,670 --> 00:02:00,480 but now I mean we are really getting as close to the computer's RAM 47 00:02:00,480 --> 00:02:04,027 or virtual memory as we can, by just using these lower level primitives. 48 00:02:04,027 --> 00:02:06,360 DOUG LLOYD: I remember this being a turning point for me 49 00:02:06,360 --> 00:02:10,669 as a student in CS50 when it finally sort of clicked for me. 50 00:02:10,669 --> 00:02:15,491 It does really give me the power to get beyond the lower level stuff, 51 00:02:15,491 --> 00:02:17,490 and granted we're still on a low level language, 52 00:02:17,490 --> 00:02:21,306 but you can get into a slightly higher point. 53 00:02:21,306 --> 00:02:23,430 DAVID MALAN: Yeah, and I forgot to do it this year, 54 00:02:23,430 --> 00:02:27,180 last year you'll recall we had someone come up on stage 55 00:02:27,180 --> 00:02:31,230 and she did the little switcheroo with the milk and water-- 56 00:02:31,230 --> 00:02:32,820 milk and orange juice-- 57 00:02:32,820 --> 00:02:35,867 58 00:02:35,867 --> 00:02:38,700 As an aside-- you're not going to see here in this particular year-- 59 00:02:38,700 --> 00:02:42,225 but we use milk and orange juice because one, it's so colorfully different, 60 00:02:42,225 --> 00:02:44,100 but also because then I can kind of, sort of, 61 00:02:44,100 --> 00:02:47,252 get away with using oil as a fake substitute for orange juice, 62 00:02:47,252 --> 00:02:49,710 and then you can do a switcheroo that just magically works, 63 00:02:49,710 --> 00:02:52,751 much like the [INAUDIBLE] trick with which some of you might be familiar, 64 00:02:52,751 --> 00:02:55,440 by which you can indeed swap two variables without the need 65 00:02:55,440 --> 00:02:57,610 for a temporary storage variable. 66 00:02:57,610 --> 00:03:00,750 Which is to say, can't offer to you in this year's CS50, 67 00:03:00,750 --> 00:03:03,090 but there's a really good example in 2015s 68 00:03:03,090 --> 00:03:05,475 wherein we use that as part of this demonstration. 69 00:03:05,475 --> 00:03:07,350 Still doesn't address the fundamental problem 70 00:03:07,350 --> 00:03:11,040 at hand, which is that swap does not work as intended here, 71 00:03:11,040 --> 00:03:13,470 and that's why we have to introduce now the syntax 72 00:03:13,470 --> 00:03:15,840 with which to declare these things as pointers. 73 00:03:15,840 --> 00:03:19,530 And this, I will admit, this is my biggest regret with the design of C 74 00:03:19,530 --> 00:03:22,320 syntactically, is that the star or asterisk 75 00:03:22,320 --> 00:03:27,360 operator was used both to declare a pointer, but also to dereference it. 76 00:03:27,360 --> 00:03:31,650 Because it's such a subtlety and I try to emphasize to students that when 77 00:03:31,650 --> 00:03:34,260 the star comes immediately after a type and is immediately 78 00:03:34,260 --> 00:03:36,930 prefixing a variable, that it's declaring the pointer, 79 00:03:36,930 --> 00:03:40,140 but if there's no mention of that type later when you see the star, 80 00:03:40,140 --> 00:03:41,280 it is dereferencing it. 81 00:03:41,280 --> 00:03:43,630 I mean not to mention that star is also used for multiplication, 82 00:03:43,630 --> 00:03:45,510 so I don't know what I would have picked otherwise, 83 00:03:45,510 --> 00:03:46,630 but that conceptual difference. 84 00:03:46,630 --> 00:03:47,100 DOUG LLOYD: It's a hang up. 85 00:03:47,100 --> 00:03:47,933 It's a real hang up. 86 00:03:47,933 --> 00:03:50,440 I think it just adds to the-- 87 00:03:50,440 --> 00:03:54,600 not allure-- but the drama that can sometimes surround pointers. 88 00:03:54,600 --> 00:03:57,840 It's silly little things like this, the syntax trip ups 89 00:03:57,840 --> 00:04:01,890 that, once you get your head around this concept, 90 00:04:01,890 --> 00:04:04,470 like yeah, it's not that strange, but that 91 00:04:04,470 --> 00:04:07,735 is just an unnecessary obstacle built into the language I think to get there. 92 00:04:07,735 --> 00:04:08,610 DAVID MALAN: I agree. 93 00:04:08,610 --> 00:04:13,947 I will say a perk of CS50 IDE this year and the debugger that's now built in, 94 00:04:13,947 --> 00:04:15,780 thanks to Dan, and [? Kareem, ?] and others, 95 00:04:15,780 --> 00:04:18,720 is the fact that you can now see these changes happening in real-time. 96 00:04:18,720 --> 00:04:21,660 In the past if I were to use the command line version of GDP 97 00:04:21,660 --> 00:04:25,500 it's not all that enlightening to be showing the code here, 98 00:04:25,500 --> 00:04:27,990 then have to run the program, and then hit print, 99 00:04:27,990 --> 00:04:29,780 or display the value of some variable. 100 00:04:29,780 --> 00:04:32,780 Just seeing it in real-time I think it's pretty powerful, not to mention 101 00:04:32,780 --> 00:04:35,530 having the big highlight over the line of code in which you're in, 102 00:04:35,530 --> 00:04:37,710 you can really see the state that's being changed. 103 00:04:37,710 --> 00:04:39,626 Not to mention the fact that the debugger also 104 00:04:39,626 --> 00:04:43,020 shows you the call stack so you can see very clearly you're inside 105 00:04:43,020 --> 00:04:44,719 of swaps frame, not inside of mains. 106 00:04:44,719 --> 00:04:47,760 DOUG LLOYD: And main is just kind of waiting for you to finish your work. 107 00:04:47,760 --> 00:04:48,635 DAVID MALAN: Exactly. 108 00:04:48,635 --> 00:04:51,480 So a lot of features this year have come together 109 00:04:51,480 --> 00:04:54,759 to hopefully paint a much clearer picture as to what's going on 110 00:04:54,759 --> 00:04:56,550 and what isn't going on underneath the hood 111 00:04:56,550 --> 00:04:57,049 Here. 112 00:04:57,049 --> 00:04:59,460 Now A is about to become B.