1 00:00:00,000 --> 00:00:00,666 [VIDEO PLAYBACK] 2 00:00:00,666 --> 00:00:04,450 DAVID MALAN: So what else can go wrong now that we have the ability to touch, 3 00:00:04,450 --> 00:00:06,780 correctly or incorrectly, any memory that we add-- 4 00:00:06,780 --> 00:00:06,830 [END PLAYBACK] 5 00:00:06,830 --> 00:00:07,870 DAVID MALAN: So at this point in the term, too, 6 00:00:07,870 --> 00:00:09,970 it's a nice opportunity to dive into another topic 7 00:00:09,970 --> 00:00:12,950 that you wouldn't typically see, I think, at this point in the semester, 8 00:00:12,950 --> 00:00:16,033 or let alone an intro class, which is a discussion of memory leaks, right? 9 00:00:16,033 --> 00:00:19,090 Because this language, C, is not giving us that feature, at least 10 00:00:19,090 --> 00:00:21,790 in the general case, so we have added it to the CS50 library. 11 00:00:21,790 --> 00:00:24,290 DOUG LLOYD: Memory leaks are actually kind of a tricky topic 12 00:00:24,290 --> 00:00:26,800 to teach, because in the context of CS50, in the programs 13 00:00:26,800 --> 00:00:31,341 that we're writing, we're not really going to notice a memory leak for us. 14 00:00:31,341 --> 00:00:32,299 DAVID MALAN: Right, no. 15 00:00:32,299 --> 00:00:34,590 But I think it's important for correctness for students 16 00:00:34,590 --> 00:00:37,810 to also realize the causal relationship between them allocating 17 00:00:37,810 --> 00:00:41,860 memory and the program itself ultimately leaking if they don't actually 18 00:00:41,860 --> 00:00:42,640 give it back. 19 00:00:42,640 --> 00:00:46,600 And what's nice too is that in Valgrind, even though its syntax is not nearly as 20 00:00:46,600 --> 00:00:50,080 accessible as would be ideal and its output even more so, 21 00:00:50,080 --> 00:00:53,830 it's a very deterministic tool that can tell students whether or not 22 00:00:53,830 --> 00:00:56,740 their code is leaking, and better still, if they are touching memory 23 00:00:56,740 --> 00:01:00,190 at some point that they shouldn't be, even it's not just by luck 24 00:01:00,190 --> 00:01:01,815 inducing an actual site fault for them. 25 00:01:01,815 --> 00:01:03,814 DOUG LLOYD: Right, and this year, we're actually 26 00:01:03,814 --> 00:01:06,880 able again to teach Valgrind earlier because we revamped the CS50 library 27 00:01:06,880 --> 00:01:10,000 to have garbage collection, where previously Valgrind would throw sort 28 00:01:10,000 --> 00:01:12,760 of false errors out because get string technically 29 00:01:12,760 --> 00:01:14,320 leaked memory when you used it. 30 00:01:14,320 --> 00:01:16,695 DAVID MALAN: And now, you don't have to worry about that. 31 00:01:16,695 --> 00:01:20,500 And, as we discussed when we looked again at help 50, the new command line 32 00:01:20,500 --> 00:01:23,410 tool with which students can translate a program's 33 00:01:23,410 --> 00:01:26,639 output to more human friendly messages, Valgrind 34 00:01:26,639 --> 00:01:28,180 was kind of top of our list for that. 35 00:01:28,180 --> 00:01:30,535 I mean, the output of Valgrind is just so unnecessarily complex. 36 00:01:30,535 --> 00:01:32,560 Like, I don't know why the authors decided 37 00:01:32,560 --> 00:01:35,979 to just repeat the process ID of the thing again and again 38 00:01:35,979 --> 00:01:38,020 and again and again all along the left-hand side. 39 00:01:38,020 --> 00:01:40,480 It's not all that useful, and it's certainly 40 00:01:40,480 --> 00:01:43,610 distracting except when maybe you might have multiple processes interacting 41 00:01:43,610 --> 00:01:44,860 and you want to see them both. 42 00:01:44,860 --> 00:01:46,950 But in our case, it's just it's just a distraction. 43 00:01:46,950 --> 00:01:47,500 DOUG LLOYD: Yeah, it's just a distraction. 44 00:01:47,500 --> 00:01:49,720 DAVID MALAN: And so, help 50 now, thankfully, 45 00:01:49,720 --> 00:01:51,850 will translate those messages for students in a way 46 00:01:51,850 --> 00:01:55,046 that I don't think is to their detriment in terms of understanding, 47 00:01:55,046 --> 00:01:56,920 because indeed, in reality, they're not going 48 00:01:56,920 --> 00:02:00,290 to use Valgrind all that likely beyond the scope of CS50 49 00:02:00,290 --> 00:02:02,800 if they're generally programming in other languages. 50 00:02:02,800 --> 00:02:04,250 But they're still seeing the output, and then we're 51 00:02:04,250 --> 00:02:07,416 helping them translate it, much like a TF or CA would if he or she came over 52 00:02:07,416 --> 00:02:09,180 to you to explain.