[ { "text": "[MUSIC PLAYING]", "start": 0.0, "duration": 3.479 }, { "text": "DAVID J. MALAN: All right, this\nis CS50, and this is week 4.", "start": 15.407, "duration": 4.873 }, { "text": "So let's consider first what we did\nlast time, which was focus on numbers", "start": 20.28, "duration": 5.15 }, { "text": "and focus on how we can search\nthem and how we can sort them.", "start": 25.43, "duration": 3.44 }, { "text": "And in particular, we took a\nlook at a number of algorithms", "start": 28.87, "duration": 3.2 }, { "text": "that we finally gave names to, a couple\nof which we actually saw back in week", "start": 32.07, "duration": 3.26 }, { "text": "0, but didn't treat as formally.", "start": 35.33, "duration": 2.06 }, { "text": "So we looked more formally\nlast time at linear search,", "start": 37.39, "duration": 2.84 }, { "text": "this process of searching elements\nfrom left to right or right to left", "start": 40.23, "duration": 3.74 }, { "text": "as though you're walking along\na line looking for some value.", "start": 43.97, "duration": 2.94 }, { "text": "We looked at binary search, a\ndivide and conquer algorithm", "start": 46.91, "duration": 2.94 }, { "text": "whereby we started, in\nthis case, in the middle,", "start": 49.85, "duration": 2.092 }, { "text": "and then we looked left to\nright and then went to the left,", "start": 51.942, "duration": 2.458 }, { "text": "went to the right, and then did\nthe same problem again and again.", "start": 54.4, "duration": 2.87 }, { "text": "We looked also at bubble sort.", "start": 57.27, "duration": 1.912 }, { "text": "How do you get to the\npoint where you can", "start": 59.182, "duration": 1.708 }, { "text": "use an algorithm like\nbinary search, which", "start": 60.89, "duration": 2.29 }, { "text": "assumes that your inputs are sorted?", "start": 63.18, "duration": 2.32 }, { "text": "So how do you get to that sorted state?", "start": 65.5, "duration": 1.76 }, { "text": "Well, bubble sort was one\nalgorithm that essentially", "start": 67.26, "duration": 2.34 }, { "text": "swapped elements pairwise from\nleft to right, from left to right,", "start": 69.6, "duration": 3.55 }, { "text": "until finally the biggest\nelements bubbled up to the top,", "start": 73.15, "duration": 2.48 }, { "text": "and the whole list\nwas ultimately sorted.", "start": 75.63, "duration": 2.3 }, { "text": "But we also looked at selection sort.", "start": 77.93, "duration": 1.74 }, { "text": "And selection sort is the\nname implied, selected,", "start": 79.67, "duration": 3.11 }, { "text": "then smallest element again and again\nand again, plucking it out of the list,", "start": 82.78, "duration": 4.22 }, { "text": "and putting it where it belongs.", "start": 87.0, "duration": 1.55 }, { "text": "Insertion sort, meanwhile, walked\nthrough the list really just once,", "start": 88.55, "duration": 3.55 }, { "text": "and as it encountered\neach element, it then", "start": 92.1, "duration": 2.34 }, { "text": "inserted it into the sorted half of\nthe list, the left half of the list", "start": 94.44, "duration": 4.7 }, { "text": "in its proper place.", "start": 99.14, "duration": 1.53 }, { "text": "And then lastly, and where we really\nconcluded and really got more bang", "start": 100.67, "duration": 4.6 }, { "text": "for our buck was this\nfinal algorithm merge", "start": 105.27, "duration": 2.36 }, { "text": "sort, which actually solved this same\nproblem no less correctly, but so much", "start": 107.63, "duration": 5.2 }, { "text": "more efficiently.", "start": 112.83, "duration": 1.12 }, { "text": "And indeed, lends itself perhaps\nto a fundamentally better design", "start": 113.95, "duration": 3.17 }, { "text": "in so far as it leverages a much\nbetter running time, so to speak.", "start": 117.12, "duration": 4.46 }, { "text": "In fact, we started to ascribe\nthese kinds of formulas", "start": 121.58, "duration": 2.9 }, { "text": "to the running times of or the\nperformance of our algorithms n", "start": 124.48, "duration": 3.49 }, { "text": "squared, otherwise known\nas quadratic, whereby", "start": 127.97, "duration": 2.66 }, { "text": "you take n times n number of\nsteps or minutes or seconds", "start": 130.63, "duration": 3.37 }, { "text": "or some unit of measure, n log in,\nwhich is actually what we achieved", "start": 134.0, "duration": 3.8 }, { "text": "with the merge source,\nwhich was much faster", "start": 137.8, "duration": 2.48 }, { "text": "recall than the end squared algorithms\nlike bubble sort and selection sort", "start": 140.28, "duration": 3.17 }, { "text": "and insertion sort in\ntheir upper bounds.", "start": 143.45, "duration": 2.32 }, { "text": "n would be something linear, like\na straight line relationship.", "start": 145.77, "duration": 2.85 }, { "text": "Logarithmic of n, log\nof n, log base 2 of n,", "start": 148.62, "duration": 2.944 }, { "text": "technically, would be\nsomething like week 0's divide", "start": 151.564, "duration": 2.166 }, { "text": "and conquer approach\nlooking for Mike Smith,", "start": 153.73, "duration": 2.18 }, { "text": "as well as last time's binary search.", "start": 155.91, "duration": 2.174 }, { "text": "And then one, which doesn't\nhave to be literally one step.", "start": 158.084, "duration": 2.416 }, { "text": "Maybe it's two, maybe it's 10, but\nit is a fixed finite constant number", "start": 160.5, "duration": 4.2 }, { "text": "of steps.", "start": 164.7, "duration": 0.63 }, { "text": "And that too might be the\nrunning time of some algorithm.", "start": 165.33, "duration": 2.56 }, { "text": "Now how do we describe the\nrunning times of algorithms?", "start": 167.89, "duration": 2.81 }, { "text": "Well, we use some special\nnotation, asymptotic notation,", "start": 170.7, "duration": 3.53 }, { "text": "so to speak, which while it might\nlook cryptic at first glance,", "start": 174.23, "duration": 2.71 }, { "text": "really is just a handy way of succinctly\nexpressing the fact that you know what", "start": 176.94, "duration": 3.81 }, { "text": "the upper bound on some\nalgorithms running time is,", "start": 180.75, "duration": 2.7 }, { "text": "what the lower bounds on some\nalgorithms running time is.", "start": 183.45, "duration": 2.62 }, { "text": "And if those are one in\nthe same formulas in theta,", "start": 186.07, "duration": 3.09 }, { "text": "do you have a coincidence of the two\nof big O, so speak, and capital theta.", "start": 189.16, "duration": 5.49 }, { "text": "So that, while Greek,\nliterally is just a way", "start": 194.65, "duration": 2.17 }, { "text": "of expressing a bit more succinctly\nwhat these running times are.", "start": 196.82, "duration": 2.96 }, { "text": "And we'll continue to\nrevisit this issue as we", "start": 199.78, "duration": 1.916 }, { "text": "look at more algorithms and\nsoon data structures still.", "start": 201.696, "duration": 2.894 }, { "text": "But this time, we apologize.", "start": 204.59, "duration": 3.58 }, { "text": "We pull back a layer here and admit\nthat there is no such thing as a string.", "start": 208.17, "duration": 6.73 }, { "text": "Indeed, all this time we've been saying\nthat there's ins, and there's floats,", "start": 214.9, "duration": 3.35 }, { "text": "and there's chars, and\nthere's doubles and more.", "start": 218.25, "duration": 2.075 }, { "text": "And we've also been\nsaying there are strings,", "start": 220.325, "duration": 1.875 }, { "text": "but there really aren't strings.", "start": 222.2, "duration": 1.76 }, { "text": "This is sort of a figment of the\nimagination of our so-called CS50", "start": 223.96, "duration": 3.25 }, { "text": "library.", "start": 227.21, "duration": 0.85 }, { "text": "But it's a pedagogical\nsimplification that we've", "start": 228.06, "duration": 2.28 }, { "text": "been using for the past\nseveral weeks, so as to not get", "start": 230.34, "duration": 3.32 }, { "text": "lost in the weeds, the\nlower level implementation", "start": 233.66, "duration": 2.234 }, { "text": "details of what a string\nis, so that we can just get", "start": 235.894, "duration": 2.166 }, { "text": "real work done in these first weeks.", "start": 238.06, "duration": 2.01 }, { "text": "But now we'll begin to\nlook underneath that hood", "start": 240.07, "duration": 2.58 }, { "text": "and see what a string actually\nis and what the implications are.", "start": 242.65, "duration": 3.87 }, { "text": "And it turns out, while more\ncomplicated in some sense,", "start": 246.52, "duration": 3.18 }, { "text": "it really just boils down\nto some first principles,", "start": 249.7, "duration": 2.95 }, { "text": "what it is the computer is\ndoing underneath that hood.", "start": 252.65, "duration": 3.01 }, { "text": "So let's take a look at string first\nby way of a couple of examples.", "start": 255.66, "duration": 4.9 }, { "text": "Let's go in to CS50 IDE, create a new\nfile, save it as Compare zero dot C,", "start": 260.56, "duration": 5.27 }, { "text": "and look at the little program\nthat actually doesn't necessarily", "start": 265.83, "duration": 3.45 }, { "text": "do what we think it's going to do.", "start": 269.28, "duration": 2.72 }, { "text": "In particular, let me go\nahead and include our typical,", "start": 272.0, "duration": 2.91 }, { "text": "include CS 50 dot H. And let me go ahead\nand include standard IO dot H as well.", "start": 274.91, "duration": 5.29 }, { "text": "Let me go ahead and use\nmain void, so I'm not", "start": 280.2, "duration": 2.01 }, { "text": "going to worry about any\ncommand line arguments for now.", "start": 282.21, "duration": 2.47 }, { "text": "And then I'm going to go ahead\nand just prompt the user.", "start": 284.68, "duration": 2.333 }, { "text": "Hey user, give me a string\ncalled S for instance.", "start": 287.013, "duration": 2.144 }, { "text": "And then I'm going to have no newline.", "start": 289.157, "duration": 1.583 }, { "text": "I just want that all in one same line.", "start": 290.74, "duration": 1.99 }, { "text": "Now I'm going to go\nahead and do string S,", "start": 292.73, "duration": 3.22 }, { "text": "gets gets string, open\nparen and close parens,", "start": 295.95, "duration": 2.51 }, { "text": "so as to get a string from the user.", "start": 298.46, "duration": 2.12 }, { "text": "And then, let me do this.", "start": 300.58, "duration": 1.09 }, { "text": "Let me also print T colon and ask\nthe user, essentially, for a string", "start": 301.67, "duration": 4.33 }, { "text": "that I'll call T, since T\ncomes after S quite simply.", "start": 306.0, "duration": 4.32 }, { "text": "And now let me just compare these\nstrings as the filename suggests.", "start": 310.32, "duration": 3.37 }, { "text": "So I know how to compare,\nnot with if s equals t,", "start": 313.69, "duration": 4.706 }, { "text": "because that's the assignment operator.", "start": 318.396, "duration": 1.624 }, { "text": "But we know that s equals equals t\nshould compare the values on the left", "start": 320.02, "duration": 5.062 }, { "text": "and on the right.", "start": 325.082, "duration": 0.708 }, { "text": "So let's try this.", "start": 325.79, "duration": 0.88 }, { "text": "So if s equals equals t, then I'm\ngoing to go ahead and print out same.", "start": 326.67, "duration": 5.46 }, { "text": "Elts, they are presumably\ndifference, so I'm", "start": 332.13, "duration": 2.43 }, { "text": "going to go ahead and print out\ndifferent with a newline character", "start": 334.56, "duration": 3.98 }, { "text": "and then save it.", "start": 338.54, "duration": 1.08 }, { "text": "So a pretty simple program, so let me\ngo down into my terminal window and run", "start": 339.62, "duration": 3.69 }, { "text": "make compare 0.", "start": 343.31, "duration": 2.12 }, { "text": "Now let me go ahead and\nrun dot slash compare 0. s,", "start": 345.43, "duration": 4.49 }, { "text": "I'm going to go ahead\nand type in Zamyla.", "start": 349.92, "duration": 3.84 }, { "text": "And I'm going to go ahead\nnow and type in Maria.", "start": 353.76, "duration": 3.127 }, { "text": "OK, and they're different.", "start": 356.887, "duration": 1.083 }, { "text": "I expected as much.", "start": 357.97, "duration": 1.22 }, { "text": "Now let's go ahead and run this again.", "start": 359.19, "duration": 1.83 }, { "text": "Again with Zamyla.", "start": 361.02, "duration": 1.696 }, { "text": "And let's just say\nZamyla again, different.", "start": 362.716, "duration": 4.744 }, { "text": "What did I do wrong?", "start": 367.46, "duration": 1.004 }, { "text": "Let me try this again.", "start": 368.464, "duration": 0.916 }, { "text": "Maybe it's the capitalization.", "start": 369.38, "duration": 1.63 }, { "text": "So Zamyla in all lower case, different.", "start": 371.01, "duration": 5.11 }, { "text": "Well, maybe it's just Zamyla's name.", "start": 376.12, "duration": 1.69 }, { "text": "Let me try Rob or RLB?", "start": 377.81, "duration": 3.76 }, { "text": "How about RLB?", "start": 381.57, "duration": 1.2 }, { "text": "Those are different, as is Rob and Rob.", "start": 382.77, "duration": 2.86 }, { "text": "So what is going on?", "start": 385.63, "duration": 1.82 }, { "text": "Those strings pretty\nmuch look the same to me.", "start": 387.45, "duration": 2.12 }, { "text": "I'm typing the same\nincantation of strings,", "start": 389.57, "duration": 2.273 }, { "text": "so what is it that's going on here?", "start": 391.843, "duration": 2.777 }, { "text": "You know what, let me do a\ntest with something else.", "start": 394.62, "duration": 2.46 }, { "text": "Let me go ahead and\ncreate a new file here.", "start": 397.08, "duration": 2.61 }, { "text": "I'm going to call this copyzero.c.", "start": 399.69, "duration": 1.66 }, { "text": "Maybe I'm just misunderstanding\nhow comparison works.", "start": 401.35, "duration": 2.78 }, { "text": "But surely I should be able to copy\na string and make an identical copy,", "start": 404.13, "duration": 3.11 }, { "text": "so let's do that now.", "start": 407.24, "duration": 1.14 }, { "text": "Let me go ahead and create\na file called copyzero.c.", "start": 408.38, "duration": 4.58 }, { "text": "And let me do an include cs50.h.", "start": 412.96, "duration": 2.75 }, { "text": "Include standard io.h into\nmain void, so just as before.", "start": 415.71, "duration": 6.027 }, { "text": "And now, let me go ahead and\njust prompt the user like before.", "start": 421.737, "duration": 2.583 }, { "text": "Give me a string s.", "start": 424.32, "duration": 1.3 }, { "text": "And I'll put that in a variable,\ns, calling get string as before.", "start": 425.62, "duration": 4.23 }, { "text": "And now, I remember from prior\nclasses that I'm supposed to do,", "start": 429.85, "duration": 5.09 }, { "text": "if s equals equals null, maybe I\nneed to do some error checking here.", "start": 434.94, "duration": 4.71 }, { "text": "And if that's the case, I'm\ngoing to arbitrarily return 1.", "start": 439.65, "duration": 2.48 }, { "text": "So recall that main can return values.", "start": 442.13, "duration": 2.12 }, { "text": "0 means all is well.", "start": 444.25, "duration": 1.2 }, { "text": "1 or any other non-zero value\nmeans something is wrong.", "start": 445.45, "duration": 2.6 }, { "text": "So maybe that's what I was doing wrong\nearlier, just not error checking.", "start": 448.05, "duration": 3.0 }, { "text": "So let's try that.", "start": 451.05, "duration": 1.04 }, { "text": "Now, let me declare a variable of type\nstring called t and assign it to s.", "start": 452.09, "duration": 5.94 }, { "text": "So in other words, I\nwant to copy s into t.", "start": 458.03, "duration": 3.09 }, { "text": "And I know that this happens\nfrom the right to the left.", "start": 461.12, "duration": 2.35 }, { "text": "So that's as I think it should be.", "start": 463.47, "duration": 2.41 }, { "text": "And then, if strleng of\nt is greater than 0--", "start": 465.88, "duration": 7.302 }, { "text": "you know what, I'm going\nto do one other thing.", "start": 473.182, "duration": 1.958 }, { "text": "Not just copy it.", "start": 475.14, "duration": 0.924 }, { "text": "I'm going to go ahead and do this.", "start": 476.064, "duration": 1.416 }, { "text": "Recall, that you can\ntreat strings as arrays.", "start": 477.48, "duration": 2.02 }, { "text": "So I'm going to say the zeroth\ncharacter, the first character of t--", "start": 479.5, "duration": 3.477 }, { "text": "you know what?", "start": 482.977, "duration": 0.583 }, { "text": "I want to uppercase it.", "start": 483.56, "duration": 1.61 }, { "text": "I want to really make sure that\nthese two strings are indeed", "start": 485.17, "duration": 2.79 }, { "text": "different as I intend.", "start": 487.96, "duration": 1.58 }, { "text": "And let's go ahead now and do this.", "start": 489.54, "duration": 3.3 }, { "text": "Let me go ahead now and do printf of s.", "start": 492.84, "duration": 4.5 }, { "text": "And let me plug-in s's\nvalue in a new line.", "start": 497.34, "duration": 2.18 }, { "text": "Plug-in s.", "start": 499.52, "duration": 0.7 }, { "text": "And then printf t:%s\nfor a placeholder again.", "start": 500.22, "duration": 4.87 }, { "text": "Comma t semi-colon.", "start": 505.09, "duration": 1.74 }, { "text": "So in other words, if the length\nof t is greater than zero,", "start": 506.83, "duration": 3.69 }, { "text": "let's capitalize it by\nchanging that first character.", "start": 510.52, "duration": 2.764 }, { "text": "And then, just print out s and t.", "start": 513.284, "duration": 1.375 }, { "text": "And surely, by capitalizing only t, I\nshould see only one capitalized word.", "start": 514.659, "duration": 5.091 }, { "text": "Now, I'm using both\nstrleng and two upper.", "start": 519.75, "duration": 1.812 }, { "text": "So rather than let Clang have a chance\nto yell at me, I'm going to go in here", "start": 521.562, "duration": 3.208 }, { "text": "and preemptively add ctype.h.", "start": 524.77, "duration": 3.056 }, { "text": "Which recall is a library\nyou might have seen", "start": 527.826, "duration": 1.874 }, { "text": "or certainly will soon see that\nhas a number of functions in it.", "start": 529.7, "duration": 3.34 }, { "text": "Among them, two upper.", "start": 533.04, "duration": 1.18 }, { "text": "And then, I also need to include\nstring.h, so that I can use strlen--", "start": 534.22, "duration": 4.094 }, { "text": "the function that gives me\nthe length of the string.", "start": 538.314, "duration": 2.166 }, { "text": "So Clang would have yelled at me if I\nforgot that, but let me preemptively", "start": 540.48, "duration": 2.89 }, { "text": "solve that problem.", "start": 543.37, "duration": 0.82 }, { "text": "And now, do make copies zero.", "start": 544.19, "duration": 2.35 }, { "text": "All seems to be well.", "start": 546.54, "duration": 1.03 }, { "text": "Let's run dot slash copy 0.", "start": 547.57, "duration": 1.861 }, { "text": "All right.", "start": 549.431, "duration": 0.499 }, { "text": "Let's go ahead and type in-- how about\njust my own name in all lowercase.", "start": 549.93, "duration": 6.05 }, { "text": "Huh?", "start": 555.98, "duration": 1.31 }, { "text": "Now, why is this confusing?", "start": 557.29, "duration": 2.04 }, { "text": "So I wrote code that got a string\nfrom the user and stored it in s.", "start": 559.33, "duration": 5.53 }, { "text": "I then wrote code that\ndeclared a second variable, t.", "start": 564.86, "duration": 3.48 }, { "text": "And I set t equal to s,\nthereby making I would", "start": 568.34, "duration": 2.88 }, { "text": "think a copy-- as in past weeks\nof using the assignment operator.", "start": 571.22, "duration": 3.7 }, { "text": "Then down here, I made\nsure t was long enough.", "start": 574.92, "duration": 2.21 }, { "text": "That's just a quick sanity check.", "start": 577.13, "duration": 1.71 }, { "text": "And then on this line\nhere, I'm just saying,", "start": 578.84, "duration": 3.18 }, { "text": "set the first character of t, t bracket\n0, equal to the result of upper casing", "start": 582.02, "duration": 5.85 }, { "text": "the first character of t.", "start": 587.87, "duration": 2.13 }, { "text": "So the only code that's\ntouching t is this one here.", "start": 590.0, "duration": 3.37 }, { "text": "And yet, somehow my name gets\ncapitalized both in s and in t.", "start": 593.37, "duration": 5.38 }, { "text": "So what is it that's\nactually going on here?", "start": 598.75, "duration": 3.67 }, { "text": "It seems to be broken still.", "start": 602.42, "duration": 2.559 }, { "text": "In fact, let me go ahead and open\nanother example, rather than type", "start": 604.979, "duration": 2.791 }, { "text": "this one out ourselves.", "start": 607.77, "duration": 1.73 }, { "text": "Let me go ahead and open up\nan example called no swap.", "start": 609.5, "duration": 4.42 }, { "text": "As the name suggests,\nit's a bit of a spoiler.", "start": 613.92, "duration": 2.59 }, { "text": "I wrote this program in\nadvance to do the following.", "start": 616.51, "duration": 2.82 }, { "text": "First, I've included standard\nio.h, so I can use printf.", "start": 619.33, "duration": 3.61 }, { "text": "I have a prototype of my function\ncalled swap up here, because indeed,", "start": 622.94, "duration": 3.55 }, { "text": "the goal at hand I decided was I just\nwant to write a simple function that", "start": 626.49, "duration": 4.11 }, { "text": "swaps two values.", "start": 630.6, "duration": 1.69 }, { "text": "Given a and b, make b a and a, b.", "start": 632.29, "duration": 2.86 }, { "text": "And then, return.", "start": 635.15, "duration": 0.99 }, { "text": "So I'm just going to arbitrarily test\nthis out by declaring a variable x.", "start": 636.14, "duration": 3.06 }, { "text": "Setting it equal to 1.", "start": 639.2, "duration": 1.17 }, { "text": "A variable y setting it equal to 2.", "start": 640.37, "duration": 2.35 }, { "text": "And then, just as a sanity check, I'm\ngoing to print out x is such and such,", "start": 642.72, "duration": 3.3 }, { "text": "y is such and such.", "start": 646.02, "duration": 1.18 }, { "text": "And then, I'm going to\nclaim swapping dot dot dot.", "start": 647.2, "duration": 2.92 }, { "text": "And then, the key line\nis apparently this one.", "start": 650.12, "duration": 2.68 }, { "text": "Call a function called\nswap, passing in x and y.", "start": 652.8, "duration": 3.51 }, { "text": "And if I implemented swap correctly,\nthis should swap the two variables.", "start": 656.31, "duration": 4.09 }, { "text": "Thereafter, I'm going to claim swapped\nx as such and such, y as such and such.", "start": 660.4, "duration": 4.69 }, { "text": "So let's run this program and see\nwhat else is apparently a lie.", "start": 665.09, "duration": 5.19 }, { "text": "Make no swap in my source directory.", "start": 670.28, "duration": 6.62 }, { "text": "./noswap enter.", "start": 676.9, "duration": 3.71 }, { "text": "And if we scroll back up in my\nhistory, you'll see x is 1, y is 2.", "start": 680.61, "duration": 4.95 }, { "text": "Swapping swapped.", "start": 685.56, "duration": 1.66 }, { "text": "x is 1, y is 2.", "start": 687.22, "duration": 1.431 }, { "text": "All right.", "start": 688.651, "duration": 0.499 }, { "text": "So maybe just the swap\nfunction is buggy.", "start": 689.15, "duration": 1.96 }, { "text": "This isn't necessarily\nindicative of a misunderstanding.", "start": 691.11, "duration": 3.17 }, { "text": "So let's look at the\nimplementation of swap.", "start": 694.28, "duration": 2.01 }, { "text": "Swap returns no value and I think\nthat's OK, so long as it takes inputs.", "start": 696.29, "duration": 4.5 }, { "text": "Swap takes two inputs, an ints and\nan ints, called a and b respectively.", "start": 700.79, "duration": 4.64 }, { "text": "And then, let's consider how this works.", "start": 705.43, "duration": 1.72 }, { "text": "So I've declared a temporary\nvariable, called temp--", "start": 707.15, "duration": 3.225 }, { "text": "though I could call it anything I want.", "start": 710.375, "duration": 1.625 }, { "text": "And I'm storing in it the value a.", "start": 712.0, "duration": 2.28 }, { "text": "So I'm taking a-- and it's the number 1.", "start": 714.28, "duration": 1.975 }, { "text": "And I'm just temporarily\nstoring the number 1", "start": 716.255, "duration": 1.875 }, { "text": "in this additional temporary\nvariable, so that I now", "start": 718.13, "duration": 2.22 }, { "text": "have two copies of the number\none-- in a and in temp.", "start": 720.35, "duration": 3.34 }, { "text": "Then, I change the\nvalue of a to equal b.", "start": 723.69, "duration": 3.09 }, { "text": "So at this point in the story,\na should have a value of 2.", "start": 726.78, "duration": 3.92 }, { "text": "b should have a value of 2.", "start": 730.7, "duration": 1.62 }, { "text": "Which could be problematic, except\nI've saved the original value 1", "start": 732.32, "duration": 3.88 }, { "text": "in my temporary variable.", "start": 736.2, "duration": 1.59 }, { "text": "And so, indeed I change b to\nequal that temporary value.", "start": 737.79, "duration": 3.97 }, { "text": "So indeed, I can't do\nthis magical switcheroo.", "start": 741.76, "duration": 2.63 }, { "text": "Because if I simultaneously\ntook one value and one value", "start": 744.39, "duration": 3.0 }, { "text": "and put them in each other's\nplaces, I would essentially", "start": 747.39, "duration": 2.8 }, { "text": "risk clobbering one or the other.", "start": 750.19, "duration": 1.554 }, { "text": "Because the computer typically\ncan only do one thing", "start": 751.744, "duration": 2.166 }, { "text": "at a time underneath the hood,\neven though it's super fast.", "start": 753.91, "duration": 2.97 }, { "text": "So I run the risk of somehow getting\ntwo copies of that same value,", "start": 756.88, "duration": 5.43 }, { "text": "unless I'm careful.", "start": 762.31, "duration": 0.96 }, { "text": "I really want to take one value,\nput it in a temporary space.", "start": 763.27, "duration": 3.71 }, { "text": "Then, copy one value over.", "start": 766.98, "duration": 1.72 }, { "text": "And then, do that switcheroo.", "start": 768.7, "duration": 1.84 }, { "text": "But it doesn't seem to work.", "start": 770.54, "duration": 1.54 }, { "text": "And yet, this is a good\nopportunity for one more tool.", "start": 772.08, "duration": 2.98 }, { "text": "Let me do a further check.", "start": 775.06, "duration": 1.48 }, { "text": "Let me go ahead and run\ndebug 50 on no swap.", "start": 776.54, "duration": 4.5 }, { "text": "And you know what?", "start": 781.04, "duration": 0.75 }, { "text": "I'm going to set a break point right at\nthe beginning of this program on line", "start": 781.79, "duration": 3.33 }, { "text": "10 by simply clicking in the so-called\ngutter of the line number here.", "start": 785.12, "duration": 3.99 }, { "text": "A red dot appears, so that\nnow when I run debug 50,", "start": 789.11, "duration": 3.04 }, { "text": "notice my right hand bar pops out.", "start": 792.15, "duration": 2.17 }, { "text": "Notice that the line 10 in question\nis highlighted, because execution is", "start": 794.32, "duration": 3.96 }, { "text": "paused at this line.", "start": 798.28, "duration": 1.2 }, { "text": "Let's take a look at what's going on\nthe right hand side of my debugger.", "start": 799.48, "duration": 3.0 }, { "text": "Notice that x has a value\nof 1 and y has a value of 2.", "start": 802.48, "duration": 4.1 }, { "text": "And that's what I intend.", "start": 806.58, "duration": 1.15 }, { "text": "Now, let's begin to\nstep over these lines.", "start": 807.73, "duration": 2.34 }, { "text": "So let's step over the printf.", "start": 810.07, "duration": 1.77 }, { "text": "And we see on my terminal\nwindow that x is 1.", "start": 811.84, "duration": 2.41 }, { "text": "Step over the next printf\nand we see that y is 2.", "start": 814.25, "duration": 2.99 }, { "text": "Now, let's step over that and we\nsee indeed swapping dot dot dot.", "start": 817.24, "duration": 3.62 }, { "text": "But rather than step over my call to\nthe swap function, let's step into it.", "start": 820.86, "duration": 4.31 }, { "text": "So I can actually see\nmaybe my logic is buggy.", "start": 825.17, "duration": 2.67 }, { "text": "So let's go ahead and step into swap.", "start": 827.84, "duration": 2.37 }, { "text": "And notice that debug 50 immediately\njumps down to the first line and swap.", "start": 830.21, "duration": 4.42 }, { "text": "And notice now on the right\nhand side, what's going on?", "start": 834.63, "duration": 3.17 }, { "text": "a is 1 and b is 2.", "start": 837.8, "duration": 2.21 }, { "text": "And that's as expected,\nbecause I passed an x and y.", "start": 840.01, "duration": 3.22 }, { "text": "Swap calls it's two inputs a and\nb respectively, so a is 1, b is 2.", "start": 843.23, "duration": 4.8 }, { "text": "Temp though, very curiously is 32,767.", "start": 848.03, "duration": 6.629 }, { "text": "And where is that value coming?", "start": 854.659, "duration": 1.291 }, { "text": "Well, it turns out when\nyou declare a variable,", "start": 855.95, "duration": 3.38 }, { "text": "but haven't yet said it's of some\nvalue it's going to by definition have", "start": 859.33, "duration": 3.58 }, { "text": "an undefined value.", "start": 862.91, "duration": 1.18 }, { "text": "A so-called garbage value.", "start": 864.09, "duration": 1.51 }, { "text": "Remnants of other\naspects of your program", "start": 865.6, "duration": 2.77 }, { "text": "having used that chunk of memory before.", "start": 868.37, "duration": 2.17 }, { "text": "And we'll see why this is in just a bit.", "start": 870.54, "duration": 1.77 }, { "text": "So this is anomalous.", "start": 872.31, "duration": 1.25 }, { "text": "It's not a bug in the program.", "start": 873.56, "duration": 1.25 }, { "text": "It's to be expected, because\nI haven't yet assigned", "start": 874.81, "duration": 2.22 }, { "text": "temp a value until-- let me zoom out.", "start": 877.03, "duration": 3.11 }, { "text": "Until I step over line 21 and\nactually let temp equal a.", "start": 880.14, "duration": 6.58 }, { "text": "And notice, if you watch the\nright hand side of my screen now,", "start": 886.72, "duration": 2.75 }, { "text": "temp is currently 32,767.", "start": 889.47, "duration": 4.03 }, { "text": "And when I step over it, OK.", "start": 893.5, "duration": 2.51 }, { "text": "It becomes 1.", "start": 896.01, "duration": 1.04 }, { "text": "So that's as expected.", "start": 897.05, "duration": 1.29 }, { "text": "Now, a is about to become b.", "start": 898.34, "duration": 2.61 }, { "text": "And indeed, we now see that a\nis 2 b is 2 and temp is still 1.", "start": 900.95, "duration": 4.2 }, { "text": "So now we have to put temp into\nb, so let's step over that line.", "start": 905.15, "duration": 3.4 }, { "text": "And indeed, we've done\na switcheroo of a and b.", "start": 908.55, "duration": 3.57 }, { "text": "a is 2, b is 1.", "start": 912.12, "duration": 2.18 }, { "text": "OK.", "start": 914.3, "duration": 0.5 }, { "text": "Let's go ahead and play this out.", "start": 914.8, "duration": 2.55 }, { "text": "Let's continue here.", "start": 917.35, "duration": 1.06 }, { "text": "I claim that I've swapped.", "start": 918.41, "duration": 1.52 }, { "text": "I'm printing x-- and somehow\nx and y are still 1 and 2.", "start": 919.93, "duration": 6.68 }, { "text": "Even though the debunker\ntold me it would", "start": 926.61, "duration": 1.97 }, { "text": "seem that my swap function was correct.", "start": 928.58, "duration": 2.33 }, { "text": "So what is happening after all of these\nweeks of CS50 and all of these problems", "start": 930.91, "duration": 4.24 }, { "text": "solved thus far?", "start": 935.15, "duration": 0.792 }, { "text": "It would seem that all of\nour understanding of things", "start": 935.942, "duration": 2.208 }, { "text": "is kind of unraveling.", "start": 938.15, "duration": 1.059 }, { "text": "And that's because we've been very\ncareful over the past few weeks,", "start": 939.209, "duration": 2.791 }, { "text": "at least in class.", "start": 942.0, "duration": 0.93 }, { "text": "And in sections to try to avoid\ntripping over some of these lower-level", "start": 942.93, "duration": 3.41 }, { "text": "implementation details.", "start": 946.34, "duration": 1.26 }, { "text": "And there's not that many of them.", "start": 947.6, "duration": 1.56 }, { "text": "But today, is now a time\nto peel back this layer", "start": 949.16, "duration": 4.12 }, { "text": "and understand exactly\nwhat it is that's going on.", "start": 953.28, "duration": 2.83 }, { "text": "Indeed, all of this time when you\nrun a program on your computer,", "start": 956.11, "duration": 4.06 }, { "text": "double-click an icon or run the\nprogram's name with dot slash", "start": 960.17, "duration": 2.89 }, { "text": "something or other at your\nterminal window-- what happens", "start": 963.06, "duration": 2.56 }, { "text": "is that your computer gives that program\nthe illusion of a really big chunk", "start": 965.62, "duration": 4.93 }, { "text": "of memory all of its own.", "start": 970.55, "duration": 1.34 }, { "text": "Maybe two gigabytes of memory,\neven though it doesn't necessarily", "start": 971.89, "duration": 3.05 }, { "text": "use all of that memory.", "start": 974.94, "duration": 1.337 }, { "text": "And that memory-- if you just\nthink of your computer's memory,", "start": 976.277, "duration": 2.583 }, { "text": "as we've done before, is\nlike a rectangular region.", "start": 978.86, "duration": 2.125 }, { "text": "And we could number of\nthe bytes in my memory.", "start": 980.985, "duration": 1.922 }, { "text": "But it doesn't really matter what the\naddresses are, what the numbers are,", "start": 982.907, "duration": 3.083 }, { "text": "just that it exists.", "start": 985.99, "duration": 1.78 }, { "text": "It turns out that when\nyou run a program,", "start": 987.77, "duration": 3.19 }, { "text": "your operating system typically lays\nout your program's memory in a fairly", "start": 990.96, "duration": 3.79 }, { "text": "standard way.", "start": 994.75, "duration": 1.65 }, { "text": "There's a chunk of memory down here for\nsomething called environment variables.", "start": 996.4, "duration": 3.52 }, { "text": "There's a bigger growable chunk\nof memory down here called", "start": 999.92, "duration": 3.66 }, { "text": "the so-called stack.", "start": 1003.58, "duration": 1.58 }, { "text": "On the opposite side of the\npicture is a so-called heap.", "start": 1005.16, "duration": 2.379 }, { "text": "Another chunk of memory that actually\ngrows in the other direction.", "start": 1007.539, "duration": 2.791 }, { "text": "So long story short, bad things\ncan happen if both of those", "start": 1010.33, "duration": 3.04 }, { "text": "grow bigger than you intend.", "start": 1013.37, "duration": 1.78 }, { "text": "Then, there's some kind of uninitialized\nand initialized data up top.", "start": 1015.15, "duration": 3.53 }, { "text": "And then, text.", "start": 1018.68, "duration": 1.39 }, { "text": "Now, it turns out text is\nthe segment of memory where", "start": 1020.07, "duration": 3.2 }, { "text": "your program's zeros and ones live.", "start": 1023.27, "duration": 2.299 }, { "text": "So when you double-click\nan icon on your Mac or PC", "start": 1025.569, "duration": 2.111 }, { "text": "or when you run the command\ndot slash something,", "start": 1027.68, "duration": 2.07 }, { "text": "those zeros and ones are loaded from\nyour hard disk or solid state disk", "start": 1029.75, "duration": 3.54 }, { "text": "into RAM or memory.", "start": 1033.29, "duration": 1.64 }, { "text": "And it's put conceptually\nat the top of the memory", "start": 1034.93, "duration": 3.38 }, { "text": "that your computer program is using.", "start": 1038.31, "duration": 1.64 }, { "text": "And below that is the actual\ndata that your program is using.", "start": 1039.95, "duration": 4.149 }, { "text": "The variables and the\nvalues inside of it.", "start": 1044.099, "duration": 2.451 }, { "text": "Now, each of these types of\nmemory have different purposes.", "start": 1046.55, "duration": 3.06 }, { "text": "And we'll see in just a moment\nwhat it is that's going on.", "start": 1049.61, "duration": 3.49 }, { "text": "And we'll ultimately\npeel back these layers.", "start": 1053.1, "duration": 2.31 }, { "text": "So what is it that's actually\ngoing on underneath the hood here?", "start": 1055.41, "duration": 3.53 }, { "text": "Well, let's consider this\nto be my computer's memory.", "start": 1058.94, "duration": 2.77 }, { "text": "So focusing on just that\nbottom most portion,", "start": 1061.71, "duration": 2.99 }, { "text": "which I called the stack a moment ago.", "start": 1064.7, "duration": 2.08 }, { "text": "So if we draw just the bottom of my\ncomputer's memory kind of like this,", "start": 1066.78, "duration": 3.645 }, { "text": "the bottom of it has technically\nenvironment variables.", "start": 1070.425, "duration": 2.574 }, { "text": "But let's focus on the\nregion known as the stack.", "start": 1072.999, "duration": 2.041 }, { "text": "And the stack, as the name implies,\nis kind of like the stack of trays", "start": 1075.04, "duration": 3.68 }, { "text": "that you might see in a\ncafeteria or a dining hall,", "start": 1078.72, "duration": 2.18 }, { "text": "where you put trays on top of another\nuntil it can get potentially pretty", "start": 1080.9, "duration": 3.89 }, { "text": "tall.", "start": 1084.79, "duration": 0.62 }, { "text": "And it turns out when\nyou run a program, not", "start": 1085.41, "duration": 2.74 }, { "text": "only is your program given the illusion\nof this really big memory space laid", "start": 1088.15, "duration": 3.51 }, { "text": "out as proposed, but\nit also by convention", "start": 1091.66, "duration": 3.23 }, { "text": "uses this memory in a\nfairly standard way.", "start": 1094.89, "duration": 2.94 }, { "text": "Specifically, when main is called,\nmain is given a chunk of memory", "start": 1097.83, "duration": 5.47 }, { "text": "at the bottom, so to\nspeak, of this stack space.", "start": 1103.3, "duration": 2.48 }, { "text": "And so, let me go ahead\nhere and write main.", "start": 1105.78, "duration": 2.67 }, { "text": "And any local variables that main\nhas and any arguments to main,", "start": 1108.45, "duration": 5.0 }, { "text": "namely argc an argv, end up inside here.", "start": 1113.45, "duration": 3.5 }, { "text": "So if indeed you are using\nsomething like argv and argc,", "start": 1116.95, "duration": 4.68 }, { "text": "you might have a value\nlike this down here.", "start": 1121.63, "duration": 2.83 }, { "text": "And you might have another chunk\nof memory carved out here for argv.", "start": 1124.46, "duration": 4.46 }, { "text": "And if you have a couple of\nlocal variables, for instance x", "start": 1128.92, "duration": 4.03 }, { "text": "and another one, y, those two would\nbe allocated space in this slice", "start": 1132.95, "duration": 5.48 }, { "text": "if you will.", "start": 1138.43, "duration": 0.53 }, { "text": "This frame of memory.", "start": 1138.96, "duration": 2.21 }, { "text": "Meanwhile, if main calls a\nfunction, like swap-- swap", "start": 1141.17, "duration": 4.72 }, { "text": "is allocated a swath of memory, a\nframe of memory, above main by design.", "start": 1145.89, "duration": 5.45 }, { "text": "So if I've called swap,\nits memory ends up here.", "start": 1151.34, "duration": 3.56 }, { "text": "And if the swap function\nitself has arguments,", "start": 1154.9, "duration": 3.26 }, { "text": "like a and b or any\nother local variables,", "start": 1158.16, "duration": 2.94 }, { "text": "those values too are put inside\nof the so-called stack frame.", "start": 1161.1, "duration": 4.1 }, { "text": "So this might be a and this might be b.", "start": 1165.2, "duration": 3.659 }, { "text": "In other words, the concepts\nthat we've been taking for", "start": 1168.859, "duration": 2.291 }, { "text": "granted, both in Scratch and in C, at\nthe end of the day, boil down to values", "start": 1171.15, "duration": 4.48 }, { "text": "needing to go somewhere physically.", "start": 1175.63, "duration": 2.1 }, { "text": "And so, if you assume that\nthe big rectangular region", "start": 1177.73, "duration": 2.31 }, { "text": "here is your computer's memory.", "start": 1180.04, "duration": 1.44 }, { "text": "And then, you consider that\nthe operating system really", "start": 1181.48, "duration": 2.49 }, { "text": "just slices and dices this memory,\nsuch that mains memory is down here.", "start": 1183.97, "duration": 5.16 }, { "text": "Any function that main calls\nis immediately above it.", "start": 1189.13, "duration": 2.6 }, { "text": "And frankly, if swap called its own\nfunction, it would end up above it.", "start": 1191.73, "duration": 4.54 }, { "text": "But now, given this basic\ndefinition of memory management", "start": 1196.27, "duration": 4.16 }, { "text": "and the layout of computer\nprogram's memory space,", "start": 1200.43, "duration": 2.87 }, { "text": "you can perhaps start to infer\nwhy all of these failures", "start": 1203.3, "duration": 4.23 }, { "text": "have started to happen in my program.", "start": 1207.53, "duration": 2.08 }, { "text": "A moment ago, I didn't\nhave argv and argc.", "start": 1209.61, "duration": 2.88 }, { "text": "I just had for instance x and y.", "start": 1212.49, "duration": 2.04 }, { "text": "And I had the value 1\nand I had the value 2.", "start": 1214.53, "duration": 4.21 }, { "text": "Main then called swap and put a copy\nof 1 there and a copy of 2 there.", "start": 1218.74, "duration": 6.53 }, { "text": "And indeed, that's the key insight.", "start": 1225.27, "duration": 2.34 }, { "text": "When a function calls another function,\npassing in arguments as inputs,", "start": 1227.61, "duration": 4.55 }, { "text": "that the function is being\npassed to copies of those inputs.", "start": 1232.16, "duration": 3.72 }, { "text": "So at this point in time, if you\nopened up the lid of your computer", "start": 1235.88, "duration": 3.01 }, { "text": "and looked inside digitally,\nyou would see 1 and 2 down here.", "start": 1238.89, "duration": 3.302 }, { "text": "And you would see\nanother pattern of bits", "start": 1242.192, "duration": 1.708 }, { "text": "representing 1 and 2\nup here in duplicate.", "start": 1243.9, "duration": 2.87 }, { "text": "So now when my swap\nfunction operates, it", "start": 1246.77, "duration": 2.98 }, { "text": "declares a temporary\nvariable recall, called temp.", "start": 1249.75, "duration": 2.69 }, { "text": "So let me draw that here.", "start": 1252.44, "duration": 2.62 }, { "text": "And as I recall, it stores\nin temp, which value?", "start": 1255.06, "duration": 3.97 }, { "text": "The value of a.", "start": 1259.03, "duration": 1.52 }, { "text": "The value of a is 1.", "start": 1260.55, "duration": 2.11 }, { "text": "It then took the value of b, put it\nin a-- which puts that value here.", "start": 1262.66, "duration": 7.47 }, { "text": "And now at this point in the\nstory, a and b are incorrect.", "start": 1270.13, "duration": 3.15 }, { "text": "We still need to put the value 1 in b.", "start": 1273.28, "duration": 2.45 }, { "text": "And that's why we then took\ntemps value, put it in b.", "start": 1275.73, "duration": 3.93 }, { "text": "Thereby giving the me ultimately\nthe number 1 in b's slot as well.", "start": 1279.66, "duration": 7.08 }, { "text": "And so, at this point in the\nstory, temp still exists.", "start": 1286.74, "duration": 3.1 }, { "text": "And a and b have the correct\nanswers, 2 and 1 respectively.", "start": 1289.84, "duration": 3.79 }, { "text": "But the catch is the moment\nthat swap returns, this happens.", "start": 1293.63, "duration": 6.87 }, { "text": "Essentially, everything that was\nbeing used above main disappears.", "start": 1300.5, "duration": 4.36 }, { "text": "It's not actually deleted.", "start": 1304.86, "duration": 1.099 }, { "text": "All of those bits are still\nthere, so technically the numbers", "start": 1305.959, "duration": 2.541 }, { "text": "are still there.", "start": 1308.5, "duration": 0.67 }, { "text": "But the computer just\nforgets about those values.", "start": 1309.17, "duration": 2.2 }, { "text": "And indeed, the key\ntakeaway here is that when", "start": 1311.37, "duration": 3.34 }, { "text": "execution returns to main-- that\nis when swap says, return I'm done.", "start": 1314.71, "duration": 3.96 }, { "text": "And main takes over operation again,\n1 and 2 are completely unchanged.", "start": 1318.67, "duration": 5.017 }, { "text": "So all of the word that\nwe just did up there", "start": 1323.687, "duration": 1.833 }, { "text": "was a complete waste\nof time in some sense.", "start": 1325.52, "duration": 2.0 }, { "text": "Because even though it was correct\nin so far as it swapped a and b,", "start": 1327.52, "duration": 3.23 }, { "text": "it did not actually have a\npermanent impact on the variables", "start": 1330.75, "duration": 3.78 }, { "text": "that I actually cared about.", "start": 1334.53, "duration": 1.67 }, { "text": "Let's consider another example now.", "start": 1336.2, "duration": 2.06 }, { "text": "Recall my previous example where when I\nwhipped up some code to compare values,", "start": 1338.26, "duration": 4.79 }, { "text": "I called get string once and\nstored its return value in s.", "start": 1343.05, "duration": 3.09 }, { "text": "I called get string twice and\nstored its return value in t.", "start": 1346.14, "duration": 3.66 }, { "text": "And then, I just tried\nto compare s and t.", "start": 1349.8, "duration": 2.92 }, { "text": "Well, what does that actually mean?", "start": 1352.72, "duration": 2.11 }, { "text": "It turns out that when\nyou call get string,", "start": 1354.83, "duration": 3.11 }, { "text": "it's not technically\nreturning a string to per say.", "start": 1357.94, "duration": 3.3 }, { "text": "It's not technically returning\nto you a sequence of characters.", "start": 1361.24, "duration": 3.81 }, { "text": "It's actually returning\nsomething much simpler.", "start": 1365.05, "duration": 3.36 }, { "text": "When I call get string and do this--\nstring s gets get underscore string,", "start": 1368.41, "duration": 11.76 }, { "text": "all that's happening is this.", "start": 1380.17, "duration": 2.69 }, { "text": "The left hand side of this expression\nis telling the computer, hey, computer.", "start": 1382.86, "duration": 3.42 }, { "text": "I need a variable called s that's big\nenough to fit a string so to speak.", "start": 1386.28, "duration": 5.51 }, { "text": "On the right hand side, get string\nindeed gets a string from the user,", "start": 1391.79, "duration": 3.09 }, { "text": "like D-A-V-I-D or Z-A-M-Y-L-A.", "start": 1394.88, "duration": 3.5 }, { "text": "But where does that string come from?", "start": 1398.38, "duration": 1.8 }, { "text": "It turns out it comes from\nsomewhere else in memory.", "start": 1400.18, "duration": 2.79 }, { "text": "And suppose that the user has indeed\ntyped in Z-A-M-Y-L-A quote unquote", "start": 1402.97, "duration": 6.12 }, { "text": "so to speak.", "start": 1409.09, "duration": 0.67 }, { "text": "That's just kind of floating\nsomewhere in my computer's memory.", "start": 1409.76, "duration": 2.88 }, { "text": "And if this string happens to\nexist at address number 123,", "start": 1412.64, "duration": 5.66 }, { "text": "byte number 123 in my computer's memory.", "start": 1418.3, "duration": 3.3 }, { "text": "What's actually going to get\nreturned is the value 123.", "start": 1421.6, "duration": 6.38 }, { "text": "In other words, a string is\ntechnically just the address", "start": 1427.98, "duration": 4.46 }, { "text": "of a character in memory.", "start": 1432.44, "duration": 1.92 }, { "text": "In fact, if we zoom\nin further on Zamyla,", "start": 1434.36, "duration": 2.32 }, { "text": "recall that Zamyla's name really\nlooks like this underneath the hood.", "start": 1436.68, "duration": 4.88 }, { "text": "It's an array, a block of\ncontiguous memory with a backslash 0", "start": 1441.56, "duration": 4.68 }, { "text": "at the very end.", "start": 1446.24, "duration": 1.11 }, { "text": "And if this first byte just\nhappens to be the number 123,", "start": 1447.35, "duration": 4.05 }, { "text": "the second byte is going to be 124.", "start": 1451.4, "duration": 2.28 }, { "text": "The third byte it's going to be 125.", "start": 1453.68, "duration": 2.39 }, { "text": "And so forth.", "start": 1456.07, "duration": 0.647 }, { "text": "In other words, if my\ncomputer has a billion bytes", "start": 1456.717, "duration": 2.083 }, { "text": "of memory, 2 billion bytes of memory--\nlike a gigabyte or two gigabytes,", "start": 1458.8, "duration": 3.949 }, { "text": "you can certainly number each\nof those bytes and the computer", "start": 1462.749, "duration": 2.541 }, { "text": "does underneath the hood.", "start": 1465.29, "duration": 1.38 }, { "text": "And so Z-A-M-Y-L-A backslash 0 is simply\na sequence of 7 characters that live", "start": 1466.67, "duration": 8.67 }, { "text": "somewhere physically in memory.", "start": 1475.34, "duration": 2.08 }, { "text": "And what a string technically is,\nis just kind of a breadcrumb--", "start": 1477.42, "duration": 5.97 }, { "text": "specifically the address of, the number\nof the first byte of Zamyla's name.", "start": 1483.39, "duration": 5.57 }, { "text": "So when you return a string, you're not\nhanding back Z-A-M-Y-L-A backslash 0,", "start": 1488.96, "duration": 5.349 }, { "text": "you're handing back\nsomething much smaller.", "start": 1494.309, "duration": 1.791 }, { "text": "A little scrap of paper, if you\nwill, that's a map to Zamyla's name", "start": 1496.1, "duration": 4.31 }, { "text": "somewhere in your computer's memory.", "start": 1500.41, "duration": 3.01 }, { "text": "And so over here, this number 123 is\ngenerally called, not just a number,", "start": 1503.42, "duration": 5.09 }, { "text": "but an address.", "start": 1508.51, "duration": 1.4 }, { "text": "Much like post boxes and houses have\naddresses physically that uniquely", "start": 1509.91, "duration": 4.92 }, { "text": "identify them, 123 uniquely\nidentifies that byte in memory.", "start": 1514.83, "duration": 3.66 }, { "text": "And so via this address,\nthis map if you will,", "start": 1518.49, "duration": 2.905 }, { "text": "can my program later on find Zamyla's\nname and do something with it.", "start": 1521.395, "duration": 5.095 }, { "text": "Like printed out, or capitalize it,\nor compare it, or anything else.", "start": 1526.49, "duration": 4.23 }, { "text": "And so, how though\ndoes the computer know", "start": 1530.72, "duration": 3.14 }, { "text": "where Zamyla's name begins and ends?", "start": 1533.86, "duration": 2.43 }, { "text": "Well, it begins at 123.", "start": 1536.29, "duration": 2.79 }, { "text": "And again, recall from two weeks\nago, it ends with backslash 0.", "start": 1539.08, "duration": 5.07 }, { "text": "So with those two markers,\nwhere does it begin?", "start": 1544.15, "duration": 2.51 }, { "text": "Where does it end?", "start": 1546.66, "duration": 0.82 }, { "text": "Can you infer the\nentirety of someone's name", "start": 1547.48, "duration": 2.67 }, { "text": "or the entirety of a string in between?", "start": 1550.15, "duration": 1.83 }, { "text": "Now, consider my second line of code.", "start": 1551.98, "duration": 2.11 }, { "text": "String t gets get string.", "start": 1554.09, "duration": 3.98 }, { "text": "So what does this mean?", "start": 1558.07, "duration": 1.8 }, { "text": "This is another function call.", "start": 1559.87, "duration": 1.85 }, { "text": "And suppose that I just happen to type\nin Z-A-M-Y-L-A enter, just like before.", "start": 1561.72, "duration": 5.54 }, { "text": "What's happening there is that the\ncomputer, blind to the fact that,", "start": 1567.26, "duration": 4.13 }, { "text": "that might already exist\nsomewhere in memory,", "start": 1571.39, "duration": 2.26 }, { "text": "is going to give me another chunk of\nmemory somewhere with Z-A-M-Y-L-A.", "start": 1573.65, "duration": 6.65 }, { "text": "And then, backslash 0.", "start": 1580.3, "duration": 2.02 }, { "text": "Whereby, this is effectively\nan array of its own.", "start": 1582.32, "duration": 2.972 }, { "text": "And I don't know where it is.", "start": 1585.292, "duration": 1.208 }, { "text": "Maybe it's at address number\n234, followed by 235 dot dot dot.", "start": 1586.5, "duration": 6.28 }, { "text": "It's somewhere else in memory.", "start": 1592.78, "duration": 1.25 }, { "text": "I don't know or need to\neven care where it is.", "start": 1594.03, "duration": 2.26 }, { "text": "But the key detail here is that t in\nso far is it is a variable itself.", "start": 1596.29, "duration": 7.51 }, { "text": "It gets what value?", "start": 1603.8, "duration": 2.64 }, { "text": "Well, if the string's Zamyla,\nthe second time I type it in it", "start": 1606.44, "duration": 2.67 }, { "text": "just happens to end up at location 234.", "start": 1609.11, "duration": 2.88 }, { "text": "What ends up in t is\nsimply the number 234.", "start": 1611.99, "duration": 5.44 }, { "text": "So if we change back to my code where\nI was comparing s and t, as I am here.", "start": 1617.43, "duration": 6.901 }, { "text": "I've called get string\nonce, stored the value in s.", "start": 1624.331, "duration": 2.124 }, { "text": "Called get string twice\nstored the value in t.", "start": 1626.455, "duration": 1.916 }, { "text": "And then, I'm quite simply saying,\nif s equals equals t, then say same.", "start": 1628.371, "duration": 4.524 }, { "text": "Else, say different.", "start": 1632.895, "duration": 2.315 }, { "text": "Now, when you consider what is really\ngoing on underneath the hood all", "start": 1635.21, "duration": 4.91 }, { "text": "these weeks, certainly 123\nis not the same as 234.", "start": 1640.12, "duration": 5.39 }, { "text": "And so, s does not equal t.", "start": 1645.51, "duration": 4.9 }, { "text": "Capitalization meanwhile.", "start": 1650.41, "duration": 1.419 }, { "text": "What happens with that other program?", "start": 1651.829, "duration": 1.541 }, { "text": "Well, we'll do this one more quickly.", "start": 1653.37, "duration": 2.07 }, { "text": "But if I for instance,\ncall string s gets get", "start": 1655.44, "duration": 6.88 }, { "text": "underscore string open\nparen close paren.", "start": 1662.32, "duration": 4.07 }, { "text": "That again, is going to\ngive me something like this.", "start": 1666.39, "duration": 2.26 }, { "text": "And it's going to give me something\nlike Z-A-M-Y-L-A backslash 0,", "start": 1668.65, "duration": 7.31 }, { "text": "all of which I can think of, as before,\nlike an array-- if sloppily drawn.", "start": 1675.96, "duration": 5.65 }, { "text": "Maybe starting again at address 123.", "start": 1681.61, "duration": 2.36 }, { "text": "And so that's what ends up in s.", "start": 1683.97, "duration": 2.085 }, { "text": "But in my copy 0 program,\nrecall that I did this.", "start": 1686.055, "duration": 3.315 }, { "text": "String t gets s.", "start": 1689.37, "duration": 4.61 }, { "text": "I didn't call get string again.", "start": 1693.98, "duration": 1.65 }, { "text": "I just said, store s inside of t.", "start": 1695.63, "duration": 3.08 }, { "text": "Effectively, I thought,\nmaking a copy of it.", "start": 1698.71, "duration": 2.99 }, { "text": "What this left-hand side gives\nme is another box called t.", "start": 1701.7, "duration": 3.11 }, { "text": "If this is s, this is t.", "start": 1704.81, "duration": 2.74 }, { "text": "But what goes inside of this new box t?", "start": 1707.55, "duration": 3.32 }, { "text": "Well, what goes inside\nis literally a copy of s.", "start": 1710.87, "duration": 2.34 }, { "text": "What is s? s is 123.", "start": 1713.21, "duration": 1.94 }, { "text": "OK. t then, is 123, which\nmeans later in my copy program,", "start": 1715.15, "duration": 6.55 }, { "text": "when I simply decided to\ncapitalize t bracket 0,", "start": 1721.7, "duration": 6.08 }, { "text": "the first character in\nmy string t-- that's", "start": 1727.78, "duration": 3.65 }, { "text": "kind of misleading, because my\nstring t is really just my string s.", "start": 1731.43, "duration": 4.65 }, { "text": "They're sort of synonyms for\none another at this point,", "start": 1736.08, "duration": 2.36 }, { "text": "because one indeed equals the other.", "start": 1738.44, "duration": 2.02 }, { "text": "And so what has happened is that I\nhave gone to the first character in t,", "start": 1740.46, "duration": 5.9 }, { "text": "which of course is z.", "start": 1746.36, "duration": 1.37 }, { "text": "And recall from my example earlier when\nI typed it in all lowercase, David,", "start": 1747.73, "duration": 3.52 }, { "text": "for instance, with a lowercase d, it\ncapitalized it not only in t, but also", "start": 1751.25, "duration": 5.31 }, { "text": "in s.", "start": 1756.56, "duration": 0.786 }, { "text": "In other words-- and frankly, it\ndoesn't really matter, typically,", "start": 1757.346, "duration": 4.184 }, { "text": "what these addresses are.", "start": 1761.53, "duration": 1.24 }, { "text": "I'm just using 123 and 234 because\nthey're sort of easy to say.", "start": 1762.77, "duration": 3.98 }, { "text": "But you can really think of\ns as again, being a map that", "start": 1766.75, "duration": 3.29 }, { "text": "leads you to the string you care about.", "start": 1770.04, "duration": 2.62 }, { "text": "A pointer, if you will.", "start": 1772.66, "duration": 1.15 }, { "text": "Literally an arrow.", "start": 1773.81, "duration": 0.96 }, { "text": "And t, similarly, can be\nthought of as a pointer.", "start": 1774.77, "duration": 3.17 }, { "text": "And the key detail here is that\nbecause I've set s equal to t-- or t", "start": 1777.94, "duration": 6.5 }, { "text": "equal to s-- they are effectively\npointing at the same thing.", "start": 1784.44, "duration": 5.88 }, { "text": "So strings are a lie.", "start": 1790.32, "duration": 1.33 }, { "text": "There is no such thing\nas a string data type.", "start": 1791.65, "duration": 2.09 }, { "text": "There are things called\nchars, characters", "start": 1793.74, "duration": 2.4 }, { "text": "that can live somewhere in memory.", "start": 1796.14, "duration": 1.45 }, { "text": "And we humans can\narbitrarily decide that hey,", "start": 1797.59, "duration": 2.66 }, { "text": "if we put a backslash 0 character at the\nend of a sequence of other characters,", "start": 1800.25, "duration": 4.74 }, { "text": "we can all just agree universally\nto treat that as the end of a quote", "start": 1804.99, "duration": 4.03 }, { "text": "unquote \"string,\" that\nis a word or a phrase", "start": 1809.02, "duration": 2.27 }, { "text": "or paragraph or anything even bigger.", "start": 1811.29, "duration": 2.3 }, { "text": "But we need a convention for\nremembering where strings begin.", "start": 1813.59, "duration": 3.696 }, { "text": "We've already solved the\nwhere do they end problem.", "start": 1817.286, "duration": 2.124 }, { "text": "So where does the string begin?", "start": 1819.41, "duration": 1.32 }, { "text": "It begins at an address.", "start": 1820.73, "duration": 1.83 }, { "text": "It begins with a pointer.", "start": 1822.56, "duration": 2.21 }, { "text": "And so this special data type\nthat we declare in CS50's library", "start": 1824.77, "duration": 2.94 }, { "text": "called string really is just\nin CS50 IDE an 8-byte value,", "start": 1827.71, "duration": 5.23 }, { "text": "a 64-bit value that is just a\nreally big number that represents", "start": 1832.94, "duration": 5.51 }, { "text": "the address in memory of a string.", "start": 1838.45, "duration": 1.74 }, { "text": "And I say really big just\nbecause the IDE gives you access", "start": 1840.19, "duration": 2.69 }, { "text": "to lots of memory, certainly\nnumbers bigger than 123.", "start": 1842.88, "duration": 3.14 }, { "text": "But a string is just a number, is\njust an address, AKA a pointer.", "start": 1846.02, "duration": 6.43 }, { "text": "And that explains, then, why\nall three of these examples", "start": 1852.45, "duration": 3.07 }, { "text": "did not behave as I might\nhave hoped, because rather", "start": 1855.52, "duration": 3.38 }, { "text": "they were taking things\na little too literally.", "start": 1858.9, "duration": 3.89 }, { "text": "Or I was failing to appreciate\nwhat's actually going on.", "start": 1862.79, "duration": 3.62 }, { "text": "Let's pause for a moment,\ntake things down a notch.", "start": 1866.41, "duration": 2.49 }, { "text": "Make things a little more\nreal with a bit of claymation", "start": 1868.9, "duration": 2.49 }, { "text": "that will motivate, eventually,\npeeling back this layer further", "start": 1871.39, "duration": 3.07 }, { "text": "and seeing what's really going on.", "start": 1874.46, "duration": 3.21 }, { "text": "[STRUMMED CHORD]", "start": 1877.67, "duration": 1.4 }, { "text": "NICK PARLANTE: Hey, Binky.", "start": 1879.437, "duration": 1.083 }, { "text": "Wake up.", "start": 1880.52, "duration": 0.74 }, { "text": "It's time for pointer fun.", "start": 1881.26, "duration": 2.51 }, { "text": "BINKY: What's that?", "start": 1883.77, "duration": 1.21 }, { "text": "Learn about pointers?", "start": 1884.98, "duration": 1.748 }, { "text": "Oh, goodie!", "start": 1886.728, "duration": 2.789 }, { "text": "DAVID J. MALAN: Binky, who exist\nhere only in claymation form,", "start": 1889.517, "duration": 2.583 }, { "text": "is the product of a good friend of\nours, Nick Parlante at Stanford,", "start": 1892.1, "duration": 3.372 }, { "text": "who teaches computer science there.", "start": 1895.472, "duration": 1.458 }, { "text": "You'll see more of Binky and hear\nmore of Nick in just a moment.", "start": 1896.93, "duration": 3.62 }, { "text": "But these here are\nsort of metaphorically", "start": 1900.55, "duration": 2.39 }, { "text": "the training wheels that we've\nhad on for the past few weeks.", "start": 1902.94, "duration": 2.56 }, { "text": "And the goal now at hand\nis to take these off,", "start": 1905.5, "duration": 2.37 }, { "text": "and to finally start looking at what's\nreally going on underneath the hood.", "start": 1907.87, "duration": 4.8 }, { "text": "And starting to remove,\nif you will, let's", "start": 1912.67, "duration": 2.426 }, { "text": "see if-- [BANG] --probably\nnot the best idea.", "start": 1915.096, "duration": 4.564 }, { "text": "Remove, if you will,\nthese training wheels,", "start": 1919.66, "duration": 2.02 }, { "text": "and actually see what's\ngoing on, and understand", "start": 1921.68, "duration": 2.87 }, { "text": "and take advantage of the same.", "start": 1924.55, "duration": 1.57 }, { "text": "As follows.", "start": 1926.12, "duration": 0.78 }, { "text": "Let's go ahead now into CS50\nIDE, and go ahead and open up,", "start": 1926.9, "duration": 4.18 }, { "text": "let's say, compare1.c, which I\nwrote in advance to look as follows.", "start": 1931.08, "duration": 6.41 }, { "text": "And you'll notice that it works a\nlittle differently from version zero.", "start": 1937.49, "duration": 3.04 }, { "text": "Here we have a prompt for string s.", "start": 1940.53, "duration": 2.26 }, { "text": "And we store in it the\nreturn value of get string.", "start": 1942.79, "duration": 3.8 }, { "text": "But notice what's on the left-hand side.", "start": 1946.59, "duration": 3.64 }, { "text": "Char star s, all of a sudden.", "start": 1950.23, "duration": 2.58 }, { "text": "Indeed, all of this time,\nI've been treating things", "start": 1952.81, "duration": 2.18 }, { "text": "as though they are strings, literally.", "start": 1954.99, "duration": 3.54 }, { "text": "But it turns out a\nstring is just a synonym", "start": 1958.53, "duration": 5.11 }, { "text": "for a data type known as a char star.", "start": 1963.64, "duration": 2.54 }, { "text": "And the new syntax today,\nthen, is this star operator.", "start": 1966.18, "duration": 2.51 }, { "text": "The asterisk that actually has special\nmeaning in certain contexts, not just", "start": 1968.69, "duration": 3.78 }, { "text": "multiplication.", "start": 1972.47, "duration": 0.94 }, { "text": "But in this case, it specifies\nthat the type of a variable", "start": 1973.41, "duration": 3.74 }, { "text": "is not a char literally,\nbut a char star, the address", "start": 1977.15, "duration": 3.79 }, { "text": "of a char, a pointer to a char.", "start": 1980.94, "duration": 3.3 }, { "text": "Now, why char?", "start": 1984.24, "duration": 0.7 }, { "text": "I thought we were talking about strings.", "start": 1984.94, "duration": 1.666 }, { "text": "But again, recall that a string\nis just a sequence of characters", "start": 1986.606, "duration": 3.174 }, { "text": "back to back to back, and\ntherefore, you can define a string", "start": 1989.78, "duration": 4.51 }, { "text": "by the address of its first character.", "start": 1994.29, "duration": 2.8 }, { "text": "Ergo, what we really\nneed underneath the hood", "start": 1997.09, "duration": 2.31 }, { "text": "is a data type that lets us\nstore the address of a character.", "start": 1999.4, "duration": 5.29 }, { "text": "There is no string.", "start": 2004.69, "duration": 1.68 }, { "text": "And so what does this allow us to do?", "start": 2006.37, "duration": 1.92 }, { "text": "Let me go into CS50 IDE, and let\nme declare then, on this line", "start": 2008.29, "duration": 4.01 }, { "text": "here, that s, this time, will\nnot be a \"string\" quote unquote.", "start": 2012.3, "duration": 4.98 }, { "text": "That was from the CS50 library.", "start": 2017.28, "duration": 1.66 }, { "text": "But rather it's going to be a char star.", "start": 2018.94, "duration": 3.022 }, { "text": "It turns out that all this time\nget string, again, does not", "start": 2021.962, "duration": 2.458 }, { "text": "return a string, it returns\nthe address of a string, AKA", "start": 2024.42, "duration": 4.33 }, { "text": "the address of the first\ncharacter in a string.", "start": 2028.75, "duration": 3.65 }, { "text": "And so the type of value it's\nreturning is not just a number.", "start": 2032.4, "duration": 3.83 }, { "text": "It's not just an int.", "start": 2036.23, "duration": 0.96 }, { "text": "It's a special type of an int.", "start": 2037.19, "duration": 2.09 }, { "text": "It's used for a different purpose.", "start": 2039.28, "duration": 1.51 }, { "text": "It's simply an integer that\nrepresents the address of a char.", "start": 2040.79, "duration": 3.87 }, { "text": "And the way you type out address\nof a char is literally char star.", "start": 2044.66, "duration": 4.74 }, { "text": "So this, then, is identical to my\npreviously in weeks past having typed", "start": 2049.4, "duration": 5.27 }, { "text": "string s.", "start": 2054.67, "duration": 1.08 }, { "text": "Now I'm going to start\ntyping it as char star.", "start": 2055.75, "duration": 2.76 }, { "text": "Meanwhile, t is going to be the same.", "start": 2058.51, "duration": 1.709 }, { "text": "So when I prompt the user\nfor another string there,", "start": 2060.219, "duration": 2.125 }, { "text": "I'm going to store that\nreturn value inside of t.", "start": 2062.344, "duration": 3.006 }, { "text": "And now, notice, just for\ngood measure, I'm making sure", "start": 2065.35, "duration": 2.77 }, { "text": "that both s and t are not null.", "start": 2068.12, "duration": 2.71 }, { "text": "I'm using a bit of conditional\nlogic there, saying if s is not null", "start": 2070.83, "duration": 3.04 }, { "text": "and t is not null, it's safe to proceed.", "start": 2073.87, "duration": 2.099 }, { "text": "Because recall that get string can\naccidentally return null sometimes", "start": 2075.969, "duration": 3.595 }, { "text": "if your computer is out of memory,\nor something else goes wrong.", "start": 2079.564, "duration": 2.666 }, { "text": "Or not so much\naccidentally, but by design.", "start": 2082.23, "duration": 3.17 }, { "text": "But notice this new chunk of code.", "start": 2085.4, "duration": 2.85 }, { "text": "It turns out-- and we\nknow now from a moment", "start": 2088.25, "duration": 2.8 }, { "text": "ago-- you can't just\ncompare s against t.", "start": 2091.05, "duration": 1.822 }, { "text": "They're not going to\nequal the same thing", "start": 2092.872, "duration": 1.708 }, { "text": "if you type in two independent strings.", "start": 2094.58, "duration": 2.34 }, { "text": "We need a special function that actually\ncompares strings in a conceptual way.", "start": 2096.92, "duration": 5.33 }, { "text": "I mean that a string is equal to another\nstring if every one of its characters", "start": 2102.25, "duration": 5.83 }, { "text": "equals every one of the\nother string's characters.", "start": 2108.08, "duration": 2.83 }, { "text": "Thankfully, there exists in C a function\nthat does exactly that called strcomp,", "start": 2110.91, "duration": 4.67 }, { "text": "string compare, and it\ntakes two arguments.", "start": 2115.58, "duration": 3.09 }, { "text": "The first is a string.", "start": 2118.67, "duration": 1.14 }, { "text": "The second is a string.", "start": 2119.81, "duration": 1.01 }, { "text": "Or more properly, the first\nis the address of a string.", "start": 2120.82, "duration": 2.58 }, { "text": "Or even more properly, the first\nis the address of a character.", "start": 2123.4, "duration": 4.3 }, { "text": "The second is the\naddress of a character.", "start": 2127.7, "duration": 2.25 }, { "text": "And str compare is just going to\nhope that both of those strings", "start": 2129.95, "duration": 4.92 }, { "text": "eventually end in a\nbackslash zero, so that they", "start": 2134.87, "duration": 4.01 }, { "text": "don't loop forever through memory.", "start": 2138.88, "duration": 1.6 }, { "text": "They eventually hit that\nspecial null terminating byte.", "start": 2140.48, "duration": 2.85 }, { "text": "And if so, and those characters are\nall entirely equal, you print same.", "start": 2143.33, "duration": 4.23 }, { "text": "Else, as before, we print out different.", "start": 2147.56, "duration": 4.24 }, { "text": "So now, when I compile this\nprogram, make compare 1,", "start": 2151.8, "duration": 3.64 }, { "text": "and then I do ./compare1.", "start": 2155.44, "duration": 4.73 }, { "text": "Now I'll type in david in all\nlowercase, david in all lowercase.", "start": 2160.17, "duration": 3.951 }, { "text": "They're indeed the same.", "start": 2164.121, "duration": 0.999 }, { "text": "Let's do it again.", "start": 2165.12, "duration": 0.94 }, { "text": "Zamyla with a capital Z. Zamyla with a\ncapital Z. There are indeed the same.", "start": 2166.06, "duration": 4.93 }, { "text": "Let's do Zamyla with a capital\nZ. zamyla with a lowercase z.", "start": 2170.99, "duration": 3.97 }, { "text": "Different.", "start": 2174.96, "duration": 1.09 }, { "text": "And then FUBAR, clearly different.", "start": 2176.05, "duration": 2.95 }, { "text": "Now we're actually comparing\nthese things properly.", "start": 2179.0, "duration": 2.7 }, { "text": "Because now we're appreciating what\nit actually means to be a string,", "start": 2181.7, "duration": 3.76 }, { "text": "and we are underneath the hood\ncomparing what we should be doing.", "start": 2185.46, "duration": 3.69 }, { "text": "Now, underneath the hood,\nwhat is str compare doing?", "start": 2189.15, "duration": 2.21 }, { "text": "Honestly, it's probably just\na while loop or a for loop", "start": 2191.36, "duration": 2.71 }, { "text": "that is iterating over the\nstring and their lengths", "start": 2194.07, "duration": 2.67 }, { "text": "and looking at the i-th\ncharacter in each string,", "start": 2196.74, "duration": 3.05 }, { "text": "and making sure they're\nall in fact equal.", "start": 2199.79, "duration": 2.49 }, { "text": "Let's go ahead and fix\ncopy with version 1 here.", "start": 2202.28, "duration": 2.58 }, { "text": "Copy 1.c that I've written in\nadvance now looks like this.", "start": 2204.86, "duration": 4.06 }, { "text": "I still prompt the user for a\nstring s with these lines here.", "start": 2208.92, "duration": 4.73 }, { "text": "I then, just to be safe, say\nhey, wait a minute, if s equals", "start": 2213.65, "duration": 3.0 }, { "text": "equals null, return 1.", "start": 2216.65, "duration": 1.179 }, { "text": "And again, 1 is just arbitrary.", "start": 2217.829, "duration": 1.291 }, { "text": "I just want to get out, lest\nI break something later.", "start": 2219.12, "duration": 3.59 }, { "text": "Now, down here, this\nis a new line of code.", "start": 2222.71, "duration": 2.227 }, { "text": "And this is perhaps one of\nthe most powerful ingredients", "start": 2224.937, "duration": 2.333 }, { "text": "we'll see this week, is this\nnew function called malloc,", "start": 2227.27, "duration": 3.55 }, { "text": "memory allocate.", "start": 2230.82, "duration": 1.64 }, { "text": "This is a special function via which\nyou can ask the operating system, Linux", "start": 2232.46, "duration": 5.05 }, { "text": "in the case here, or Mac OS, or Windows,\nif you're running the code locally,", "start": 2237.51, "duration": 3.4 }, { "text": "hey operating system, please give\nme a bunch of bytes of memory.", "start": 2240.91, "duration": 5.22 }, { "text": "Now, why do I want this?", "start": 2246.13, "duration": 1.17 }, { "text": "This program is copy 1.c.", "start": 2247.3, "duration": 1.64 }, { "text": "The goal at hand is to create a\nprogram that copies a string s", "start": 2248.94, "duration": 4.24 }, { "text": "and stores the copy in t, so to speak.", "start": 2253.18, "duration": 2.47 }, { "text": "Last time, it was not sufficient\njust to say t equals s,", "start": 2255.65, "duration": 3.06 }, { "text": "because that copied the addresses.", "start": 2258.71, "duration": 1.87 }, { "text": "That didn't give me a copy of\nZ-A-M-Y-- Z-A-M-Y-L-A backlash 0.", "start": 2260.58, "duration": 7.31 }, { "text": "It instead just gave me\na copy of the address.", "start": 2267.89, "duration": 2.27 }, { "text": "So how do I get a complete\ncopy of Zamyla's name?", "start": 2270.16, "duration": 4.18 }, { "text": "I need to preemptively do\na little bit of arithmetic", "start": 2274.34, "duration": 2.63 }, { "text": "and say, all right, how\nlong is Zamyla's name?", "start": 2276.97, "duration": 2.05 }, { "text": "Well, it's the length\nof s, str len of s.", "start": 2279.02, "duration": 4.06 }, { "text": "But plus one.", "start": 2283.08, "duration": 1.223 }, { "text": "Why plus one?", "start": 2284.303, "duration": 2.878 }, { "text": "Why plus one?", "start": 2287.181, "duration": 1.409 }, { "text": "Yeah?", "start": 2288.59, "duration": 2.05 }, { "text": "Exactly.", "start": 2290.64, "duration": 0.59 }, { "text": "We now were hit the lowest\nlevel of the computer.", "start": 2291.23, "duration": 2.91 }, { "text": "If we don't ask the operating system for\nmemory for that extra backslash 0 byte,", "start": 2294.14, "duration": 5.047 }, { "text": "we're not going to get it.", "start": 2299.187, "duration": 1.083 }, { "text": "So we have to explicitly\nsay, give me one more byte,", "start": 2300.27, "duration": 2.266 }, { "text": "because I know how strings are\nimplemented underneath the hood.", "start": 2302.536, "duration": 2.624 }, { "text": "I need to put that backslash\nzero there, ultimately,", "start": 2305.16, "duration": 2.3 }, { "text": "and then whatever that expression is,\nthe length of Zamyla, so Z-A-M-Y-L-A,", "start": 2307.46, "duration": 4.45 }, { "text": "six, plus one, seven bytes.", "start": 2311.91, "duration": 2.95 }, { "text": "Times the size of a character.", "start": 2314.86, "duration": 1.9 }, { "text": "Turns out it's always going\nto be one, by definition.", "start": 2316.76, "duration": 2.26 }, { "text": "But just for good measure, I'm\nclearly saying, give me seven times", "start": 2319.02, "duration": 3.49 }, { "text": "the size of a char,\nwhich is going to be one.", "start": 2322.51, "duration": 2.37 }, { "text": "That gives me seven total bytes.", "start": 2324.88, "duration": 1.96 }, { "text": "So just to simplify.", "start": 2326.84, "duration": 1.41 }, { "text": "If you multiply all this out,\nbecause the line looks unnecessarily", "start": 2328.25, "duration": 2.81 }, { "text": "cryptic at the moment, this really\nis equivalent, at the moment,", "start": 2331.06, "duration": 6.3 }, { "text": "to just this.", "start": 2337.36, "duration": 1.18 }, { "text": "Call the function malloc.", "start": 2338.54, "duration": 1.45 }, { "text": "Give it the number seven, so that\nmalloc, and in turn, the operating", "start": 2339.99, "duration": 3.66 }, { "text": "system, looks inside of its memory\nbank, so to speak, and says,", "start": 2343.65, "duration": 2.7 }, { "text": "hmm, where are there are seven available\nbytes that aren't currently in use?", "start": 2346.35, "duration": 3.58 }, { "text": "Ah, here is a chunk of them.", "start": 2349.93, "duration": 1.73 }, { "text": "And it's a contiguous chunk.", "start": 2351.66, "duration": 1.37 }, { "text": "It's going to find a block of memory,\na rectangular region, if you will,", "start": 2353.03, "duration": 3.44 }, { "text": "and grab seven bytes, and\nreturn them to my main function.", "start": 2356.47, "duration": 6.64 }, { "text": "But what do I mean to\nreturn a chunk of memory?", "start": 2363.11, "duration": 2.34 }, { "text": "Well, just as get\nstring returns a string", "start": 2365.45, "duration": 3.92 }, { "text": "by returning the address of the\nfirst character in that string,", "start": 2369.37, "duration": 3.95 }, { "text": "so does malloc equivalently\nsimply return the address", "start": 2373.32, "duration": 3.9 }, { "text": "of the first byte of memory.", "start": 2377.22, "duration": 3.7 }, { "text": "But the danger now is\nthat unlike a string,", "start": 2380.92, "duration": 3.07 }, { "text": "malloc is not giving you characters.", "start": 2383.99, "duration": 1.88 }, { "text": "It's just giving you seven bytes in\na row that you are now free to use.", "start": 2385.87, "duration": 3.93 }, { "text": "It does not give you a backslash\nzero at the end of them.", "start": 2389.8, "duration": 3.19 }, { "text": "If you want to remember the\nlength of the chunk of memory", "start": 2392.99, "duration": 3.43 }, { "text": "you just allocated, the burden is\nentirely on you, the programmer.", "start": 2396.42, "duration": 3.682 }, { "text": "And indeed, one of the most common\nsources of bugs in writing code in C", "start": 2400.102, "duration": 2.958 }, { "text": "is to forget about how long\nwas this chunk of memory,", "start": 2403.06, "duration": 2.55 }, { "text": "and to accidentally, with a loop,\ngo too far past the end of it.", "start": 2405.61, "duration": 3.376 }, { "text": "And we'll see what can\nhappen in those cases.", "start": 2408.986, "duration": 1.874 }, { "text": "So now, assuming I do have in t the\naddress of that chunk of memory,", "start": 2410.86, "duration": 3.92 }, { "text": "let me just say, if t equals\nequals null, return 1.", "start": 2414.78, "duration": 3.73 }, { "text": "Something happened that's bad,\nprobably the operating system just", "start": 2418.51, "duration": 3.21 }, { "text": "didn't have seven extra\nbytes of memory to give me.", "start": 2421.72, "duration": 2.27 }, { "text": "So fine, I'll quit.", "start": 2423.99, "duration": 1.51 }, { "text": "Then down here, what do I want to do?", "start": 2425.5, "duration": 1.88 }, { "text": "Well, I now need to implement, at\nleast in this example, my own copying", "start": 2427.38, "duration": 4.98 }, { "text": "process.", "start": 2432.36, "duration": 1.46 }, { "text": "Here, at this point in the story,\nI have two variables, s and t.", "start": 2433.82, "duration": 4.12 }, { "text": "s contains the address\nof Zamyla's name. t", "start": 2437.94, "duration": 2.975 }, { "text": "contains the address of a new\nchunk of memory of length seven.", "start": 2440.915, "duration": 3.495 }, { "text": "So here's what I want to do.", "start": 2444.41, "duration": 1.22 }, { "text": "Just like a couple of\nweeks ago, I'm going", "start": 2445.63, "duration": 1.92 }, { "text": "to iterate from zero on up\nto the length of the string.", "start": 2447.55, "duration": 3.65 }, { "text": "But not up to, but up through\nthe length of the string.", "start": 2451.2, "duration": 2.684 }, { "text": "Because in this case, I actually\nwant to iterate with a for loop", "start": 2453.884, "duration": 2.666 }, { "text": "up through that backslash 0 byte.", "start": 2456.55, "duration": 3.33 }, { "text": "And then just this syntax\nfrom a couple of weeks ago,", "start": 2459.88, "duration": 2.41 }, { "text": "when we simply manipulated strings as\nfor our cryptography ciphers, character", "start": 2462.29, "duration": 6.06 }, { "text": "by character.", "start": 2468.35, "duration": 0.98 }, { "text": "Make the i-th character of t\nequal to the i-th character of s.", "start": 2469.33, "duration": 3.95 }, { "text": "And this is perfectly valid,\nbecause so long as this loop doesn't", "start": 2473.28, "duration": 3.32 }, { "text": "go past n, the number of characters\nthat I allocated, seven, in this case,", "start": 2476.6, "duration": 5.58 }, { "text": "I can go to t bracket 0, bracket 1,\nbracket 2, all the way up through n,", "start": 2482.18, "duration": 5.64 }, { "text": "effectively copying the string.", "start": 2487.82, "duration": 2.66 }, { "text": "And so now when I actually print out s\nand t, I should see truly a copy of t.", "start": 2490.48, "duration": 8.12 }, { "text": "Because even when I\nforce its first character", "start": 2498.6, "duration": 2.37 }, { "text": "to lower case with this same\nline of code here as before,", "start": 2500.97, "duration": 3.24 }, { "text": "I'm actually changing different memory.", "start": 2504.21, "duration": 2.884 }, { "text": "So let's compile this.", "start": 2507.094, "duration": 0.916 }, { "text": "Make copy 1, ./copy1.", "start": 2508.01, "duration": 3.62 }, { "text": "And let me go ahead and type\nin zamyla in all lowercase,", "start": 2511.63, "duration": 3.89 }, { "text": "and now notice the\nprogram does seem to work.", "start": 2515.52, "duration": 2.86 }, { "text": "Zamyla is reprinted in lower\ncase for s, but it's then", "start": 2518.38, "duration": 3.67 }, { "text": "print in uppercase for\nits first letter for t.", "start": 2522.05, "duration": 2.62 }, { "text": "And because the z's look pretty\nsimilar, let's do my name again,", "start": 2524.67, "duration": 2.93 }, { "text": "whereby I type david in all lowercase.", "start": 2527.6, "duration": 2.47 }, { "text": "Type Enter.", "start": 2530.07, "duration": 0.64 }, { "text": "And now, you see s is\nstill david all lowercase,", "start": 2530.71, "duration": 3.14 }, { "text": "but t has only now been\ncapitalized itself.", "start": 2533.85, "duration": 3.36 }, { "text": "It hasn't had a side\neffect of some sort on s,", "start": 2537.21, "duration": 1.98 }, { "text": "because they're different\nchunks of memory.", "start": 2539.19, "duration": 2.571 }, { "text": "Why?", "start": 2541.761, "duration": 0.499 }, { "text": "Well, what has just happened\nin this program is this.", "start": 2542.26, "duration": 3.72 }, { "text": "We have, again, done\nstring s gets get string.", "start": 2545.98, "duration": 4.73 }, { "text": "And when we typed get\nstring, this gives me", "start": 2553.34, "duration": 5.52 }, { "text": "a chunk of memory for the address of s.", "start": 2558.86, "duration": 2.32 }, { "text": "Get string gives me a name,\nlike D-A-V-I-D in all lowercase,", "start": 2561.18, "duration": 4.84 }, { "text": "plus that backslash 0.", "start": 2566.02, "duration": 1.64 }, { "text": "Which again, is really just an\narray underneath the hood like this,", "start": 2567.66, "duration": 3.6 }, { "text": "that starts at some byte, and maybe\nit's again, by coincidence, 123.", "start": 2571.26, "duration": 3.446 }, { "text": "Just because it's easy\nto say, but that's not", "start": 2574.706, "duration": 1.874 }, { "text": "where it's necessarily going to end up.", "start": 2576.58, "duration": 1.624 }, { "text": "And so what ends up here is 123.", "start": 2578.204, "duration": 3.176 }, { "text": "And then later, when\nI allocate t, I again", "start": 2581.38, "duration": 3.95 }, { "text": "get this little chunk of\nmemory that's supposed", "start": 2585.33, "duration": 3.08 }, { "text": "to store the address of a character.", "start": 2588.41, "duration": 2.41 }, { "text": "And actually, recall\nthat we're now doing this", "start": 2590.82, "duration": 2.22 }, { "text": "as char star, not even string.", "start": 2593.04, "duration": 3.31 }, { "text": "So t is similarly a char star.", "start": 2596.35, "duration": 2.36 }, { "text": "And what happens, malloc,\nwhen I ask it for seven bytes,", "start": 2598.71, "duration": 3.57 }, { "text": "gives me 1, 2, 3, 4, 5,\n6, 7 bytes of memory.", "start": 2602.28, "duration": 5.47 }, { "text": "There's no null terminating\ncharacter just yet.", "start": 2607.75, "duration": 2.02 }, { "text": "It's just a block of memory.", "start": 2609.77, "duration": 1.19 }, { "text": "And frankly, there could be\nsome random values here, as", "start": 2610.96, "duration": 3.42 }, { "text": "denoted with question marks here.", "start": 2614.38, "duration": 1.79 }, { "text": "It's just a chunk of memory that\nmight have been used previously", "start": 2616.17, "duration": 2.68 }, { "text": "in my program for some other purpose.", "start": 2618.85, "duration": 1.66 }, { "text": "But what gets stored here, if\nthis happens to be at address 234,", "start": 2620.51, "duration": 5.12 }, { "text": "is this value here, 234.", "start": 2625.63, "duration": 2.85 }, { "text": "And if you're not liking\nthe numbers, again, you", "start": 2628.48, "duration": 2.18 }, { "text": "can think of these as just\nbeing pointers, arrows,", "start": 2630.66, "duration": 3.85 }, { "text": "to these chunks of memory.", "start": 2634.51, "duration": 1.74 }, { "text": "But now, in my C code, when I have\na few lines above this loop whereby", "start": 2636.25, "duration": 9.24 }, { "text": "I am copying from s\nbracket i into t bracket", "start": 2645.49, "duration": 3.79 }, { "text": "i, each of the characters in my\nloop, what's actually happening?", "start": 2649.28, "duration": 5.2 }, { "text": "Well, fairly intuitively, this lower\ncase d ends up getting copied here.", "start": 2654.48, "duration": 4.96 }, { "text": "This lower case a ends\nup getting copied here.", "start": 2659.44, "duration": 2.692 }, { "text": "V-I-D, on through.", "start": 2662.132, "duration": 3.218 }, { "text": "And David can't count,\nso-- backslash-- oh, right.", "start": 2665.35, "duration": 3.467 }, { "text": "David's name is shorter than Zamyla's\nname, which means we didn't actually", "start": 2668.817, "duration": 3.083 }, { "text": "ask for this many characters over here.", "start": 2671.9, "duration": 2.79 }, { "text": "But we have taken the\ncomputer more literally now.", "start": 2674.69, "duration": 2.49 }, { "text": "Give me six bytes, not\nseven bytes, in this case.", "start": 2677.18, "duration": 2.53 }, { "text": "And then literally copy each of the\ncharacters from the original string", "start": 2679.71, "duration": 3.94 }, { "text": "into this new string all the way up\nthrough that backslash 0 character.", "start": 2683.65, "duration": 5.0 }, { "text": "And then when you capitalize\nthe first character in t,", "start": 2688.65, "duration": 3.73 }, { "text": "you are literally only changing\nthis-- we can do better than this.", "start": 2692.38, "duration": 5.27 }, { "text": "We are only changing this\nfirst character here,", "start": 2697.65, "duration": 4.22 }, { "text": "which looks like that now.", "start": 2701.87, "duration": 3.46 }, { "text": "And that's what's going\non underneath the hood.", "start": 2705.33, "duration": 2.54 }, { "text": "So this is why, then, in\nthe beginning of the class,", "start": 2707.87, "duration": 2.18 }, { "text": "we don't introduce\nstrings as char stars,", "start": 2710.05, "duration": 2.19 }, { "text": "because you very quickly get\ncaught up in a lot of this minutia.", "start": 2712.24, "duration": 2.797 }, { "text": "But at the end of the day,\nit's not all that complicated", "start": 2715.037, "duration": 2.333 }, { "text": "once you realize that a\nstring is just an address,", "start": 2717.37, "duration": 2.95 }, { "text": "and malloc, this new function,\nalso just returns an address.", "start": 2720.32, "duration": 3.25 }, { "text": "This is very powerful,\nbecause now you have", "start": 2723.57, "duration": 2.17 }, { "text": "these sort of breadcrumbs that can\nlead you to different places in memory.", "start": 2725.74, "duration": 4.15 }, { "text": "A little map, so to speak, that can\nlead you to actual strings in memory,", "start": 2729.89, "duration": 3.95 }, { "text": "and can actually now solve\nproblems more effectively.", "start": 2733.84, "duration": 2.78 }, { "text": "For instance, we can go back\nand solve one other problem", "start": 2736.62, "duration": 2.47 }, { "text": "we saw a moment ago, which was swap.", "start": 2739.09, "duration": 4.17 }, { "text": "So this version of swap was broken why?", "start": 2743.26, "duration": 4.18 }, { "text": "What was the source of\nthis fundamental problem?", "start": 2747.44, "duration": 3.99 }, { "text": "Yeah?", "start": 2751.43, "duration": 1.319 }, { "text": "STUDENT: [INAUDIBLE]", "start": 2752.749, "duration": 0.833 }, { "text": "DAVID J. MALAN: Yeah.", "start": 2755.765, "duration": 0.875 }, { "text": "When you went back to main, you\nerased the memory on top of it,", "start": 2756.64, "duration": 3.0 }, { "text": "and the fundamental problem there\nwas when I passed in x and y,", "start": 2759.64, "duration": 5.55 }, { "text": "they became copies called a and\nb, in different chunks of memory.", "start": 2765.19, "duration": 4.48 }, { "text": "And so the fundamental\nproblem seems to be", "start": 2769.67, "duration": 2.93 }, { "text": "that swap is incorrectly implemented.", "start": 2772.6, "duration": 2.61 }, { "text": "It's logically correct.", "start": 2775.21, "duration": 1.58 }, { "text": "It does swap two values.", "start": 2776.79, "duration": 1.2 }, { "text": "And we saw that with debug 50, but it's\nkind of fundamentally flawed in so far", "start": 2777.99, "duration": 4.77 }, { "text": "as it requires, it\nseems, by design of C,", "start": 2782.76, "duration": 3.81 }, { "text": "that a and b be passed in by\nvalue as copies, so to speak.", "start": 2786.57, "duration": 4.8 }, { "text": "We need some way to change this function\nto say main, hey, uh-uh, don't give me", "start": 2791.37, "duration": 4.62 }, { "text": "copies of your variables.", "start": 2795.99, "duration": 1.58 }, { "text": "Give me a treasure map that\nwill lead me to your variables.", "start": 2797.57, "duration": 3.49 }, { "text": "Give me the address of x.", "start": 2801.06, "duration": 2.12 }, { "text": "Give me the address of y.", "start": 2803.18, "duration": 1.91 }, { "text": "And I'll still call them a\nand b, or whatever I want,", "start": 2805.09, "duration": 2.37 }, { "text": "but lead me to the original values.", "start": 2807.46, "duration": 2.5 }, { "text": "Don't just pass me\ncopies of those values.", "start": 2809.96, "duration": 2.93 }, { "text": "And so we can change swap as follows\nfrom a program or a function that's", "start": 2812.89, "duration": 4.32 }, { "text": "incorrect entirely, but\ninto one that is correct.", "start": 2817.21, "duration": 5.23 }, { "text": "And we need to change\nthe syntax a little bit.", "start": 2822.44, "duration": 2.12 }, { "text": "So before is what we had here.", "start": 2824.56, "duration": 3.2 }, { "text": "After is what we now have.", "start": 2827.76, "duration": 2.23 }, { "text": "Before, after.", "start": 2829.99, "duration": 2.12 }, { "text": "Before, after.", "start": 2832.11, "duration": 1.619 }, { "text": "So if you see it in\nrapid succession there,", "start": 2833.729, "duration": 1.791 }, { "text": "all you see is that a whole bunch\nof stars are appearing in the code.", "start": 2835.52, "duration": 3.64 }, { "text": "And unfortunately, C was not\ndesigned in the best of ways", "start": 2839.16, "duration": 3.85 }, { "text": "to make clear what star\nmeans in different contexts,", "start": 2843.01, "duration": 2.57 }, { "text": "but it's all related as follows.", "start": 2845.58, "duration": 2.14 }, { "text": "The fact that I have now\nchanged a and b to be not ints,", "start": 2847.72, "duration": 4.61 }, { "text": "but ints stars, int pointers, if you\nwill, means that when main calls swap,", "start": 2852.33, "duration": 7.42 }, { "text": "it is by design of the C language,\ngoing to pass in the address of x", "start": 2859.75, "duration": 4.07 }, { "text": "and the address of y.", "start": 2863.82, "duration": 0.95 }, { "text": "So that's what the star means.", "start": 2864.77, "duration": 1.25 }, { "text": "Give me the address of an int and the\naddress of an int, not actual ints.", "start": 2866.02, "duration": 4.08 }, { "text": "Now, down here, the star unfortunately\nmeans something slightly different,", "start": 2870.1, "duration": 3.23 }, { "text": "but related in spirit.", "start": 2873.33, "duration": 1.79 }, { "text": "Int temp just gives me an integer,\nan int variable called temp.", "start": 2875.12, "duration": 4.78 }, { "text": "Star a, in this context, without\nthe word int in front of it again,", "start": 2879.9, "duration": 5.01 }, { "text": "means go to that location.", "start": 2884.91, "duration": 2.66 }, { "text": "Follow the treasure map, so to speak.", "start": 2887.57, "duration": 1.8 }, { "text": "Go to the address that is in a.", "start": 2889.37, "duration": 2.71 }, { "text": "So for instance, if the\naddress of a is say,", "start": 2892.08, "duration": 3.78 }, { "text": "33 Oxford Street,\nCambridge, Massachusetts.", "start": 2895.86, "duration": 2.739 }, { "text": "That happens to be the address\nof the CS building at Harvard.", "start": 2898.599, "duration": 2.541 }, { "text": "Star a means go to 33 Oxford\nStreet in Cambridge, Massachusetts.", "start": 2901.14, "duration": 5.06 }, { "text": "The star just means go to\nthat particular address.", "start": 2906.2, "duration": 4.12 }, { "text": "So what does that mean, then, down\nhere, when I say star a gets star b?", "start": 2910.32, "duration": 4.17 }, { "text": "That means go to the address\nin b and get its value,", "start": 2914.49, "duration": 3.56 }, { "text": "and store it at whenever\na is pointing at to.", "start": 2918.05, "duration": 3.45 }, { "text": "So go to a and wait for me for a value.", "start": 2921.5, "duration": 2.97 }, { "text": "Go to b, get a value, and put\nthat value at the location in a.", "start": 2924.47, "duration": 4.61 }, { "text": "And then lastly, this just\nmeans go to the location in b,", "start": 2929.08, "duration": 3.645 }, { "text": "go to whatever building\nthat is, so to speak,", "start": 2932.725, "duration": 2.295 }, { "text": "and put the value that is in\ntemp inside of that building.", "start": 2935.02, "duration": 4.88 }, { "text": "So a pointer is just an address.", "start": 2939.9, "duration": 2.96 }, { "text": "These stars just mean\npointers are involved.", "start": 2942.86, "duration": 3.75 }, { "text": "Give me the address of an int,\ngive me the address of an int.", "start": 2946.61, "duration": 3.04 }, { "text": "And again, confusing,\nadmittedly, the star", "start": 2949.65, "duration": 2.58 }, { "text": "in this context where we don't have\nthe word int in front of it again,", "start": 2952.23, "duration": 3.73 }, { "text": "on the side of the equal sign,\njust means go to that address.", "start": 2955.96, "duration": 2.83 }, { "text": "Go to that building.", "start": 2958.79, "duration": 1.29 }, { "text": "Go to that other building\nand put something there.", "start": 2960.08, "duration": 3.55 }, { "text": "So we can now fix our\nswap program correctly.", "start": 2963.63, "duration": 3.98 }, { "text": "We can now open up, as I will here,\nswap dot c, which I wrote in advance.", "start": 2967.61, "duration": 5.1 }, { "text": "That looks almost the same, except\nthat I've changed the swap function", "start": 2972.71, "duration": 3.94 }, { "text": "as follows. a is now\nint*, b is now int*,", "start": 2976.65, "duration": 3.5 }, { "text": "and I also borrowed the stars\ninside of the function, as well.", "start": 2980.15, "duration": 3.62 }, { "text": "But something's gotta change.", "start": 2983.77, "duration": 1.88 }, { "text": "There's one more line of code I need\nto change for all of this to work.", "start": 2985.65, "duration": 4.46 }, { "text": "What is that?", "start": 2990.11, "duration": 0.67 }, { "text": "What line needs to change?", "start": 2994.161, "duration": 1.649 }, { "text": "Well who cares about swap?", "start": 2995.81, "duration": 1.73 }, { "text": "It's main that was calling\nthis thing in the first place,", "start": 2997.54, "duration": 3.07 }, { "text": "so let's go back to the original story.", "start": 3000.61, "duration": 2.29 }, { "text": "Main, here, declares x and y as 1 and\n2, does some printfs here, as before.", "start": 3002.9, "duration": 5.73 }, { "text": "But notice this line has to change.", "start": 3008.63, "duration": 1.64 }, { "text": "So one more piece of syntax today.", "start": 3010.27, "duration": 2.739 }, { "text": "And we're running out of new symbols.", "start": 3013.009, "duration": 1.541 }, { "text": "We've seen most of C already.", "start": 3014.55, "duration": 2.11 }, { "text": "&x and &y means get me the address\nof x, and get me the address of y,", "start": 3016.66, "duration": 7.0 }, { "text": "and pass those in instead.", "start": 3023.66, "duration": 1.89 }, { "text": "So x,y would just mean pass in\na copy of x and a copy of y,", "start": 3025.55, "duration": 4.11 }, { "text": "or the values thereof.", "start": 3029.66, "duration": 2.23 }, { "text": "&x &y means give me a little, you know,\nmap with the address of x and a little", "start": 3031.89, "duration": 5.36 }, { "text": "map with the address of y, so that\nswap-- who's receiving those maps--", "start": 3037.25, "duration": 4.71 }, { "text": "can go there.", "start": 3041.96, "duration": 1.98 }, { "text": "So what does this mean\nin pictorial form?", "start": 3043.94, "duration": 2.81 }, { "text": "If we now go back to the\nbeginning of this story,", "start": 3046.75, "duration": 2.26 }, { "text": "where we were looking at my computer's\nmemory as this big rectangular region", "start": 3049.01, "duration": 4.53 }, { "text": "like this.", "start": 3053.54, "duration": 1.26 }, { "text": "With main's chunk of\nmemory at the bottom here.", "start": 3054.8, "duration": 4.61 }, { "text": "And inside of main was two variables,\nlike x, and another variable y.", "start": 3059.41, "duration": 6.16 }, { "text": "And inside of those were\nthe numbers 1 and 2.", "start": 3065.57, "duration": 3.4 }, { "text": "And then I called swap.", "start": 3068.97, "duration": 1.92 }, { "text": "And so swap gets its own frame\non the stack, so to speak.", "start": 3070.89, "duration": 3.57 }, { "text": "This is swap's frame.", "start": 3074.46, "duration": 1.77 }, { "text": "It, too, had a variable called\na and a variable called b.", "start": 3076.23, "duration": 6.472 }, { "text": "But what goes in there now?", "start": 3082.702, "duration": 1.638 }, { "text": "It's not 1 and 2.", "start": 3084.34, "duration": 1.49 }, { "text": "We need to know a little something\nmore about my computer's memory.", "start": 3085.83, "duration": 2.8 }, { "text": "And I don't know where\neverything's laid out,", "start": 3088.63, "duration": 2.22 }, { "text": "but let me just arbitrarily\nassume that, you know,", "start": 3090.85, "duration": 3.42 }, { "text": "it's inside of my computer's memory.", "start": 3094.27, "duration": 1.92 }, { "text": "Maybe this is byte number 90.", "start": 3096.19, "duration": 2.48 }, { "text": "This is going to be 91.", "start": 3098.67, "duration": 1.97 }, { "text": "This here is going to be 92,\n93, 94, 95, and so forth.", "start": 3100.64, "duration": 3.91 }, { "text": "I just need to know that there's\nsome kind of numbering scheme there.", "start": 3104.55, "duration": 3.33 }, { "text": "So what goes inside of a is 91.", "start": 3107.88, "duration": 3.51 }, { "text": "What goes inside of b is 92.", "start": 3111.39, "duration": 3.94 }, { "text": "And not the values 1 and 2, but rather\nthe addresses of those values 1 and 2.", "start": 3115.33, "duration": 7.22 }, { "text": "Because now my code for the swap\nfunction, consider what it does.", "start": 3122.55, "duration": 4.23 }, { "text": "It says, upon receiving\nthe address of an integer,", "start": 3126.78, "duration": 3.44 }, { "text": "called a, upon receiving the address\nof another integer, called b,", "start": 3130.22, "duration": 4.531 }, { "text": "go there and store that value in temp.", "start": 3134.751, "duration": 3.469 }, { "text": "Go to the address in b and store\nthat value at the address in a.", "start": 3138.22, "duration": 4.46 }, { "text": "Store the value in temp\nat the address in b.", "start": 3142.68, "duration": 3.6 }, { "text": "So let's see what happens then.", "start": 3146.28, "duration": 2.26 }, { "text": "So first of all, I need another\nvariable here, called temp.", "start": 3148.54, "duration": 4.51 }, { "text": "Temp, meanwhile, is not a pointer.", "start": 3153.05, "duration": 3.56 }, { "text": "It's just an integer,\nbut what does it store?", "start": 3156.61, "duration": 3.54 }, { "text": "Well, according to my\ncode, temp gets the value", "start": 3160.15, "duration": 4.55 }, { "text": "of going to a, going\nto the address in a.", "start": 3164.7, "duration": 3.25 }, { "text": "So what is a?", "start": 3167.95, "duration": 1.583 }, { "text": "a is 99.", "start": 3169.533, "duration": 1.777 }, { "text": "That's like a treasure map leading\nto, OK, this chunk of memory", "start": 3171.31, "duration": 3.4 }, { "text": "down here in my computer.", "start": 3174.71, "duration": 1.61 }, { "text": "And what value is there\nonce I've gone there?", "start": 3176.32, "duration": 2.11 }, { "text": "Once I've gone to the CS building\ninside of it, I see the number 1,", "start": 3178.43, "duration": 3.8 }, { "text": "and so I put the number 1 in temp.", "start": 3182.23, "duration": 3.12 }, { "text": "Meanwhile, my second line of\ncode says go to the address in b", "start": 3185.35, "duration": 4.795 }, { "text": "and grab its value, and put\nit at the address that's in a.", "start": 3190.145, "duration": 5.015 }, { "text": "So what does that mean?", "start": 3195.16, "duration": 1.64 }, { "text": "Well, star b means\nstart here and go to 92.", "start": 3196.8, "duration": 4.767 }, { "text": "So it's like an arrow-- kind\nof like chutes and ladders,", "start": 3201.567, "duration": 2.333 }, { "text": "if you know the game--\nlike go to address 92.", "start": 3203.9, "duration": 2.64 }, { "text": "What value is there?", "start": 3206.54, "duration": 0.99 }, { "text": "The number 2.", "start": 3207.53, "duration": 0.98 }, { "text": "And the other half of the\nequation, on the left,", "start": 3208.51, "duration": 2.0 }, { "text": "said, go to the address\nin a, which is here,", "start": 3210.51, "duration": 2.79 }, { "text": "and copy the 2 into that location.", "start": 3213.3, "duration": 4.89 }, { "text": "And then the last line--\nonly one more line--", "start": 3218.19, "duration": 2.61 }, { "text": "is this, get the value in temp-- that's\neasy-- and put it at the address in b.", "start": 3220.8, "duration": 5.53 }, { "text": "So we have to go to b\nand put temp in there.", "start": 3226.33, "duration": 3.29 }, { "text": "So to do that, here's\ntemp, it's the number 1.", "start": 3229.62, "duration": 4.35 }, { "text": "I have to go to the address in b.", "start": 3233.97, "duration": 1.69 }, { "text": "The address and b is 92.", "start": 3235.66, "duration": 1.18 }, { "text": "So let's go there, and\naha, let me go ahead then,", "start": 3236.84, "duration": 3.44 }, { "text": "and overwrite the value that's\nthere with the number 1.", "start": 3240.28, "duration": 7.39 }, { "text": "So now this frame of\nmemory on the stack--", "start": 3247.67, "duration": 3.5 }, { "text": "the 91, the 92, and the\ntemporary variable--", "start": 3251.17, "duration": 2.45 }, { "text": "they are, by design of my\nnew function, disposable.", "start": 3253.62, "duration": 3.43 }, { "text": "I really don't care, after swap\nreturns, if those things continue--", "start": 3257.05, "duration": 5.07 }, { "text": "I did care a little bit about that.", "start": 3262.12, "duration": 1.46 }, { "text": "I don't care if those\nthings continue to exist.", "start": 3267.43, "duration": 2.52 }, { "text": "All I care about is that\nx and y continue to exist.", "start": 3269.95, "duration": 4.21 }, { "text": "So in this way is the new and\nimproved version of the swap function", "start": 3274.16, "duration": 3.91 }, { "text": "actually having a permanent\nimpact on my data?", "start": 3278.07, "duration": 2.89 }, { "text": "And with the frame, the\nmemory still looks like that,", "start": 3280.96, "duration": 3.07 }, { "text": "because it's gone to the address in a.", "start": 3284.03, "duration": 2.01 }, { "text": "Gone to the address in b, which\nleads it to the original x and y.", "start": 3286.04, "duration": 4.15 }, { "text": "And so by way of pointers,\nby way of these addresses,", "start": 3290.19, "duration": 2.87 }, { "text": "do we have the ability to actually\ngo much, much deeper into a program", "start": 3293.06, "duration": 4.75 }, { "text": "and actually get at\nvalues that previously we", "start": 3297.81, "duration": 3.43 }, { "text": "had no way of even expressing.", "start": 3301.24, "duration": 3.04 }, { "text": "So it's at this point in the story where\nI usually admit that, at least for me,", "start": 3304.28, "duration": 5.13 }, { "text": "this has been among the most challenging\ntopics when I, myself, was a student.", "start": 3309.41, "duration": 3.91 }, { "text": "And in fact, all these years later--\nit's like, 20, 20 year-- yeah,", "start": 3313.32, "duration": 2.905 }, { "text": "I think we're up to 20 years ago.", "start": 3316.225, "duration": 1.375 }, { "text": "20 years ago-- I didn't\ntake this photo then--", "start": 3317.6, "duration": 2.96 }, { "text": "but I sat in what was, at the time,\nthe back right hand corner of Elliot", "start": 3320.56, "duration": 3.59 }, { "text": "House's dining hall, here at Harvard.", "start": 3324.15, "duration": 2.07 }, { "text": "And I sat down with my teaching\nfellow, who of all the TFs I had", "start": 3326.22, "duration": 2.93 }, { "text": "as an undergrad, still remember to\nthis day, [? Nishat ?] [? Meda ?].", "start": 3329.15, "duration": 2.42 }, { "text": "And we just reconnected on\nFacebook, all these years later.", "start": 3331.57, "duration": 2.52 }, { "text": "Very exciting.", "start": 3334.09, "duration": 0.96 }, { "text": "And it was he who\nwonderfully sat down with me", "start": 3335.05, "duration": 2.419 }, { "text": "at office hours one\nday in the dining hall,", "start": 3337.469, "duration": 1.791 }, { "text": "trying to help me understand pointers,\nbecause it was just so much more", "start": 3339.26, "duration": 3.03 }, { "text": "technical than all the other stuff.", "start": 3342.29, "duration": 1.458 }, { "text": "Like, there is no puzzle piece in\nScratch for the address of something", "start": 3343.748, "duration": 3.142 }, { "text": "that leads you somewhere so\npowerfully as these stars seem", "start": 3346.89, "duration": 3.53 }, { "text": "to be able to, here.", "start": 3350.42, "duration": 1.011 }, { "text": "And this is only to say that\nthis is among those topics that", "start": 3351.431, "duration": 2.499 }, { "text": "might take a little bit of\ntime to sink in, but it does.", "start": 3353.93, "duration": 3.33 }, { "text": "And when it does, it really is that\nproverbial light bulb that goes off.", "start": 3357.26, "duration": 3.39 }, { "text": "And for me, that light bulb\nwent off right then and there.", "start": 3360.65, "duration": 4.9 }, { "text": "Now, what more can we do with these\nthings, after that motivational speech?", "start": 3365.55, "duration": 3.37 }, { "text": "Pointer arithmetic.", "start": 3368.92, "duration": 1.22 }, { "text": "So, sort of complicated sounding\ntopic, but really, it just", "start": 3370.14, "duration": 3.58 }, { "text": "goes back to first principles,\nas to what a pointer actually is.", "start": 3373.72, "duration": 3.12 }, { "text": "And it allows us now\nto do things like this.", "start": 3376.84, "duration": 1.99 }, { "text": "Let me go ahead and open up one other\nprogram that I wrote in advance here,", "start": 3378.83, "duration": 3.38 }, { "text": "called pointers dot c.", "start": 3382.21, "duration": 2.11 }, { "text": "And take a look at what\nthis thing does here.", "start": 3384.32, "duration": 2.42 }, { "text": "It works a little differently\nfrom the syntax we're used to,", "start": 3386.74, "duration": 2.72 }, { "text": "and from any of our\ncrypto problems thus far.", "start": 3389.46, "duration": 3.09 }, { "text": "So notice on this first line here,\nI get string and I store in s.", "start": 3392.55, "duration": 4.08 }, { "text": "No more string right\nnow, just char star.", "start": 3396.63, "duration": 1.85 }, { "text": "We can be real and talk about\nit as the address of a char.", "start": 3398.48, "duration": 3.55 }, { "text": "A little sanity check,\nis s equal equal to null?", "start": 3402.03, "duration": 2.44 }, { "text": "If so, just return.", "start": 3404.47, "duration": 0.94 }, { "text": "Something went wrong, so\nlet's not deal with it now.", "start": 3405.41, "duration": 2.32 }, { "text": "Down here, a for loop.", "start": 3407.73, "duration": 1.75 }, { "text": "For i gets 0 all the way up to n.", "start": 3409.48, "duration": 3.71 }, { "text": "So this is just a standard syntax\nwe've used a few times now, even back", "start": 3413.19, "duration": 3.05 }, { "text": "in week 1 when we just\nwanted iterate over.", "start": 3416.24, "duration": 2.36 }, { "text": "Or in week 2, when we wanted iterate\nover the characters in a string.", "start": 3418.6, "duration": 3.67 }, { "text": "But we've never seen this\nkind of craziness before.", "start": 3422.27, "duration": 2.85 }, { "text": "A star, and then some\narithmetic in parentheses.", "start": 3425.12, "duration": 3.47 }, { "text": "In the past, when we wanted to print\nout a character, as implied by %c here,", "start": 3428.59, "duration": 5.0 }, { "text": "we quite simply, as I recall, did this.", "start": 3433.59, "duration": 3.7 }, { "text": "Which was nice and intuitive, right?", "start": 3437.29, "duration": 1.5 }, { "text": "The square brackets\ndenote to treat the string", "start": 3438.79, "duration": 2.327 }, { "text": "as though it's an array, which it\nreally is, an array of characters.", "start": 3441.117, "duration": 2.833 }, { "text": "And that means get the\ni-th character of s.", "start": 3443.95, "duration": 3.05 }, { "text": "But now that we understand what s is, we\ndon't need to use this syntactic sugar,", "start": 3447.0, "duration": 5.12 }, { "text": "as it's called.", "start": 3452.12, "duration": 0.64 }, { "text": "Any time a language has a\nfeature that's convenient to use,", "start": 3452.76, "duration": 2.92 }, { "text": "and easier to read sometimes,\nbut isn't fundamentally", "start": 3455.68, "duration": 2.69 }, { "text": "necessary to express\nyourself, it's often", "start": 3458.37, "duration": 1.75 }, { "text": "called syntactic sugar, which means\nit's just kind of a nicety to have.", "start": 3460.12, "duration": 3.89 }, { "text": "And indeed, that square\nbracket notation is just", "start": 3464.01, "duration": 2.98 }, { "text": "sugar for this more arcane, but\nperhaps more well-defined syntax now.", "start": 3466.99, "duration": 7.42 }, { "text": "The star operator in this context is\nthe dereference operator, technically.", "start": 3474.41, "duration": 4.5 }, { "text": "It's the go there operator,\nas I've been describing it.", "start": 3478.91, "duration": 2.94 }, { "text": "Go to some address.", "start": 3481.85, "duration": 1.52 }, { "text": "Well s, recall, is a string.", "start": 3483.37, "duration": 1.91 }, { "text": "But there is no string.", "start": 3485.28, "duration": 1.27 }, { "text": "Strings are just the\naddresses of characters now.", "start": 3486.55, "duration": 2.32 }, { "text": "The first in a string.", "start": 3488.87, "duration": 1.94 }, { "text": "So initially in this\nloop, what am I doing?", "start": 3490.81, "duration": 3.42 }, { "text": "s is the address of a string, the\naddress of its first character.", "start": 3494.23, "duration": 3.33 }, { "text": "And I'm saying, add to s, the value i.", "start": 3497.56, "duration": 2.7 }, { "text": "Well, i is just this variable in my\nfor loop that's initialized to 0.", "start": 3500.26, "duration": 4.27 }, { "text": "So s plus 0 is obviously just s.", "start": 3504.53, "duration": 3.19 }, { "text": "s is the address of a char.", "start": 3507.72, "duration": 1.86 }, { "text": "*s means go to s.", "start": 3509.58, "duration": 2.97 }, { "text": "What do you find when you get there?", "start": 3512.55, "duration": 1.91 }, { "text": "A character, because s is a char\nstar, the address of a character.", "start": 3514.46, "duration": 3.82 }, { "text": "And so printing out %c\n*s effectively means,", "start": 3518.28, "duration": 4.41 }, { "text": "go print that character right there.", "start": 3522.69, "duration": 2.2 }, { "text": "On the second iteration of\nthe loop, i is, of course, 1.", "start": 3524.89, "duration": 2.96 }, { "text": "So s plus 1 is 1 byte farther\nfrom the beginning of the string.", "start": 3527.85, "duration": 4.46 }, { "text": "And the star means go to that\ncharacter and print it with %c.", "start": 3532.31, "duration": 4.59 }, { "text": "One more iteration, i is now 2.", "start": 3536.9, "duration": 2.35 }, { "text": "s plus 2 is 2 bytes away\nfrom the start of the string.", "start": 3539.25, "duration": 2.35 }, { "text": "Go there and print that\ncharacter in the string.", "start": 3541.6, "duration": 3.0 }, { "text": "And so forth.", "start": 3544.6, "duration": 0.57 }, { "text": "And do this up until the\nlength of the string.", "start": 3545.17, "duration": 3.3 }, { "text": "Now this is perfectly correct.", "start": 3548.47, "duration": 2.12 }, { "text": "And if you really kind of want\nto look cool with your code", "start": 3550.59, "duration": 2.47 }, { "text": "you can use pointer arithmetic\nin this way, because all it is", "start": 3553.06, "duration": 2.541 }, { "text": "is just expressing more precisely\nwhat is going on underneath the hood.", "start": 3555.601, "duration": 3.668 }, { "text": "But it's a little more cryptic.", "start": 3559.269, "duration": 1.291 }, { "text": "It's certainly a couple more characters.", "start": 3560.56, "duration": 2.29 }, { "text": "But it is functionally\nequivalent to what", "start": 3562.85, "duration": 3.09 }, { "text": "we've been doing for weeks, which\nhas been, again, just s [i].", "start": 3565.94, "duration": 3.86 }, { "text": "So whereas some of today's\nideas are admittedly", "start": 3569.8, "duration": 2.9 }, { "text": "new-- allocating memory\nand actually looking", "start": 3572.7, "duration": 2.69 }, { "text": "underneath the hood\nat what a string is--", "start": 3575.39, "duration": 1.89 }, { "text": "we're not really getting\nnewfound capabilities", "start": 3577.28, "duration": 3.25 }, { "text": "that we didn't already have when it\ncomes to manipulating existing strings.", "start": 3580.53, "duration": 4.82 }, { "text": "So this is pointer arithmetic,\nso to speak, insofar", "start": 3585.35, "duration": 3.54 }, { "text": "as we are doing\narithmetic with pointers.", "start": 3588.89, "duration": 3.88 }, { "text": "Math with pointers.", "start": 3592.77, "duration": 2.73 }, { "text": "All right, let's take a look now\nat where things can go wrong.", "start": 3595.5, "duration": 5.77 }, { "text": "So this is a program written by our\nfriend Nick Parlante at Stanford,", "start": 3601.27, "duration": 3.14 }, { "text": "inspired by Binky, who will\nreturn in just a moment.", "start": 3604.41, "duration": 2.58 }, { "text": "And it's fundamentally broken.", "start": 3606.99, "duration": 1.56 }, { "text": "This code is incorrect.", "start": 3608.55, "duration": 1.17 }, { "text": "It also doesn't really\ndo anything useful.", "start": 3609.72, "duration": 2.26 }, { "text": "But it's meant to be demonstrative\nof things that can go wrong.", "start": 3611.98, "duration": 2.72 }, { "text": "So at the top of this program, notice\nwe are declaring two variables, x and y.", "start": 3614.7, "duration": 4.22 }, { "text": "But today those variables are not ints,\nas they might have been in weeks past,", "start": 3618.92, "duration": 3.41 }, { "text": "they are int*s, the addresses ints.", "start": 3622.33, "duration": 2.8 }, { "text": "They're not being initialized\nyet to any value, so that's fine.", "start": 3625.13, "duration": 2.9 }, { "text": "So really, this is just\ngiving us, if you will,", "start": 3628.03, "duration": 2.84 }, { "text": "like, two boxes on the screen.", "start": 3630.87, "duration": 1.85 }, { "text": "So x at this point in the story looks\nlike this, y at this point in the story", "start": 3632.72, "duration": 4.23 }, { "text": "looks like this.", "start": 3636.95, "duration": 1.07 }, { "text": "I have no idea what's inside of them.", "start": 3638.02, "duration": 1.98 }, { "text": "They have garbage value, so to speak.", "start": 3640.0, "duration": 2.06 }, { "text": "Because if we didn't\nassign them a value,", "start": 3642.06, "duration": 1.79 }, { "text": "the computer is not\ngoing to do it for us,", "start": 3643.85, "duration": 1.79 }, { "text": "so they might just have remnants\nof some past usage of that memory.", "start": 3645.64, "duration": 2.93 }, { "text": "So we don't know what's inside of them.", "start": 3648.57, "duration": 1.625 }, { "text": "But that's OK, because on this next\nline I call malloc and actually allocate", "start": 3650.195, "duration": 6.455 }, { "text": "enough memory for one int.", "start": 3656.65, "duration": 1.95 }, { "text": "Now this is kind of a silly use.", "start": 3658.6, "duration": 2.09 }, { "text": "It's not the best way\nto give yourself an int.", "start": 3660.69, "duration": 2.73 }, { "text": "We've seen for weeks\nnow how you get an int.", "start": 3663.42, "duration": 1.89 }, { "text": "You say, like, int x, or int y, or int\nz, and the computer gives you an int.", "start": 3665.31, "duration": 5.124 }, { "text": "But if we want to use malloc\nin the simplest way possible,", "start": 3670.434, "duration": 2.416 }, { "text": "we can just say, hey, malloc,\ngive me enough space for an int.", "start": 3672.85, "duration": 3.86 }, { "text": "And recall from the past that an\nint here is generally 4 bytes.", "start": 3676.71, "duration": 5.24 }, { "text": "So give me 4 bytes of\nmemory, specifically", "start": 3681.95, "duration": 2.61 }, { "text": "the address of the first of those bytes.", "start": 3684.56, "duration": 2.16 }, { "text": "That's all malloc is doing, and\nit's storing that address in x.", "start": 3686.72, "duration": 2.76 }, { "text": "For good measure I should check for\nnull, but we're not, in this case,", "start": 3689.48, "duration": 3.08 }, { "text": "per Binky.", "start": 3692.56, "duration": 1.0 }, { "text": "So what's the next line do?", "start": 3693.56, "duration": 2.03 }, { "text": "This next line means go to the address\nin x and put the number 42 there.", "start": 3695.59, "duration": 5.94 }, { "text": "That seems OK.", "start": 3701.53, "duration": 0.88 }, { "text": "Because assuming malloc\nreturns the address", "start": 3702.41, "duration": 2.8 }, { "text": "of a chunk of memory, the\nfirst address of 4 bytes,", "start": 3705.21, "duration": 3.0 }, { "text": "we can go there and put the number 42\nin binary, in 4 bytes worth of bits.", "start": 3708.21, "duration": 7.332 }, { "text": "But what about this line?", "start": 3715.542, "duration": 2.418 }, { "text": "I've flagged it in red\nbecause this program probably", "start": 3717.96, "duration": 2.81 }, { "text": "is going to go no further.", "start": 3720.77, "duration": 1.71 }, { "text": "In fact, something very,\nvery bad is about to happen.", "start": 3722.48, "duration": 4.46 }, { "text": "Why?", "start": 3726.94, "duration": 0.992 }, { "text": "Well, what is the value in y?", "start": 3727.932, "duration": 3.278 }, { "text": "Well, originally x was a garbage\nvalue until I called malloc", "start": 3731.21, "duration": 3.63 }, { "text": "and asked malloc, hey, malloc,\ngive me enough space for an int.", "start": 3734.84, "duration": 3.76 }, { "text": "So I'll draw it as a box here.", "start": 3738.6, "duration": 1.66 }, { "text": "Give me the address of\nthat chunk of memory.", "start": 3740.26, "duration": 2.21 }, { "text": "So this question mark is really now\nan arrow to that chunk of memory.", "start": 3742.47, "duration": 3.92 }, { "text": "And then I said, hey, computer, go\nahead and go there and put the value 42.", "start": 3746.39, "duration": 6.56 }, { "text": "But then my next line of code said,\nhey, computer, go to the value in y", "start": 3752.95, "duration": 4.16 }, { "text": "and put the unlucky number 13 there.", "start": 3757.11, "duration": 2.47 }, { "text": "So that's like saying, go-- I don't know\nwhere to go, because this has no value.", "start": 3759.58, "duration": 7.04 }, { "text": "And so something very\nbad is going to happen.", "start": 3766.62, "duration": 2.79 }, { "text": "Because the question mark\nimplies, this is a garbage value.", "start": 3769.41, "duration": 2.65 }, { "text": "Maybe it's 0, maybe it's 1,000, maybe\nit's some number in between or bigger.", "start": 3772.06, "duration": 3.29 }, { "text": "It's some garbage value, which\nmeans if I just go there,", "start": 3775.35, "duration": 3.27 }, { "text": "who knows where I'm going to end up?", "start": 3778.62, "duration": 1.58 }, { "text": "But odds are I'm going\nto end up somewhere", "start": 3780.2, "duration": 1.88 }, { "text": "I shouldn't, because I should not be\ntouching memory that is not my own.", "start": 3782.08, "duration": 4.33 }, { "text": "And indeed, thanks to Binky, we're about\nto see that bad things indeed happen", "start": 3786.41, "duration": 5.38 }, { "text": "when you touch memory\nthat you shouldn't.", "start": 3791.79, "duration": 2.31 }, { "text": "Let's take a look.", "start": 3794.1, "duration": 0.894 }, { "text": "[VIDEO PLAYBACK]", "start": 3794.994, "duration": 0.666 }, { "text": "-Hey, Binky, wake up.", "start": 3797.236, "duration": 2.144 }, { "text": "It's time for pointer fun.", "start": 3799.38, "duration": 2.016 }, { "text": "-What's that?", "start": 3801.396, "duration": 1.694 }, { "text": "Learn about pointers?", "start": 3803.09, "duration": 1.58 }, { "text": "Oh, goody!", "start": 3804.67, "duration": 1.77 }, { "text": "-Well, to get started, I guess we're\ngoing to need a couple pointers.", "start": 3806.44, "duration": 3.01 }, { "text": "-OK.", "start": 3809.45, "duration": 0.8 }, { "text": "This code allocates two pointers\nwhich can point to integers.", "start": 3810.25, "duration": 3.74 }, { "text": "-OK, well, I see the\ntwo pointers, but they", "start": 3813.99, "duration": 2.457 }, { "text": "don't seem to be pointing to anything.", "start": 3816.447, "duration": 1.583 }, { "text": "-That's right.", "start": 3818.03, "duration": 0.81 }, { "text": "Initially, pointers\ndon't point to anything.", "start": 3818.84, "duration": 2.12 }, { "text": "The things they point to are called\npointees, and setting them up's", "start": 3820.96, "duration": 2.98 }, { "text": "a separate step.", "start": 3823.94, "duration": 1.23 }, { "text": "-Oh, right, right, I knew that.", "start": 3825.17, "duration": 1.65 }, { "text": "The pointees are separate.", "start": 3826.82, "duration": 1.65 }, { "text": "Er, so how do you allocate a pointee?", "start": 3828.47, "duration": 2.61 }, { "text": "-OK, well, this code allocates\na new integer pointee,", "start": 3831.08, "duration": 3.78 }, { "text": "and this part sets x to point to it.", "start": 3834.86, "duration": 3.09 }, { "text": "-Hey, that looks better.", "start": 3837.95, "duration": 1.15 }, { "text": "So make it do something.", "start": 3839.1, "duration": 1.75 }, { "text": "-OK, I'll dereference the pointer x to\nstore the number 42 into its pointee.", "start": 3840.85, "duration": 5.62 }, { "text": "For this trick I'll need my\nmagic wand of dereferencing.", "start": 3846.47, "duration": 3.56 }, { "text": "-Your magic wand of dereferencing?", "start": 3850.03, "duration": 2.77 }, { "text": "Uh, that's great.", "start": 3852.8, "duration": 2.38 }, { "text": "-This is what the code looks like.", "start": 3855.18, "duration": 1.78 }, { "text": "I'll just set up the number and--", "start": 3856.96, "duration": 1.555 }, { "text": "[POP]", "start": 3858.515, "duration": 0.571 }, { "text": "-Hey, look, there it goes.", "start": 3859.586, "duration": 2.434 }, { "text": "So doing a dereference on x follows\nthe arrow to access its pointee.", "start": 3862.02, "duration": 4.84 }, { "text": "In this case, to store 42 in there.", "start": 3866.86, "duration": 2.17 }, { "text": "Hey, try using it to store the number\n13 through the other pointer, y.", "start": 3869.03, "duration": 4.44 }, { "text": "-OK, I'll just go over here to\ny and get the number 13 set up,", "start": 3873.47, "duration": 5.7 }, { "text": "and then take the wand of\ndereferencing and just--", "start": 3879.17, "duration": 3.746 }, { "text": "[BUZZER]", "start": 3882.916, "duration": 0.499 }, { "text": "Oh!", "start": 3883.415, "duration": 0.825 }, { "text": "-Oh, hey, that didn't work.", "start": 3884.24, "duration": 2.71 }, { "text": "Say, Binky, I don't think the\ndereferencing y is a good idea,", "start": 3886.95, "duration": 3.64 }, { "text": "because, you know, setting up\nthe pointee is a separate step,", "start": 3890.59, "duration": 3.25 }, { "text": "and I don't think we ever did it.", "start": 3893.84, "duration": 1.77 }, { "text": "-Oh, good point.", "start": 3895.61, "duration": 1.89 }, { "text": "-Yeah, we allocated the pointer y, but\nwe never set it to point to a pointee.", "start": 3897.5, "duration": 4.61 }, { "text": "-Hm, very observant.", "start": 3902.11, "duration": 2.02 }, { "text": "-Hey, you're looking good there, Binky.", "start": 3904.13, "duration": 2.19 }, { "text": "Can you fix it so that y points\nto the same pointee as x?", "start": 3906.32, "duration": 2.96 }, { "text": "-Sure.", "start": 3909.28, "duration": 0.57 }, { "text": "I'll use my magic wand\nof pointer assignment.", "start": 3909.85, "duration": 2.81 }, { "text": "-Is that going to be a\nproblem, like before?", "start": 3912.66, "duration": 2.19 }, { "text": "-No, this doesn't touch the pointees.", "start": 3914.85, "duration": 1.8 }, { "text": "It just changes one pointer to\npoint to the same thing as another.", "start": 3916.65, "duration": 3.57 }, { "text": "-Oh, I see.", "start": 3920.22, "duration": 1.09 }, { "text": "Now y points to the same place as x.", "start": 3921.31, "duration": 2.77 }, { "text": "So wait, now y is fixed.", "start": 3924.08, "duration": 1.78 }, { "text": "It has a pointee, so you can try\nthe wand of dereferencing again", "start": 3925.86, "duration": 3.03 }, { "text": "to send the 13 over.", "start": 3928.89, "duration": 1.788 }, { "text": "-Oh, OK, here goes,", "start": 3930.678, "duration": 3.402 }, { "text": "-Hey, look at that.", "start": 3934.08, "duration": 1.12 }, { "text": "Now dereferencing works on y.", "start": 3935.2, "duration": 1.73 }, { "text": "And because the pointers are sharing\nthat one pointee, they both see the 13.", "start": 3936.93, "duration": 4.11 }, { "text": "-Yeah, sharing, whatever.", "start": 3941.04, "duration": 1.8 }, { "text": "So are we going to switch places now?", "start": 3942.84, "duration": 1.81 }, { "text": "-Oh look, we're out of time.", "start": 3944.65, "duration": 1.96 }, { "text": "-But--", "start": 3946.61, "duration": 0.84 }, { "text": "-Just remember the three pointer rules.", "start": 3947.45, "duration": 1.94 }, { "text": "Number one, the basic structure\nis that you have a pointer", "start": 3949.39, "duration": 3.05 }, { "text": "and it points over to a pointee.", "start": 3952.44, "duration": 2.11 }, { "text": "But the pointer and\npointee are separate,", "start": 3954.55, "duration": 1.96 }, { "text": "and the common error\nis to set up a pointer,", "start": 3956.51, "duration": 1.99 }, { "text": "but to forget to give it a pointee.", "start": 3958.5, "duration": 2.38 }, { "text": "Number two, pointer dereferencing\nstarts at the pointer", "start": 3960.88, "duration": 3.13 }, { "text": "and follows its arrow over\nto access its pointee.", "start": 3964.01, "duration": 2.9 }, { "text": "As we all know, this only works if there\nis a pointee, which kind of gets back", "start": 3966.91, "duration": 3.88 }, { "text": "to rule number one.", "start": 3970.79, "duration": 1.34 }, { "text": "Number three, pointer\nassignment takes one pointer", "start": 3972.13, "duration": 2.78 }, { "text": "and changes it to point to the\nsame pointee as another pointer.", "start": 3974.91, "duration": 3.31 }, { "text": "So after the assignment the two\npointers will point to the same pointee.", "start": 3978.22, "duration": 3.57 }, { "text": "Sometimes that's called sharing.", "start": 3981.79, "duration": 1.867 }, { "text": "And that's all there is to it, really.", "start": 3983.657, "duration": 1.583 }, { "text": "Bye bye, now.", "start": 3985.24, "duration": 1.52 }, { "text": "[END PLAYBACK]", "start": 3986.76, "duration": 0.936 }, { "text": "DAVID J. MALAN: So what\nelse can go wrong now", "start": 3987.696, "duration": 1.874 }, { "text": "that we have the ability\nto touch, correctly", "start": 3989.57, "duration": 2.36 }, { "text": "or incorrectly, any\nmemory that we actually", "start": 3991.93, "duration": 2.19 }, { "text": "have access to inside of our program?", "start": 3994.12, "duration": 1.85 }, { "text": "Well, memory leaks are one such problem.", "start": 3995.97, "duration": 2.31 }, { "text": "Now that you have the capability to\nask the operating system for memory", "start": 3998.28, "duration": 3.84 }, { "text": "via the malloc function, you\nhave the ability to accidentally", "start": 4002.12, "duration": 4.19 }, { "text": "not give that memory back.", "start": 4006.31, "duration": 1.72 }, { "text": "In fact, a very common mistake\nin some programming languages", "start": 4008.03, "duration": 3.05 }, { "text": "is to ask the operating\nsystem for a chunk of memory,", "start": 4011.08, "duration": 2.26 }, { "text": "use it, and then never actually free it.", "start": 4013.34, "duration": 2.52 }, { "text": "To give it back so that\nother parts of your program,", "start": 4015.86, "duration": 2.7 }, { "text": "or other programs on the computer,\ncan actually make use of that memory.", "start": 4018.56, "duration": 3.96 }, { "text": "But fortunately there exist tools\nvia which we can detect this,", "start": 4022.52, "duration": 3.26 }, { "text": "and one of them is called Valgrind.", "start": 4025.78, "duration": 1.6 }, { "text": "So this is another debugging\ntool that you'll now", "start": 4027.38, "duration": 2.041 }, { "text": "be able to use once\nyou start dynamically", "start": 4029.421, "duration": 2.679 }, { "text": "allocating memory in your program.", "start": 4032.1, "duration": 1.87 }, { "text": "So up until now you probably\nhave not used malloc,", "start": 4033.97, "duration": 2.44 }, { "text": "and therefore you have\nnot likely actually asked", "start": 4036.41, "duration": 3.2 }, { "text": "the operating system for more\nmemory in this very dynamic way.", "start": 4039.61, "duration": 3.25 }, { "text": "Instead, you have just declared\nan integer or an array,", "start": 4042.86, "duration": 3.36 }, { "text": "and asked the computer for memory\nin other ways, not using malloc.", "start": 4046.22, "duration": 4.52 }, { "text": "But suppose that we have\na program like this.", "start": 4050.74, "duration": 2.26 }, { "text": "In memory dot c I've made some mistakes.", "start": 4053.0, "duration": 3.72 }, { "text": "Deliberately, but mistakes nonetheless.", "start": 4056.72, "duration": 3.614 }, { "text": "And indeed, this draws inspiration from\nthe documentation for this very tool", "start": 4060.334, "duration": 3.166 }, { "text": "to highlight a couple of\nits most useful features.", "start": 4063.5, "duration": 2.46 }, { "text": "So let's look at this program here.", "start": 4065.96, "duration": 1.52 }, { "text": "There's no prototype\nfor this function f,", "start": 4067.48, "duration": 1.77 }, { "text": "just because the example that Valgrind,\nthis tool, gives just puts it up top,", "start": 4069.25, "duration": 3.4 }, { "text": "and that's fine.", "start": 4072.65, "duration": 0.91 }, { "text": "But let's focus on main first.", "start": 4073.56, "duration": 1.69 }, { "text": "Main takes no command line\narguments, per it's mention of void,", "start": 4075.25, "duration": 2.88 }, { "text": "and it returns an int as usual.", "start": 4078.13, "duration": 1.98 }, { "text": "The function f gets called here.", "start": 4080.11, "duration": 2.44 }, { "text": "So f open bracket, close bracket\njust means call the function f,", "start": 4082.55, "duration": 3.38 }, { "text": "but pass it no inputs, and\nthen eventually return 0.", "start": 4085.93, "duration": 3.06 }, { "text": "So let's look at f.", "start": 4088.99, "duration": 0.82 }, { "text": "What happens?", "start": 4089.81, "duration": 1.06 }, { "text": "Well, f takes no inputs,\nproduces no outputs.", "start": 4090.87, "duration": 3.05 }, { "text": "It only apparently has\nthese two lines of code.", "start": 4093.92, "duration": 2.547 }, { "text": "And let's consider what it does.", "start": 4096.467, "duration": 1.333 }, { "text": "It calls malloc, memory allocate,\n10 times the size of int.", "start": 4097.8, "duration": 4.33 }, { "text": "So that just means, hey, malloc,\ngive me enough space for 10 integers.", "start": 4102.13, "duration": 4.599 }, { "text": "Now we know on CS50 IDE that the size\nof an int recall is 4 bytes, or 32 bits.", "start": 4106.729, "duration": 5.25 }, { "text": "So this is like saying, hey,\ncomputer, give me 40 bytes total.", "start": 4111.979, "duration": 4.891 }, { "text": "4 times 10.", "start": 4116.87, "duration": 1.26 }, { "text": "Malloc recall returns the\naddress of that chunk of memory,", "start": 4118.13, "duration": 3.349 }, { "text": "and it stores it in x, which, according\nto the star, is the address of an int.", "start": 4121.479, "duration": 4.09 }, { "text": "So that's all.", "start": 4125.569, "duration": 0.811 }, { "text": "So borrowing some of the same\nideas from the Binky example.", "start": 4126.38, "duration": 2.75 }, { "text": "What is bad about this\nis this next line.", "start": 4129.13, "duration": 4.59 }, { "text": "Why is x[10]=0 a problem?", "start": 4133.72, "duration": 4.522 }, { "text": "Well, the size of this chunk of\nmemory is what, 10 ints worth.", "start": 4138.242, "duration": 3.848 }, { "text": "So 10 times 4, 40 bytes.", "start": 4142.09, "duration": 1.84 }, { "text": "But remember how chunks\nof memory are indexed", "start": 4143.93, "duration": 3.369 }, { "text": "when you use square bracket notation.", "start": 4147.299, "duration": 1.541 }, { "text": "When you use square bracket\nnotation, you better", "start": 4148.84, "duration": 2.0 }, { "text": "start counting at 0, which means\nif we have 10 ints, first one is 0,", "start": 4150.84, "duration": 4.0 }, { "text": "the last one is 9, the\n10th one does not exist.", "start": 4154.84, "duration": 4.33 }, { "text": "And so if I'm saying\nxx[10] gets any value,", "start": 4159.17, "duration": 3.149 }, { "text": "that's like saying go there\nand put the value 0 there,", "start": 4162.319, "duration": 3.281 }, { "text": "but I don't have access\nto that chunk of memory.", "start": 4165.6, "duration": 2.429 }, { "text": "I did not ask the\ncomputer for that memory.", "start": 4168.029, "duration": 2.361 }, { "text": "I asked the computer for all of this\nother memory, only 10 of these bytes.", "start": 4170.39, "duration": 4.639 }, { "text": "So this feels problematic.", "start": 4175.029, "duration": 1.551 }, { "text": "Moreover, I have asked the operating\nsystem for memory via malloc,", "start": 4176.58, "duration": 3.699 }, { "text": "hey, give me 40 bytes.", "start": 4180.279, "duration": 1.33 }, { "text": "I never handed it back.", "start": 4181.609, "duration": 2.091 }, { "text": "Thereby introducing what we\nwould call a memory leak.", "start": 4183.7, "duration": 4.22 }, { "text": "Now, as an aside, once your program\nquits, all of the memory it's allocated", "start": 4187.92, "duration": 3.63 }, { "text": "is actually automatically\ngiven back, but the problem is", "start": 4191.55, "duration": 2.72 }, { "text": "that, for long running programs,\nthings like your browsers,", "start": 4194.27, "duration": 2.54 }, { "text": "or word processing\nprograms, or any number--", "start": 4196.81, "duration": 2.521 }, { "text": "Skype-- or any number of other\nprograms that you might run locally", "start": 4199.331, "duration": 2.749 }, { "text": "on your Mac or PC, if those bigger\nprograms have memory leaks, such", "start": 4202.08, "duration": 3.94 }, { "text": "that they keep asking Mac\nOS or Windows for memory", "start": 4206.02, "duration": 2.66 }, { "text": "and never actually\nremember to give it back,", "start": 4208.68, "duration": 2.71 }, { "text": "you might experience what you might\nhave in the past of your Mac or PC", "start": 4211.39, "duration": 4.17 }, { "text": "really starting to slow\ndown and kind of crawl.", "start": 4215.56, "duration": 4.14 }, { "text": "And that can be for any number\nof reasons, but one of them", "start": 4219.7, "duration": 2.98 }, { "text": "is if your program or programs inside\nof them have some form of memory leak.", "start": 4222.68, "duration": 4.21 }, { "text": "Not your fault. It's the programmer's\nfault. But memory leaks, nonetheless.", "start": 4226.89, "duration": 4.36 }, { "text": "So what is Valgrind good for here?", "start": 4231.25, "duration": 2.64 }, { "text": "Well, it turns out, that in Valgrind\nyou can run the command like here.", "start": 4233.89, "duration": 5.04 }, { "text": "Valgrind.", "start": 4238.93, "duration": 0.74 }, { "text": "Leak check equals full.", "start": 4239.67, "duration": 1.71 }, { "text": "Dot slash.", "start": 4241.38, "duration": 0.584 }, { "text": "And then the name of your program.", "start": 4241.964, "duration": 1.416 }, { "text": "And so let me do that.", "start": 4243.38, "duration": 1.05 }, { "text": "Let me go ahead back into CS50 IDE\nand let me go ahead and make memory.", "start": 4244.43, "duration": 5.31 }, { "text": "Compiles OK, so it doesn't appear\nto be syntactically flawed.", "start": 4249.74, "duration": 2.74 }, { "text": "Dot slash memory.", "start": 4252.48, "duration": 2.09 }, { "text": "Nothing bad seems to happen.", "start": 4254.57, "duration": 1.63 }, { "text": "So that's all fine and good.", "start": 4256.2, "duration": 1.57 }, { "text": "And you might think that, OK my\nprogram works, it's time to submit,", "start": 4257.77, "duration": 4.19 }, { "text": "all is well, but not necessarily.", "start": 4261.96, "duration": 2.27 }, { "text": "Let me go ahead and run Valgrind.", "start": 4264.23, "duration": 2.19 }, { "text": "And let me actually go\nahead-- let me go up here", "start": 4266.42, "duration": 2.314 }, { "text": "and not create a new\nfile, but a new terminal.", "start": 4268.734, "duration": 1.916 }, { "text": "Just so that we have\na much bigger window.", "start": 4270.65, "duration": 1.75 }, { "text": "And let me go ahead into this directory\nand run Valgrind, dot slash memory.", "start": 4272.4, "duration": 4.94 }, { "text": "Without any command line\narguments besides that.", "start": 4277.34, "duration": 2.65 }, { "text": "And hit Enter.", "start": 4279.99, "duration": 0.91 }, { "text": "And the downside of\nValgrind, frankly, is", "start": 4280.9, "duration": 2.36 }, { "text": "that it's just-- output is atrocious.", "start": 4283.26, "duration": 1.9 }, { "text": "Like it's just not easy to read.", "start": 4285.16, "duration": 1.68 }, { "text": "But you start to learn\nto notice patterns in it.", "start": 4286.84, "duration": 2.45 }, { "text": "So I've learned to notice when I make\nmistakes in programs that invalid right", "start": 4289.29, "duration": 3.92 }, { "text": "of size 4, not a good thing.", "start": 4293.21, "duration": 1.74 }, { "text": "That's indeed a problem of some sort.", "start": 4294.95, "duration": 2.12 }, { "text": "But what's nice about\nValgrind is that it tells me", "start": 4297.07, "duration": 2.83 }, { "text": "that the source of this invalid\nright, whatever that means,", "start": 4299.9, "duration": 3.54 }, { "text": "starts in memory.c line 13 and really\nrears its head in line 8 of memory.c.", "start": 4303.44, "duration": 7.92 }, { "text": "Well, what are those lines?", "start": 4311.36, "duration": 1.19 }, { "text": "Let's go to line 13.", "start": 4312.55, "duration": 2.12 }, { "text": "Ah, that's my call to F. And line 8.", "start": 4314.67, "duration": 2.6 }, { "text": "Oh, that's my use of x bracket 10.", "start": 4317.27, "duration": 3.79 }, { "text": "So why is Valgrind telling\nme invalid right of size 4?", "start": 4321.06, "duration": 3.89 }, { "text": "Well, size 4 sounds familiar.", "start": 4324.95, "duration": 1.32 }, { "text": "That's the size of an int.", "start": 4326.27, "duration": 1.71 }, { "text": "Invalid just means bad.", "start": 4327.98, "duration": 1.53 }, { "text": "Right means change something,\nlike mutate a value.", "start": 4329.51, "duration": 3.36 }, { "text": "So invalid right of size\n4 maybe just means this.", "start": 4332.87, "duration": 4.38 }, { "text": "I am in an invalid way\nchanging 4 bytes of memory", "start": 4337.25, "duration": 4.45 }, { "text": "by storing 0 from the right hand side\ninto x bracket 10 on the left hand", "start": 4341.7, "duration": 4.29 }, { "text": "side because, again, that x\nbracket 10 is an 11th byte, rather,", "start": 4345.99, "duration": 5.26 }, { "text": "an 11th int, that I never\nask the operating system for.", "start": 4351.25, "duration": 4.07 }, { "text": "I only asked it for 10, not 11.", "start": 4355.32, "duration": 2.98 }, { "text": "And so Valgrind is telling me, somewhat\ncryptically, that I've screwed up.", "start": 4358.3, "duration": 4.69 }, { "text": "On line 8 ultimately I've screwed\nup by touching 4 bytes of memory", "start": 4362.99, "duration": 4.23 }, { "text": "that I shouldn't have.", "start": 4367.22, "duration": 1.63 }, { "text": "Meanwhile, a little\nmore worrisome is this.", "start": 4368.85, "duration": 2.74 }, { "text": "Address such and such as\nzero bytes after a block", "start": 4371.59, "duration": 2.63 }, { "text": "of size 4 allots, da-da da-da.", "start": 4374.22, "duration": 1.94 }, { "text": "Oh, not that.", "start": 4376.16, "duration": 0.78 }, { "text": "Let's focus on-- here we go.", "start": 4376.94, "duration": 3.103 }, { "text": "Uh oh.", "start": 4380.043, "duration": 0.747 }, { "text": "Leak summary.", "start": 4380.79, "duration": 1.06 }, { "text": "Definitely lost.", "start": 4381.85, "duration": 1.12 }, { "text": "Oh, my god.", "start": 4382.97, "duration": 0.71 }, { "text": "Like I have lost 40 bytes\nof the computer's memory.", "start": 4383.68, "duration": 2.71 }, { "text": "Now what does that mean?", "start": 4386.39, "duration": 1.08 }, { "text": "This simply means that\nValgrind has noticed", "start": 4387.47, "duration": 2.01 }, { "text": "that I must have called malloc\nor some similar function,", "start": 4389.48, "duration": 2.44 }, { "text": "asked for memory, 40 bytes'\nworth, and never gave it back.", "start": 4391.92, "duration": 3.77 }, { "text": "Well, we haven't introduced the\nway of giving memory back so let", "start": 4395.69, "duration": 2.89 }, { "text": "me at least address that now.", "start": 4398.58, "duration": 1.63 }, { "text": "But let me take this advice.", "start": 4400.21, "duration": 1.45 }, { "text": "Rerun with leak check full to\nsee details of leaked memory.", "start": 4401.66, "duration": 2.999 }, { "text": "So let me go ahead and do that.", "start": 4404.659, "duration": 1.291 }, { "text": "Let me go ahead and copy\nthis so I can rerun Valgrind.", "start": 4405.95, "duration": 4.323 }, { "text": "Whoops.", "start": 4410.273, "duration": 0.967 }, { "text": "Valgrind.", "start": 4411.24, "duration": 0.857 }, { "text": "With that command line argument.", "start": 4412.097, "duration": 1.333 }, { "text": "Dot slash memory.", "start": 4413.43, "duration": 1.58 }, { "text": "And now I'll see a little\nmore detail sometimes.", "start": 4415.01, "duration": 2.87 }, { "text": "Although, it appears in this\ncase it's already pretty verbose.", "start": 4417.88, "duration": 3.36 }, { "text": "And so indeed I've still\nlost 40 bytes in one block.", "start": 4421.24, "duration": 3.69 }, { "text": "And up here again is a reiteration\nof that invalid right of size 4.", "start": 4424.93, "duration": 4.01 }, { "text": "So how do I fix this?", "start": 4428.94, "duration": 1.38 }, { "text": "Well, let me go back into my program.", "start": 4430.32, "duration": 1.74 }, { "text": "And you know what?", "start": 4432.06, "duration": 0.76 }, { "text": "The program's still pretty useless,\nbut at least let me fix that mistake.", "start": 4432.82, "duration": 3.91 }, { "text": "And go ahead now and go at x bracket 9.", "start": 4436.73, "duration": 3.43 }, { "text": "Let me remake my program.", "start": 4440.16, "duration": 1.35 }, { "text": "Make memory.", "start": 4441.51, "duration": 1.48 }, { "text": "And let me in my bigger terminal\nwindow do Valgrind dot slash memory.", "start": 4442.99, "duration": 6.9 }, { "text": "And now, oh, damn it.", "start": 4449.89, "duration": 1.12 }, { "text": "Still have definitely lost\n40 bytes in one blocks,", "start": 4451.01, "duration": 2.53 }, { "text": "but notice the output\nis much shorter now.", "start": 4453.54, "duration": 3.19 }, { "text": "I don't have a mistake that\nwas higher up here before.", "start": 4456.73, "duration": 2.57 }, { "text": "So I fixed one problem.", "start": 4459.3, "duration": 1.2 }, { "text": "Let me go ahead and\nfix that other problem.", "start": 4460.5, "duration": 1.86 }, { "text": "After using this memory,\nlet me go ahead and free x.", "start": 4462.36, "duration": 4.2 }, { "text": "So free is the opposite\nof malloc in this case.", "start": 4466.56, "duration": 3.1 }, { "text": "And if I go ahead and recompile\nmake memory and then run it", "start": 4469.66, "duration": 3.44 }, { "text": "in my bigger terminal window, nice.", "start": 4473.1, "duration": 3.4 }, { "text": "All heap blocks were freed.", "start": 4476.5, "duration": 2.3 }, { "text": "No leaks are possible.", "start": 4478.8, "duration": 2.35 }, { "text": "Now what does that mean?", "start": 4481.15, "duration": 1.19 }, { "text": "Well, it turns out, when we\nlook at our computer's memory,", "start": 4482.34, "duration": 4.124 }, { "text": "there is indeed that stack at the\nbottom that we talked about earlier.", "start": 4486.464, "duration": 2.916 }, { "text": "And the stack is where frames of\nmemory go, slices of memory go,", "start": 4489.38, "duration": 3.96 }, { "text": "that are used when\nfunctions are being called.", "start": 4493.34, "duration": 2.53 }, { "text": "And they get layered\non top of each other", "start": 4495.87, "duration": 2.18 }, { "text": "and get un-layered only\nonce the function's return.", "start": 4498.05, "duration": 2.64 }, { "text": "The heap, meanwhile, is a chunk of\nmemory above the stack, by design.", "start": 4500.69, "duration": 3.77 }, { "text": "At least as we've drawn it here.", "start": 4504.46, "duration": 1.59 }, { "text": "And in the heap you have just\na pool of available memory", "start": 4506.05, "duration": 4.44 }, { "text": "that you can draw from at any point.", "start": 4510.49, "duration": 2.76 }, { "text": "And what's powerful\nabout the heap is that,", "start": 4513.25, "duration": 2.37 }, { "text": "whereas the stack keeps growing\nup and then as functions return", "start": 4515.62, "duration": 3.28 }, { "text": "it grows back down thereby\nthrowing away, effectively", "start": 4518.9, "duration": 3.67 }, { "text": "losing track of, any memory\nthat we had on the stack,", "start": 4522.57, "duration": 2.69 }, { "text": "like x's and y's and a's and\nb's and temp variables still,", "start": 4525.26, "duration": 3.7 }, { "text": "the heap does not experience\nthat kind of disposal of memory.", "start": 4528.96, "duration": 5.44 }, { "text": "If you ask for memory via malloc, malloc\ngives you a chunk of memory over here,", "start": 4534.4, "duration": 5.38 }, { "text": "or over here, or over here, up above\nthe stack inside of the so-called heap.", "start": 4539.78, "duration": 4.92 }, { "text": "And even when your function\nreturns, whether it's f, or swap,", "start": 4544.7, "duration": 3.77 }, { "text": "or something else, that use of\nmemory persists permanently.", "start": 4548.47, "duration": 5.49 }, { "text": "Only once you call free do\nyou give that chunk of memory", "start": 4553.96, "duration": 3.11 }, { "text": "back to the operating\nsystem and allow it to reuse", "start": 4557.07, "duration": 2.68 }, { "text": "that memory for something else.", "start": 4559.75, "duration": 2.48 }, { "text": "But unfortunately this\npicture kind of suggests", "start": 4562.23, "duration": 3.23 }, { "text": "a problem and bad design in some sense.", "start": 4565.46, "duration": 2.62 }, { "text": "Although, given a finite\namount of memory, eventually", "start": 4568.08, "duration": 2.37 }, { "text": "something's got to bump\nup against something else", "start": 4570.45, "duration": 2.18 }, { "text": "if you want to keep asking\nfor and using memory.", "start": 4572.63, "duration": 2.38 }, { "text": "Those arrows kind of suggest\nthat the stack and the heap", "start": 4575.01, "duration": 3.55 }, { "text": "are really destined to collide\nwith each other if what happens?", "start": 4578.56, "duration": 3.14 }, { "text": "Well, if the stack grows too large.", "start": 4581.7, "duration": 1.907 }, { "text": "In other words, if you keep calling\nfunction after function after function", "start": 4583.607, "duration": 3.083 }, { "text": "after function and never return.", "start": 4586.69, "duration": 1.43 }, { "text": "And maybe this is accidental.", "start": 4588.12, "duration": 1.27 }, { "text": "Maybe you're using\nrecursion, per last time,", "start": 4589.39, "duration": 2.19 }, { "text": "and you accidentally\ndon't have a base case", "start": 4591.58, "duration": 2.285 }, { "text": "so you just keep calling functions.", "start": 4593.865, "duration": 1.795 }, { "text": "So much so that you\nrun out of stack space.", "start": 4595.66, "duration": 3.41 }, { "text": "Or maybe you call malloc so many\ntimes that the heap keeps growing down", "start": 4599.07, "duration": 4.53 }, { "text": "and down and hits the stack, meanwhile.", "start": 4603.6, "duration": 2.2 }, { "text": "Either of those problems can persist.", "start": 4605.8, "duration": 1.85 }, { "text": "And you might recognize\nsome familiar terms.", "start": 4607.65, "duration": 2.23 }, { "text": "The first of those scenarios\ndescribe stack overflow.", "start": 4609.88, "duration": 2.601 }, { "text": "So if any of you have ever discovered\nthe website, Stack Overflow,", "start": 4612.481, "duration": 2.749 }, { "text": "which is a wonderful\nplace to go for advice", "start": 4615.23, "duration": 3.632 }, { "text": "and tips and tricks with\nprogramming, more generally.", "start": 4618.862, "duration": 2.208 }, { "text": "stackoverflow.com draws its name\nfrom exactly that programming", "start": 4621.07, "duration": 4.87 }, { "text": "risk and problem.", "start": 4625.94, "duration": 1.5 }, { "text": "Heap overflow is similar in spirit.", "start": 4627.44, "duration": 1.922 }, { "text": "It's just the opposite.", "start": 4629.362, "duration": 0.958 }, { "text": "When the heap grows down\ntoo far, as we've drawn it.", "start": 4630.32, "duration": 2.76 }, { "text": "And they're both\nexamples, more generally,", "start": 4633.08, "duration": 1.99 }, { "text": "of what you might call buffer overflows.", "start": 4635.07, "duration": 2.25 }, { "text": "A buffer is a chunk of space that's\ntypically finite in some form", "start": 4637.32, "duration": 4.31 }, { "text": "and you can eventually overflow it\nby trying to use more memory then", "start": 4641.63, "duration": 3.47 }, { "text": "you should have.", "start": 4645.1, "duration": 0.69 }, { "text": "And a buffer overflows\ntypically actually relates", "start": 4645.79, "duration": 2.53 }, { "text": "to arrays and chunks of memory.", "start": 4648.32, "duration": 2.17 }, { "text": "And, in fact, a great example\nfrom Wikipedia is this one here.", "start": 4650.49, "duration": 3.17 }, { "text": "The code is a little cryptic too because\nit doesn't really do anything useful,", "start": 4653.66, "duration": 4.04 }, { "text": "but it's fundamentally\nflawed in the following way.", "start": 4657.7, "duration": 2.35 }, { "text": "Let's consider exactly what happens\nwhen you have a stack overflow.", "start": 4660.05, "duration": 4.04 }, { "text": "It turns out that this is a bad\nenough problem that you can actually", "start": 4664.09, "duration": 3.39 }, { "text": "lose control over your computer itself.", "start": 4667.48, "duration": 2.77 }, { "text": "It can be hacked into if\nan adversary, a bad guy,", "start": 4670.25, "duration": 3.21 }, { "text": "has the ability somehow to inject his\nor her own adversarial code, code that", "start": 4673.46, "duration": 5.41 }, { "text": "deletes your files, or encrypt your\nfiles, or does something malicious,", "start": 4678.87, "duration": 3.79 }, { "text": "into your program because of a\nmemory related mistake you've made.", "start": 4682.66, "duration": 3.13 }, { "text": "Let's consider this example.", "start": 4685.79, "duration": 1.3 }, { "text": "Here's the main function.", "start": 4687.09, "duration": 1.15 }, { "text": "In this particular example, it takes\ncommand line arguments arg-c and arg-v", "start": 4688.24, "duration": 4.07 }, { "text": "and just apparently calls this function\nfoo, passing in the second word", "start": 4692.31, "duration": 5.0 }, { "text": "that the user typed in.", "start": 4697.31, "duration": 1.02 }, { "text": "So the first command line argument\nthat the user ran at the prompt.", "start": 4698.33, "duration": 2.83 }, { "text": "And it just blindly does this.", "start": 4701.16, "duration": 1.25 }, { "text": "It doesn't check arg-c, it doesn't check\nfor null, it doesn't check anything.", "start": 4702.41, "duration": 3.53 }, { "text": "It just blindly passes\nin arg-v bracket 1.", "start": 4705.94, "duration": 2.714 }, { "text": "And that's generally bad practice.", "start": 4708.654, "duration": 1.416 }, { "text": "Right?", "start": 4710.07, "duration": 0.5 }, { "text": "If you're not error checking, bad things\nare likely going to happen eventually.", "start": 4710.57, "duration": 3.57 }, { "text": "Well, what does foo do?", "start": 4714.14, "duration": 1.76 }, { "text": "Foo takes a char star as inputs.", "start": 4715.9, "duration": 2.99 }, { "text": "Now, a week ago, we would\nhave said string bar,", "start": 4718.89, "duration": 2.794 }, { "text": "but we don't need to\nhide that detail anymore.", "start": 4721.684, "duration": 1.916 }, { "text": "Bar is just the address of a character.", "start": 4723.6, "duration": 3.34 }, { "text": "It's a string.", "start": 4726.94, "duration": 1.19 }, { "text": "Foo returns nothing, but\nhas two lines of code.", "start": 4728.13, "duration": 2.53 }, { "text": "First, it declares an array\ncalled c-- just because.", "start": 4730.66, "duration": 3.88 }, { "text": "This is kind of a\ncontrived example online,", "start": 4734.54, "duration": 3.28 }, { "text": "but the pictures will\ntell it all-- of size 12.", "start": 4737.82, "duration": 3.126 }, { "text": "So this is saying, hey, computer.", "start": 4740.946, "duration": 1.374 }, { "text": "Give me an array of 12 characters.", "start": 4742.32, "duration": 2.16 }, { "text": "Or give me a chunk of memory of size\n12 that I plan to put characters into.", "start": 4744.48, "duration": 4.265 }, { "text": "And then, mem copy, you\nmight not have seen before,", "start": 4748.745, "duration": 2.125 }, { "text": "but it essentially does this it\ncopies into this chunk of memory", "start": 4750.87, "duration": 4.8 }, { "text": "whatever is at that chunk of\nmemory up to this many bytes.", "start": 4755.67, "duration": 4.59 }, { "text": "So mem copy, as the names\nsuggests, copies memory from here", "start": 4760.26, "duration": 3.58 }, { "text": "to here a total number\nof this many bytes.", "start": 4763.84, "duration": 4.01 }, { "text": "So why is this worrisome?", "start": 4767.85, "duration": 2.13 }, { "text": "Well, if for whatever reason\nyou have only allocated 12 bytes", "start": 4769.98, "duration": 4.12 }, { "text": "and the user has typed\na word at the prompt", "start": 4774.1, "duration": 2.82 }, { "text": "when running your program\nthat is more than 12 bytes,", "start": 4776.92, "duration": 3.78 }, { "text": "it would seem that the user\nis able to touch memory", "start": 4780.7, "duration": 6.7 }, { "text": "that you the programmer never intended.", "start": 4787.4, "duration": 1.81 }, { "text": "Why?", "start": 4789.21, "duration": 0.78 }, { "text": "Well, your use of mem copy is\nchecking the length of something,", "start": 4789.99, "duration": 3.64 }, { "text": "but it's checking the length of bar.", "start": 4793.63, "duration": 1.92 }, { "text": "So if the user types in not 12\ncharacters, but 20 characters,", "start": 4795.55, "duration": 3.71 }, { "text": "strleng of bar is going to be 20.", "start": 4799.26, "duration": 2.66 }, { "text": "The user's input is 20 characters long.", "start": 4801.92, "duration": 1.78 }, { "text": "So your code is saying copy\n20 characters from bar into c.", "start": 4803.7, "duration": 5.5 }, { "text": "Unfortunately, c is only of size 12 and\nso that's 8 bytes you end up copying", "start": 4809.2, "duration": 5.36 }, { "text": "that you shouldn't.", "start": 4814.56, "duration": 1.35 }, { "text": "So if you were given a chunk of\nmemory here that's only of size 8", "start": 4815.91, "duration": 3.8 }, { "text": "and you're just 12, you're touching\n8 more bytes to the right of it,", "start": 4819.71, "duration": 3.93 }, { "text": "so to speak, that don't\nactually belong to you.", "start": 4823.64, "duration": 3.26 }, { "text": "Now, at best, nothing's going to happen.", "start": 4826.9, "duration": 2.056 }, { "text": "Your computer, your program\nis not going to notice.", "start": 4828.956, "duration": 2.124 }, { "text": "Things are just going to hum along\nand all is going to seem fine.", "start": 4831.08, "duration": 3.39 }, { "text": "Much like my memory\nprogram a moment ago.", "start": 4834.47, "duration": 1.94 }, { "text": "Seemed fine, but Valgrind\nthought otherwise.", "start": 4836.41, "duration": 2.79 }, { "text": "In this case, really\nbad things can happen.", "start": 4839.2, "duration": 3.32 }, { "text": "Because if what the human adversary,\nthe bad guy, has typed at the prompt", "start": 4842.52, "duration": 4.85 }, { "text": "is not just some word like foo,\nor bar, or David, or Zamyla,", "start": 4847.37, "duration": 3.98 }, { "text": "or any number of sort\nof innocuous strings,", "start": 4851.35, "duration": 3.1 }, { "text": "but is actually the ASCII equivalent,\nso the textual equivalent,", "start": 4854.45, "duration": 3.93 }, { "text": "of some malicious code-- so if he or\nshe actually somehow typed at the prompt", "start": 4858.38, "duration": 4.87 }, { "text": "a pattern of characters that really\nrepresent a pattern of bits that", "start": 4863.25, "duration": 3.9 }, { "text": "do something bad, like\ndelete all your files,", "start": 4867.15, "duration": 2.17 }, { "text": "or spam everyone in your address\nbook, or any number of things.", "start": 4869.32, "duration": 3.61 }, { "text": "If they provide a string that's\nlonger than 12 characters,", "start": 4872.93, "duration": 3.74 }, { "text": "the overflow is going to\nend up somewhere in memory.", "start": 4876.67, "duration": 3.25 }, { "text": "And where is that?", "start": 4879.92, "duration": 1.17 }, { "text": "Well, let's zoom in a little lower level\nto what your computer's memory looks", "start": 4881.09, "duration": 4.52 }, { "text": "like on the stack.", "start": 4885.61, "duration": 1.78 }, { "text": "So it's a little more detail.", "start": 4887.39, "duration": 1.52 }, { "text": "So let's absorb this for just a moment.", "start": 4888.91, "duration": 1.96 }, { "text": "Think of this now as the bottom of\nthe stack, parent routines stack.", "start": 4890.87, "duration": 4.67 }, { "text": "So you can think of this as main, or\nfoo, or some function on the stack.", "start": 4895.54, "duration": 3.64 }, { "text": "It turns out-- and I didn't\nmention this earlier--", "start": 4899.18, "duration": 2.19 }, { "text": "that besides there being arguments\nor parameters on the stack", "start": 4901.37, "duration": 3.12 }, { "text": "and local variables on the stack,\nit turns out that the computer also", "start": 4904.49, "duration": 4.21 }, { "text": "uses the stack just to kind of store\na reminder for itself of the address", "start": 4908.7, "duration": 5.68 }, { "text": "in memory of the\nfunction that called it.", "start": 4914.38, "duration": 3.23 }, { "text": "So if this picture represents foo, the\nfunction right now, who called foo?", "start": 4917.61, "duration": 5.82 }, { "text": "Main called foo and so what foo\ndoes is, on its own stack, it", "start": 4923.43, "duration": 4.36 }, { "text": "just kind of jots down a note to\nitself here in pink, where it says,", "start": 4927.79, "duration": 3.0 }, { "text": "return address, when I'm done,\nreturn to this address of main.", "start": 4930.79, "duration": 3.24 }, { "text": "The computer does that for itself.", "start": 4934.03, "duration": 2.56 }, { "text": "So when foo is done executing,\npreviously, I just wipe the screen", "start": 4936.59, "duration": 3.27 }, { "text": "and remove foo.", "start": 4939.86, "duration": 1.04 }, { "text": "But how does the program know where\nto go back to after foo is done?", "start": 4940.9, "duration": 3.424 }, { "text": "It's because it put a little\nbread crumb for itself.", "start": 4944.324, "duration": 2.166 }, { "text": "The address of main, itself.", "start": 4946.49, "duration": 2.112 }, { "text": "Not the address of variables, the\naddress of the function itself,", "start": 4948.602, "duration": 2.708 }, { "text": "which, recall, is in the text\nsegment of your computer's memory.", "start": 4951.31, "duration": 3.46 }, { "text": "But never mind that for now.", "start": 4954.77, "duration": 1.95 }, { "text": "So now, let's look at what the stack\nframe for foo is actually like.", "start": 4956.72, "duration": 5.441 }, { "text": "Here is that array of size 12.", "start": 4962.161, "duration": 1.919 }, { "text": "And it's drawn as a rectangle.", "start": 4964.08, "duration": 1.42 }, { "text": "If we made it wider, we could just\ndo 0, 1, 2, all the way through C 11,", "start": 4965.5, "duration": 4.1 }, { "text": "but the author of this graphic has just\ndrawn things a little more square like.", "start": 4969.6, "duration": 4.63 }, { "text": "So this is the first byte in C and\nthis is the last byte in C, 0 and 11,", "start": 4974.23, "duration": 4.52 }, { "text": "respectively.", "start": 4978.75, "duration": 0.85 }, { "text": "And then bar, recall, is the\nonly argument to the foo function", "start": 4979.6, "duration": 3.08 }, { "text": "and that, as I did mention before,\nbelongs on the stack as well.", "start": 4982.68, "duration": 4.44 }, { "text": "But here's the problem.", "start": 4987.12, "duration": 1.33 }, { "text": "And this is by design.", "start": 4988.45, "duration": 1.44 }, { "text": "It appears that whoever\ndesigned the stack,", "start": 4989.89, "duration": 4.0 }, { "text": "has it growing upwards,\nas in this picture.", "start": 4993.89, "duration": 2.66 }, { "text": "And that's how I described it earlier.", "start": 4996.55, "duration": 1.9 }, { "text": "So the stack itself grows up.", "start": 4998.45, "duration": 1.84 }, { "text": "Frames go up and up and up and up.", "start": 5000.29, "duration": 2.39 }, { "text": "But within a frame, notice what happens.", "start": 5002.68, "duration": 2.38 }, { "text": "Within a frame, if you've got a buffer,\nan array of memory, a chunk of memory,", "start": 5005.06, "duration": 4.83 }, { "text": "and you write to it, you start\nwriting to that memory, by design,", "start": 5009.89, "duration": 4.54 }, { "text": "at the top left corner, so to\nspeak, to the right, and then down,", "start": 5014.43, "duration": 4.29 }, { "text": "and then down.", "start": 5018.72, "duration": 1.1 }, { "text": "So top left to bottom\nright, so to speak.", "start": 5019.82, "duration": 2.76 }, { "text": "Pictorially.", "start": 5022.58, "duration": 1.5 }, { "text": "This was not necessarily a good\nthing, at least in this case.", "start": 5024.08, "duration": 3.92 }, { "text": "Because if the stack is growing up\nand this is the top of foo's frame", "start": 5028.0, "duration": 4.47 }, { "text": "and it's use of the array\nc is by design going down,", "start": 5032.47, "duration": 3.91 }, { "text": "that's all fine if the user only\nprovides a string of length 12 or less,", "start": 5036.38, "duration": 4.38 }, { "text": "12 or shorter, but in this case if\nthe adversary types in a 20 character", "start": 5040.76, "duration": 5.04 }, { "text": "string or 200 character\nstring, the computer", "start": 5045.8, "duration": 2.65 }, { "text": "is not just going to stop there.", "start": 5048.45, "duration": 1.44 }, { "text": "Unless you wrote code\nwith an if condition", "start": 5049.89, "duration": 1.86 }, { "text": "to check the length that the user\nis trying to pass in and protect", "start": 5051.75, "duration": 3.92 }, { "text": "against that, it's\ngoing to overwrite all", "start": 5055.67, "duration": 2.3 }, { "text": "of these bytes, all of these bytes,\nwhatever this is, all of these bytes,", "start": 5057.97, "duration": 4.51 }, { "text": "and this is what's worrisome.", "start": 5062.48, "duration": 1.64 }, { "text": "If you have a really smart\nadversary up against you,", "start": 5064.12, "duration": 3.46 }, { "text": "trying to hack into your system, and he\nor she is smart enough or lucky enough", "start": 5067.58, "duration": 4.58 }, { "text": "to figure out how to inject a pattern\nof bits into your program in this way,", "start": 5072.16, "duration": 5.61 }, { "text": "such that he or she overwrites\nthis return address,", "start": 5077.77, "duration": 3.45 }, { "text": "a really clever adversary\ncan trick your program", "start": 5081.22, "duration": 3.46 }, { "text": "into returning to, not the\nfunction that called foo, main,", "start": 5084.68, "duration": 4.99 }, { "text": "but returning to the address\nof the adversary's own function", "start": 5089.67, "duration": 4.32 }, { "text": "that he or she has somehow injected\nby way of typing input at the prompt,", "start": 5093.99, "duration": 5.5 }, { "text": "in this case.", "start": 5099.49, "duration": 1.11 }, { "text": "In other words, if the user types in\njust, hello, h-e-l-l-o, backslash, 0,", "start": 5100.6, "duration": 4.62 }, { "text": "all is fine and good and it\nfits perfectly in that frame.", "start": 5105.22, "duration": 3.05 }, { "text": "But because we didn't\nhave good error checking", "start": 5108.27, "duration": 1.94 }, { "text": "on the length of the\nmemory we're copying,", "start": 5110.21, "duration": 2.79 }, { "text": "suppose the adversary includes a\nwhole bunch of adversarial code, which", "start": 5113.0, "duration": 3.49 }, { "text": "the author here abstracted away as a,\na, a, adversary, or attack, attack,", "start": 5116.49, "duration": 4.14 }, { "text": "attack, what might actually happen here?", "start": 5120.63, "duration": 2.79 }, { "text": "Well, if that adversary gets\nlucky enough or is smart enough,", "start": 5123.42, "duration": 4.28 }, { "text": "he or she might be able to\noverwrite these 4 bytes here", "start": 5127.7, "duration": 5.16 }, { "text": "with the address, brilliantly,\nof his or her own attack code,", "start": 5132.86, "duration": 5.87 }, { "text": "which is the bits or the characters\nthat he or she typed at the prompt.", "start": 5138.73, "duration": 3.51 }, { "text": "So again, a lot of this is luck.", "start": 5142.24, "duration": 1.46 }, { "text": "Or a lot of this is trial\nand error sometimes,", "start": 5143.7, "duration": 2.08 }, { "text": "to attack a program in\nthis way, but if you", "start": 5145.78, "duration": 2.28 }, { "text": "can trick the computer into\njumping to an address that", "start": 5148.06, "duration": 3.82 }, { "text": "happens to point at data you\ninjected into the program,", "start": 5151.88, "duration": 4.39 }, { "text": "you have effectively taken\nover that user's program,", "start": 5156.27, "duration": 2.83 }, { "text": "tricking the program into running\nany and all code that in this case", "start": 5159.1, "duration": 5.04 }, { "text": "you provided by a arg-v. So if you've\never heard of a server getting hacked,", "start": 5164.14, "duration": 3.59 }, { "text": "or in the future you will hear\nof a server getting hacked,", "start": 5167.73, "duration": 2.48 }, { "text": "or a program getting compromised,\ncould mean any number of things,", "start": 5170.21, "duration": 3.1 }, { "text": "but one of them, and an\nall too common approach,", "start": 5173.31, "duration": 2.44 }, { "text": "is that the programmer who wrote\nthe software used a buffer,", "start": 5175.75, "duration": 4.68 }, { "text": "used an array, a chunk\nof memory, and he or she", "start": 5180.43, "duration": 2.24 }, { "text": "did not check the boundaries\nof that array sufficiently.", "start": 5182.67, "duration": 4.56 }, { "text": "Did not make sure that\nhis or her own code", "start": 5187.23, "duration": 1.79 }, { "text": "was not going too far as was\nthis particular example here.", "start": 5189.02, "duration": 5.64 }, { "text": "So now that we have this\npower of going anywhere", "start": 5194.66, "duration": 2.77 }, { "text": "we want in memory, it's incredibly easy\nto accidentally or maliciously use,", "start": 5197.43, "duration": 5.36 }, { "text": "but that's it.", "start": 5202.79, "duration": 1.03 }, { "text": "That boils down to just\nthis basic understanding", "start": 5203.82, "duration": 2.01 }, { "text": "of what's going on underneath\nthe hood and what you can now", "start": 5205.83, "duration": 2.458 }, { "text": "do with the computer's memory.", "start": 5208.288, "duration": 2.322 }, { "text": "And now, let's transition to the\nsecond of our real world demands.", "start": 5213.57, "duration": 4.81 }, { "text": "Now that we have the ability\nto talk about memory addresses", "start": 5218.38, "duration": 3.1 }, { "text": "and actually do things at\nthis lower level, turns out,", "start": 5221.48, "duration": 3.44 }, { "text": "we can start solving some really\ninteresting problems, among them", "start": 5224.92, "duration": 3.0 }, { "text": "related to images and forensics, the art\nand science of recovering information.", "start": 5227.92, "duration": 5.69 }, { "text": "In fact, you might recall from various\nTV shows or movies, that it's all", "start": 5233.61, "duration": 3.28 }, { "text": "too common for the good guys\nin the movies and the TV", "start": 5236.89, "duration": 3.382 }, { "text": "shows to be looking over\nthe shoulder of a tech", "start": 5240.272, "duration": 1.958 }, { "text": "and seeing some footage of a burglary\nor some other crime that's committed", "start": 5242.23, "duration": 3.38 }, { "text": "and say, hey, can you clean that up?", "start": 5245.61, "duration": 1.74 }, { "text": "Or can you enhance that?", "start": 5247.35, "duration": 1.77 }, { "text": "And, indeed, let's take a look at\none such clip from the real world", "start": 5249.12, "duration": 3.56 }, { "text": "and see how it relates\nto the actual world", "start": 5252.68, "duration": 2.41 }, { "text": "and how we can leverage some of this\nnew found savvy with memory addresses", "start": 5255.09, "duration": 3.17 }, { "text": "and computing to actually solve\nsome of these problems for real.", "start": 5258.26, "duration": 4.804 }, { "text": "[VIDEO PLAYBACK]", "start": 5263.064, "duration": 0.666 }, { "text": "-He's lying.", "start": 5263.73, "duration": 1.85 }, { "text": "-About what?", "start": 5265.58, "duration": 0.66 }, { "text": "-I don't know.", "start": 5266.24, "duration": 2.892 }, { "text": "-So what do we know?", "start": 5269.132, "duration": 2.742 }, { "text": "-That at 9:15 Ray\nSantoya was at the ATM.", "start": 5271.874, "duration": 3.269 }, { "text": "-OK, so the question is,\nwhat was he doing at 9:16?", "start": 5275.143, "duration": 3.967 }, { "text": "-Shooting the 9 mm at something.", "start": 5279.11, "duration": 2.81 }, { "text": "Maybe he saw the sniper.", "start": 5281.92, "duration": 1.63 }, { "text": "-Or was working with him.", "start": 5283.55, "duration": 1.772 }, { "text": "-Wait.", "start": 5285.322, "duration": 1.5 }, { "text": "Go back one.", "start": 5286.822, "duration": 0.972 }, { "text": "-What do you see?", "start": 5287.794, "duration": 0.972 }, { "text": "-Bring his face up.", "start": 5296.056, "duration": 1.004 }, { "text": "Full screen.", "start": 5297.06, "duration": 3.1 }, { "text": "-His glasses.", "start": 5300.16, "duration": 0.66 }, { "text": "-There's a reflection.", "start": 5300.82, "duration": 1.5 }, { "text": "-That's the Nuevita's baseball team.", "start": 5312.56, "duration": 1.51 }, { "text": "That's their logo.", "start": 5314.07, "duration": 1.38 }, { "text": "-And he's talking to\nwhoever's wearing that jacket.", "start": 5315.45, "duration": 2.217 }, { "text": "[END PLAYBACK]", "start": 5317.667, "duration": 0.583 }, { "text": "DAVID J. MALAN: OK.", "start": 5318.25, "duration": 0.8 }, { "text": "So this is just nonsense.", "start": 5319.05, "duration": 1.26 }, { "text": "Like, you can't just\nclean up an image, or look", "start": 5320.31, "duration": 2.11 }, { "text": "at something that's very\npixelated, so to speak,", "start": 5322.42, "duration": 2.0 }, { "text": "where pixels are dots on the\nscreen, and just kind of zoom in", "start": 5324.42, "duration": 2.67 }, { "text": "and clean that up and enhance it.", "start": 5327.09, "duration": 1.944 }, { "text": "And, indeed, that's kind\nof this running joke.", "start": 5329.034, "duration": 1.916 }, { "text": "Enhance doesn't really\ngenerally mean enhance when you", "start": 5330.95, "duration": 3.41 }, { "text": "have a finite amount of information.", "start": 5334.36, "duration": 1.58 }, { "text": "For instance, here is a\nfinite amount of information.", "start": 5335.94, "duration": 2.58 }, { "text": "A photograph of Zamyla that\nlooks to be of very high quality.", "start": 5338.52, "duration": 3.53 }, { "text": "You can see lots of detail.", "start": 5342.05, "duration": 1.57 }, { "text": "And so you might think that we can\nmaybe see the reflection of someone", "start": 5343.62, "duration": 3.47 }, { "text": "in her eye if we really just\nzoom in and enhance, but no.", "start": 5347.09, "duration": 4.06 }, { "text": "This is what you see, and it's actually\nkind of creepy at this distance.", "start": 5351.15, "duration": 3.94 }, { "text": "This is what you see when you zoom\nin on that exact same photo of Zamyla", "start": 5355.09, "duration": 5.06 }, { "text": "to find that glint in her eye of the\nbad guy, or the badge on his shoulder,", "start": 5360.15, "duration": 3.4 }, { "text": "or whatever it was.", "start": 5363.55, "duration": 1.27 }, { "text": "There is only a finite\namount of information here.", "start": 5364.82, "duration": 2.57 }, { "text": "There is literally only a finite\nnumber of rows and columns", "start": 5367.39, "duration": 3.39 }, { "text": "and therefore really big pixels\nfrom which to glean information.", "start": 5370.78, "duration": 3.5 }, { "text": "Now, there do exist algorithms and\nsoftware that can smooth this out.", "start": 5374.28, "duration": 4.26 }, { "text": "You can enhance the image in the\nsense that you can kind of maybe", "start": 5378.54, "duration": 3.38 }, { "text": "tweak the color so that it looks a\nlittle more gradual, the color changes,", "start": 5381.92, "duration": 3.91 }, { "text": "and a little less jarring, but\nyou can't just put pixels there,", "start": 5385.83, "duration": 3.91 }, { "text": "put bits there that are not\nexistent in the first place.", "start": 5389.74, "duration": 4.4 }, { "text": "And so, with this, invites the\nquestion well, then what is an image,", "start": 5394.14, "duration": 4.64 }, { "text": "how do you represent it, and what\ncan you actually glean from it", "start": 5398.78, "duration": 3.17 }, { "text": "and do from it?", "start": 5401.95, "duration": 0.84 }, { "text": "Well, let's consider in the simplest\ncase what an image might be.", "start": 5402.79, "duration": 3.1 }, { "text": "Here is an image of a\nwonderfully simple smiley face.", "start": 5405.89, "duration": 2.88 }, { "text": "It's black and white.", "start": 5408.77, "duration": 1.3 }, { "text": "And if it's black and white, frankly, we\ncan get away with just ones and zeros.", "start": 5410.07, "duration": 4.05 }, { "text": "I might arbitrarily say that the number\n1 shall represent the color white", "start": 5414.12, "duration": 5.22 }, { "text": "and the number 0 shall represent\nthe color black and, as such,", "start": 5419.34, "duration": 3.42 }, { "text": "if I just have a bunch of patterns\nof 8 bits, ones and zeros,", "start": 5422.76, "duration": 3.92 }, { "text": "I can effectively think\nof them as a grid.", "start": 5426.68, "duration": 2.34 }, { "text": "Rows and columns.", "start": 5429.02, "duration": 1.36 }, { "text": "And if I see a 0 pixel, assume it's\na black dot, if I see a 1 pixel,", "start": 5430.38, "duration": 4.51 }, { "text": "it's a white dot.", "start": 5434.89, "duration": 2.03 }, { "text": "And, as such, I can\ncreate a bitmapped image.", "start": 5436.92, "duration": 3.63 }, { "text": "Bitmap, it's like a map, x's and\ny's, of bits, bitmapped image.", "start": 5440.55, "duration": 4.47 }, { "text": "There is the simplest, perhaps,\nsmiley face we can draw.", "start": 5445.02, "duration": 2.994 }, { "text": "Now it's not all that interesting.", "start": 5448.014, "duration": 1.416 }, { "text": "It would be a pretty useless\nphotograph if you only", "start": 5449.43, "duration": 2.98 }, { "text": "had dots of that level of detail.", "start": 5452.41, "duration": 1.599 }, { "text": "And indeed we saw, when you\nreally zoom in on Zumilah's eyes,", "start": 5454.009, "duration": 2.541 }, { "text": "you have really big grids.", "start": 5456.55, "duration": 1.084 }, { "text": "And those were colorful, but\nthere isn't that much information.", "start": 5457.634, "duration": 2.624 }, { "text": "We need more resolution.", "start": 5460.258, "duration": 1.082 }, { "text": "We need more dots,\nmore pixels, more bits.", "start": 5461.34, "duration": 2.82 }, { "text": "And so the general case is to use\nnot something as simple as this,", "start": 5464.16, "duration": 3.3 }, { "text": "but a more standard format like JPEG.", "start": 5467.46, "duration": 1.97 }, { "text": "JPEG is what you see on Facebook\nand on your cameras, most likely.", "start": 5469.43, "duration": 3.27 }, { "text": "It's an image format that\nphotographs are commonly stored in,", "start": 5472.7, "duration": 4.27 }, { "text": "because it stores millions of\ncolors-- potentially for photographs--", "start": 5476.97, "duration": 3.7 }, { "text": "and it also allows you to compress them.", "start": 5480.67, "duration": 3.19 }, { "text": "You can compress JPEGs in\nsuch a way that you throw away", "start": 5483.86, "duration": 3.09 }, { "text": "some of the 0's and 1's,\nthereby degrading the image.", "start": 5486.95, "duration": 2.92 }, { "text": "So it's a little blurrier,\nor a little splotchy,", "start": 5489.87, "duration": 2.76 }, { "text": "but at least it's much, much smaller\nand can be emailed or texted or stored", "start": 5492.63, "duration": 3.83 }, { "text": "with far less space involved.", "start": 5496.46, "duration": 2.17 }, { "text": "And it turns out, if we\nput on our forensics hats", "start": 5498.63, "duration": 3.41 }, { "text": "now-- it turns out that JPEGs\nall share something in common.", "start": 5502.04, "duration": 5.76 }, { "text": "Any of the JPEGs you see on the\ninternet or have on your hard drive", "start": 5507.8, "duration": 3.42 }, { "text": "or on your phone start with the\nsame three bytes, typically.", "start": 5511.22, "duration": 4.99 }, { "text": "Specifically, the first three bytes\nare the numbers 255, 216, 255.", "start": 5516.21, "duration": 7.44 }, { "text": "Why that?", "start": 5523.65, "duration": 0.52 }, { "text": "It's just the standard that was\nadopted some time ago for JPEGs.", "start": 5524.17, "duration": 4.15 }, { "text": "It's like a magic number, if you\nwill, at the start of the image.", "start": 5528.32, "duration": 3.18 }, { "text": "Now it turns out these\nare decimal numbers.", "start": 5531.5, "duration": 1.83 }, { "text": "And frankly, in the world\nof computing, and really", "start": 5533.33, "duration": 2.97 }, { "text": "the world of file inputs and\noutput-- file management--", "start": 5536.3, "duration": 3.147 }, { "text": "we don't really talk in decimal.", "start": 5539.447, "duration": 1.333 }, { "text": "It's just not commonly done.", "start": 5540.78, "duration": 1.29 }, { "text": "More commonly done is to talk,\nnot in decimal, not in binary", "start": 5542.07, "duration": 3.58 }, { "text": "as we did in week 0, but in hexadecimal.", "start": 5545.65, "duration": 2.91 }, { "text": "So it looks a little spooky\nat first, but it really", "start": 5548.56, "duration": 3.71 }, { "text": "is an alphabet of 16 characters\ninstead of 10, instead of 2.", "start": 5552.27, "duration": 4.904 }, { "text": "And you start counting at 0, as the real\nworld does-- 0, 1, 2, 3, 4, 5, 6, 7, 8,", "start": 5557.174, "duration": 3.333 }, { "text": "9-- but there is no 10, because\nthese are single digits.", "start": 5560.507, "duration": 4.643 }, { "text": "So after 9 comes A, B, C,\nD, E. So with hexadecimal,", "start": 5565.15, "duration": 5.13 }, { "text": "you can effectively count\nfrom 0 to 15 so long", "start": 5570.28, "duration": 3.95 }, { "text": "as you borrow some\nletters of the alphabet", "start": 5574.23, "duration": 1.85 }, { "text": "to do 10, 11, 12, 13, 14, and 15.", "start": 5576.08, "duration": 3.83 }, { "text": "So let's map that onto\nthis pattern of numbers", "start": 5579.91, "duration": 3.56 }, { "text": "that demarcates the start of a JPEG.", "start": 5583.47, "duration": 2.03 }, { "text": "These numbers, if we do them\nout in binary, equals this.", "start": 5585.5, "duration": 3.5 }, { "text": "And we can leave this\nas an exercise at home", "start": 5589.0, "duration": 2.4 }, { "text": "if you'd like, but trust\nme, per week 0, that these", "start": 5591.4, "duration": 4.36 }, { "text": "are the patterns of bits that are\nequivalent to 255, 216, and 255.", "start": 5595.76, "duration": 4.74 }, { "text": "Those are the light bulbs you\nwould need to turn on and off.", "start": 5600.5, "duration": 2.99 }, { "text": "Meanwhile, we can add some\nspace there-- so before, after--", "start": 5603.49, "duration": 5.42 }, { "text": "and I did this deliberately.", "start": 5608.91, "duration": 1.78 }, { "text": "Because it turns out that hexadecimal,\ninsofar as you can count as high as 15", "start": 5610.69, "duration": 6.91 }, { "text": "for 16 total characters-- it turns\nout that hexadecimal is really useful.", "start": 5617.6, "duration": 3.97 }, { "text": "Because with four bits,\nfour 0's and 1's, you", "start": 5621.57, "duration": 3.71 }, { "text": "can count from zero\nall the way up to what?", "start": 5625.28, "duration": 4.5 }, { "text": "All the way up to 15, which is perfect,\nbecause if you have four 1 bits,", "start": 5629.78, "duration": 4.78 }, { "text": "that's 15.", "start": 5634.56, "duration": 0.77 }, { "text": "If you have four 0 bits, that's 0.", "start": 5635.33, "duration": 2.05 }, { "text": "And it turns out that patterns\nof 4 bits-- so half of a byte--", "start": 5637.38, "duration": 4.22 }, { "text": "map perfectly to hexadecimal characters.", "start": 5641.6, "duration": 2.47 }, { "text": "You can express chunks of 4 bits with\nhexadecimal characters perfectly.", "start": 5644.07, "duration": 4.11 }, { "text": "As such, it turns out that 15,\nagain, is F. So we have FF.", "start": 5648.18, "duration": 5.16 }, { "text": "And it turns out that\n1101 and 1000 from 216.", "start": 5653.34, "duration": 5.419 }, { "text": "If you take those 8 bits\nand just spread them out", "start": 5658.759, "duration": 2.041 }, { "text": "slightly into two groups of four, is the\nletter D in hexadecimal and the number", "start": 5660.8, "duration": 4.21 }, { "text": "8 in hexadecimal.", "start": 5665.01, "duration": 1.24 }, { "text": "And then we have another couple\nof 15's-- two sets of four 1 bits.", "start": 5666.25, "duration": 4.41 }, { "text": "So that's FF.", "start": 5670.66, "duration": 1.18 }, { "text": "Which is to say that the first\nthree bytes at the start of any JPEG", "start": 5671.84, "duration": 5.23 }, { "text": "happen to be FF D8 FF.", "start": 5677.07, "duration": 2.81 }, { "text": "And it's human convention to prefix\nhexadecimal digits for clarity", "start": 5679.88, "duration": 4.03 }, { "text": "in the real world with just 0x.", "start": 5683.91, "duration": 1.76 }, { "text": "It means nothing fundamentally\nother than, hey world, here comes", "start": 5685.67, "duration": 3.17 }, { "text": "a hexadecimal number, so that the\nworld just knows what it's looking at", "start": 5688.84, "duration": 3.83 }, { "text": "and doesn't confuse it for some\nother numeric or base system.", "start": 5692.67, "duration": 3.2 }, { "text": "So 0x just means here comes a\nseries of hexadecimal digits.", "start": 5695.87, "duration": 3.94 }, { "text": "So that is what is at\nthe beginning of a JPEG.", "start": 5699.81, "duration": 2.88 }, { "text": "And in fact, one of the problems\nwe put before you in CS50 each year", "start": 5702.69, "duration": 4.33 }, { "text": "is to recover some JPEGs.", "start": 5707.02, "duration": 2.81 }, { "text": "In fact, it's all too common,\nunfortunately, to lose photographs", "start": 5709.83, "duration": 4.01 }, { "text": "from a memory card or to accidentally\ndelete files from your computer.", "start": 5713.84, "duration": 3.21 }, { "text": "And typically, you might\nfreak out or be worried", "start": 5717.05, "duration": 2.4 }, { "text": "that, damn, I didn't mean to delete\nthose photos or delete those files.", "start": 5719.45, "duration": 3.86 }, { "text": "And sometimes they are gone for good.", "start": 5723.31, "duration": 2.12 }, { "text": "But it turns out that\ncomputers and even phones", "start": 5725.43, "duration": 2.34 }, { "text": "don't necessarily delete\nphotos right away.", "start": 5727.77, "duration": 1.96 }, { "text": "They forget where they are, but\nthey don't outright delete them.", "start": 5729.73, "duration": 2.915 }, { "text": "They don't change the 0's to 1's and\nthe 1's to 0's unless you start taking", "start": 5732.645, "duration": 3.125 }, { "text": "more and more photos immediately.", "start": 5735.77, "duration": 1.64 }, { "text": "But if you have accidentally formatted\nor corrupted your memory card", "start": 5737.41, "duration": 3.56 }, { "text": "in a phone or a computer, such that\nyou think you've now lost your photos,", "start": 5740.97, "duration": 4.42 }, { "text": "what if you did this?", "start": 5745.39, "duration": 1.14 }, { "text": "What if you wrote software that iterated\nfrom the start of your memory card", "start": 5746.53, "duration": 4.49 }, { "text": "or your phone all the way to the end of\nyour phone or your memory card's memory", "start": 5751.02, "duration": 4.55 }, { "text": "looking for this pattern of\nbits, this pattern of numbers", "start": 5755.57, "duration": 3.9 }, { "text": "or hexadecimal digits, this pattern\nof 24 bits-- 8 plus 8 plus 8?", "start": 5759.47, "duration": 4.94 }, { "text": "With high probability,\nif, upon iterating", "start": 5764.41, "duration": 2.66 }, { "text": "through your phone's memory or\ncamera's memory, you see FF D8 and FF,", "start": 5767.07, "duration": 5.13 }, { "text": "you can perhaps infer that the following\nmegabytes-- 2 megabytes, however", "start": 5772.2, "duration": 5.16 }, { "text": "big the photograph is-- is\nthe entirety of a photo.", "start": 5777.36, "duration": 3.64 }, { "text": "And if you just grab those\nbits and save a copy,", "start": 5781.0, "duration": 2.0 }, { "text": "maybe you can recover\nall or some of the photo.", "start": 5783.0, "duration": 2.35 }, { "text": "And indeed, one of the challenges\nyou'll soon see in our problems", "start": 5785.35, "duration": 2.904 }, { "text": "that focus on forensics is\ngoing to be exactly this.", "start": 5788.254, "duration": 2.166 }, { "text": "We will accidentally format a memory\ncard from a camera or a phone.", "start": 5790.42, "duration": 4.47 }, { "text": "We will give you a forensic image\nof that memory card, so to speak,", "start": 5794.89, "duration": 3.41 }, { "text": "a perfect copy of all of the 0's and\n1's therein, give it to you as a file,", "start": 5798.3, "duration": 4.67 }, { "text": "and challenge you with writing software\nthat iterates over that file's bytes,", "start": 5802.97, "duration": 3.79 }, { "text": "from the 0th byte all the way to the end\nof the file, looking for this pattern.", "start": 5806.76, "duration": 3.665 }, { "text": "And any time you see this\npattern, you will need to say,", "start": 5810.425, "duration": 3.675 }, { "text": "this is probably a JPEG.", "start": 5814.1, "duration": 1.51 }, { "text": "Let me start copying these bytes into\na separate file name-- something.jpg.", "start": 5815.61, "duration": 5.29 }, { "text": "And indeed, if you get the code right,\nsuddenly out of this forensic image", "start": 5820.9, "duration": 4.74 }, { "text": "pops any number of photographs\nthat we thought were lost forever.", "start": 5825.64, "duration": 5.48 }, { "text": "Bitmap is another type of\nimage file-- older, perhaps,", "start": 5831.12, "duration": 4.89 }, { "text": "than JPEG and simpler--\ntruly a bitmapped image", "start": 5836.01, "duration": 2.87 }, { "text": "with slightly less fancy\nalgorithms for compression.", "start": 5838.88, "duration": 2.82 }, { "text": "But if you remember this\nworld, this is perhaps", "start": 5841.7, "duration": 3.56 }, { "text": "a perfect example of a bitmap image\nfrom Windows XP back in the day.", "start": 5845.26, "duration": 4.45 }, { "text": "The wallpaper or desktop image that many\nof you might have had on your computers", "start": 5849.71, "duration": 3.69 }, { "text": "looked like this.", "start": 5853.4, "duration": 0.87 }, { "text": "As an aside, if you go poking\naround I think on Wikipedia,", "start": 5854.27, "duration": 2.5 }, { "text": "someone wonderfully went\nto this same spot, found it", "start": 5856.77, "duration": 2.73 }, { "text": "where the artist had taken\nthis photograph for Microsoft,", "start": 5859.5, "duration": 3.156 }, { "text": "and it now looks like this.", "start": 5862.656, "duration": 1.124 }, { "text": "So it's not quite the\nbeautiful place it once was,", "start": 5863.78, "duration": 3.61 }, { "text": "but that, indeed, is the same hill.", "start": 5867.39, "duration": 1.494 }, { "text": "And this is really just\nan opportunity to talk", "start": 5868.884, "duration": 1.916 }, { "text": "about this picture,\nwhich is what's really", "start": 5870.8, "duration": 2.6 }, { "text": "underneath the hood of that\nbeautiful grassy meadow.", "start": 5873.4, "duration": 3.6 }, { "text": "It's this kind of file header.", "start": 5877.0, "duration": 2.47 }, { "text": "We very quickly can make nature\nseem very boring quickly with this.", "start": 5879.47, "duration": 3.34 }, { "text": "But this is a very complicated,\nconvoluted looking way", "start": 5882.81, "duration": 3.33 }, { "text": "of just formalizing\nwhat we mean by a file.", "start": 5886.14, "duration": 4.22 }, { "text": "Like, what is a file?", "start": 5890.36, "duration": 1.09 }, { "text": "A file is just a bunch of bits, 0's and\n1's, stored somewhere on your laptop,", "start": 5891.45, "duration": 3.87 }, { "text": "or desktop, or phone, or wherever.", "start": 5895.32, "duration": 2.367 }, { "text": "But what does it mean\nto be a Microsoft Word", "start": 5897.687, "duration": 1.833 }, { "text": "file versus a JPEG versus\na bitmap versus an Excel", "start": 5899.52, "duration": 6.0 }, { "text": "file versus any number\nof other file formats", "start": 5905.52, "duration": 2.25 }, { "text": "that you might have on your computer?", "start": 5907.77, "duration": 1.79 }, { "text": "MPEG4, or MP3, or AAC, or any number\nof media formats as well-- well really,", "start": 5909.56, "duration": 5.63 }, { "text": "what it means to be a file, is to be\nfilled with 0's and 1's-- that's true--", "start": 5915.19, "duration": 5.14 }, { "text": "but to follow a certain pattern of 0's\nand 1's such that the first several", "start": 5920.33, "duration": 5.08 }, { "text": "bits or first many bits of a\nfile typically follow a pattern.", "start": 5925.41, "duration": 4.57 }, { "text": "And indeed, JPEGs have\na pretty simple pattern.", "start": 5929.98, "duration": 2.13 }, { "text": "Those first three bytes,\nthose first 24 bits,", "start": 5932.11, "duration": 2.14 }, { "text": "follow a certain pattern\nof 255, 216, 255.", "start": 5934.25, "duration": 3.86 }, { "text": "And that demarcates the start of a JPEG.", "start": 5938.11, "duration": 1.889 }, { "text": "And there's some more\ncomplexity therein, too.", "start": 5939.999, "duration": 1.916 }, { "text": "In a bitmap, this looks\noverwhelming at first glance,", "start": 5941.915, "duration": 3.135 }, { "text": "but this is how the world\ndecided to standardize", "start": 5945.05, "duration": 2.47 }, { "text": "what's inside of the first\nfew bytes of a bitmap", "start": 5947.52, "duration": 3.13 }, { "text": "file like that grassy\nmeadow from Windows XP.", "start": 5950.65, "duration": 2.66 }, { "text": "And we'll come back to this in more\ndetail and the problem set before long.", "start": 5953.31, "duration": 3.69 }, { "text": "But you'll notice some identifiers\nhere that might jump out at you.", "start": 5957.0, "duration": 3.39 }, { "text": "So width, height, size, compression--\nso there are some key words in there", "start": 5960.39, "duration": 5.549 }, { "text": "that might make some intuitive sense.", "start": 5965.939, "duration": 1.541 }, { "text": "And indeed, somewhere inside of a\nbitmap image, like that grassy meadow,", "start": 5967.48, "duration": 3.96 }, { "text": "is information stored, like\nwhat is the width of that image?", "start": 5971.44, "duration": 2.5 }, { "text": "What is the height of that image?", "start": 5973.94, "duration": 1.374 }, { "text": "Is it compressed, and how?", "start": 5975.314, "duration": 1.116 }, { "text": "And then more interestingly,\nat the bottom of this picture", "start": 5976.43, "duration": 3.64 }, { "text": "is the beginning of what bitmap calls\nRGB triples-- Red, Green, Blue triples.", "start": 5980.07, "duration": 5.99 }, { "text": "It turns out with\npatterns of three bytes,", "start": 5986.06, "duration": 5.42 }, { "text": "you can represent, top to bottom,\nleft to right, essentially, the color", "start": 5991.48, "duration": 4.26 }, { "text": "of every one of the dots in an image.", "start": 5995.74, "duration": 2.92 }, { "text": "So when we enhanced Zamyla's image, and\nyou literally saw those really big dots", "start": 5998.66, "duration": 3.68 }, { "text": "or pixels.", "start": 6002.34, "duration": 0.61 }, { "text": "Each of those dots or pixels\nhas 24 bits representing", "start": 6002.95, "duration": 4.1 }, { "text": "its color-- how much red,\nhow much green, how much blue", "start": 6007.05, "duration": 2.54 }, { "text": "per our discussion of colors in week 0.", "start": 6009.59, "duration": 2.7 }, { "text": "And so that's what most\nof a bitmap file is here.", "start": 6012.29, "duration": 2.43 }, { "text": "The dot dot dot just means these\ncan go on for quite some time.", "start": 6014.72, "duration": 3.362 }, { "text": "What color is this dot?", "start": 6018.082, "duration": 0.958 }, { "text": "What color is this dot?", "start": 6019.04, "duration": 0.958 }, { "text": "What color is this dot?", "start": 6019.998, "duration": 0.972 }, { "text": "All of that is standardized\ninside of the file.", "start": 6020.97, "duration": 3.42 }, { "text": "And it turns out in C, we can express\nand represent that kind of structure", "start": 6024.39, "duration": 5.78 }, { "text": "using one last keyword for this week.", "start": 6030.17, "duration": 2.62 }, { "text": "In C, there is also a keyword\ncalled struct-- structure--", "start": 6032.79, "duration": 5.12 }, { "text": "that allows you to create\nan actual structure", "start": 6037.91, "duration": 2.64 }, { "text": "inside of which you store information.", "start": 6040.55, "duration": 2.02 }, { "text": "For instance, the programming\nlanguage C does not", "start": 6042.57, "duration": 2.73 }, { "text": "come with a data type\nthat represents students.", "start": 6045.3, "duration": 2.14 }, { "text": "It comes with ints, and chars,\nand floats-- heck, it doesn't even", "start": 6047.44, "duration": 2.98 }, { "text": "come with string.", "start": 6050.42, "duration": 1.08 }, { "text": "It certainly doesn't come with student.", "start": 6051.5, "duration": 2.0 }, { "text": "But I would generally\nthink of a student as being", "start": 6053.5, "duration": 2.647 }, { "text": "the combination of a few things.", "start": 6056.147, "duration": 1.333 }, { "text": "And let's keep it simple.", "start": 6057.48, "duration": 1.041 }, { "text": "A student is a combination of the\nstudent's name and their dorm.", "start": 6058.521, "duration": 3.759 }, { "text": "And bunches of other detail, but for\nnow, let's just focus on name and dorm.", "start": 6062.28, "duration": 4.1 }, { "text": "Now the syntax is a\nlittle new for us here,", "start": 6066.38, "duration": 1.81 }, { "text": "but all this is saying is, hey,\nC, define a type for me that", "start": 6068.19, "duration": 5.64 }, { "text": "is a structure containing two\nfields inside of it, name and dorm,", "start": 6073.83, "duration": 4.64 }, { "text": "and call this new type students.", "start": 6078.47, "duration": 1.984 }, { "text": "So it's a little more complicated\nthan our typedef for a string,", "start": 6080.454, "duration": 2.666 }, { "text": "because a string was just\na synonym for char star.", "start": 6083.12, "duration": 2.55 }, { "text": "This case, a student, is a synonym\nfor this container of things.", "start": 6085.67, "duration": 4.77 }, { "text": "It's like encapsulating two\npieces of data-- name and dorm.", "start": 6090.44, "duration": 4.04 }, { "text": "But this is really useful as follows.", "start": 6094.48, "duration": 3.5 }, { "text": "With this kind of C code, can\nwe now do things like this?", "start": 6097.98, "duration": 4.83 }, { "text": "Let me go ahead and open up structs.h.", "start": 6102.81, "duration": 3.501 }, { "text": "And you'll see a little\nheader file I whipped up", "start": 6106.311, "duration": 1.999 }, { "text": "here that contains very\nlittle information,", "start": 6108.31, "duration": 3.27 }, { "text": "just that same typedef\nfor struct, but I've also", "start": 6111.58, "duration": 4.0 }, { "text": "included CS50.h in my own header file.", "start": 6115.58, "duration": 3.02 }, { "text": "Why?", "start": 6118.6, "duration": 0.552 }, { "text": "Well, just for today's\npurposes, I wanted", "start": 6119.152, "duration": 1.708 }, { "text": "to relax our discussion of char star\nfor a moment and talk about string.", "start": 6120.86, "duration": 4.68 }, { "text": "But technically, I could just do this.", "start": 6125.54, "duration": 2.87 }, { "text": "And technically I could do this.", "start": 6128.41, "duration": 1.74 }, { "text": "And then I could get rid of that,\nbecause now this is true C code.", "start": 6130.15, "duration": 3.81 }, { "text": "There is no need for the CS50 library.", "start": 6133.96, "duration": 1.73 }, { "text": "But either is equivalent, and whatever\nyou're more comfortable with for now", "start": 6135.69, "duration": 3.125 }, { "text": "is certainly fine.", "start": 6138.815, "duration": 2.395 }, { "text": "But let me now open up structs-0.c\nand do something with this.", "start": 6141.21, "duration": 5.32 }, { "text": "So in this program, notice\nthat the top of this file, I", "start": 6146.53, "duration": 2.53 }, { "text": "first define a value\ncalled students to be 3.", "start": 6149.06, "duration": 4.47 }, { "text": "This is an example of a constant.", "start": 6153.53, "duration": 1.47 }, { "text": "Sharp define or pound define here\nsays, define, not a variable,", "start": 6155.0, "duration": 5.06 }, { "text": "but a constant-- and the\nconvention in programming", "start": 6160.06, "duration": 2.77 }, { "text": "is to use all capital letters--\ndefine a constant equal to some value.", "start": 6162.83, "duration": 3.37 }, { "text": "This is not a variable\nwhose value can change.", "start": 6166.2, "duration": 2.46 }, { "text": "This is just a keyword,\nSTUDENTS, in all caps,", "start": 6168.66, "duration": 2.69 }, { "text": "that is fixed now on that value 3.", "start": 6171.35, "duration": 2.47 }, { "text": "And it's one way to define a constant\nin C, a value that does not change.", "start": 6173.82, "duration": 3.47 }, { "text": "What am I now going\nto do with this value?", "start": 6177.29, "duration": 1.89 }, { "text": "I first, here, declare a variable called\nstudents containing three students.", "start": 6179.18, "duration": 5.334 }, { "text": "So it's a little confusing that we've\ngot students, STUDENTS, student,", "start": 6184.514, "duration": 2.916 }, { "text": "but they all mean different things.", "start": 6187.43, "duration": 1.458 }, { "text": "students is just going\nby the name of my array.", "start": 6188.888, "duration": 2.552 }, { "text": "STUDENTS in all caps is the value\n3 from that constant up above.", "start": 6191.44, "duration": 4.12 }, { "text": "And then student is the type of this\narray per the header file, structs.h.", "start": 6195.56, "duration": 6.63 }, { "text": "So this is just really a\nsuccinct way of saying,", "start": 6202.19, "duration": 2.33 }, { "text": "hey, computer, give me an array of size\n3 inside of which are three students,", "start": 6204.52, "duration": 6.1 }, { "text": "ultimately.", "start": 6210.62, "duration": 1.07 }, { "text": "Now here's an array that\niterates from i up to three.", "start": 6211.69, "duration": 3.06 }, { "text": "I print out name colon, prompting\nthe user with getstring for a name.", "start": 6214.75, "duration": 4.14 }, { "text": "And then notice this new operator dot.", "start": 6218.89, "duration": 3.6 }, { "text": "Because struct is being used, and\nstudents bracket i is a structure, not", "start": 6222.49, "duration": 6.62 }, { "text": "an int, or a float, or a string itself,\nwe have to go inside of that structure", "start": 6229.11, "duration": 4.42 }, { "text": "to get its field called name.", "start": 6233.53, "duration": 1.79 }, { "text": "So dot name says, here's the\nstructure student-- go inside,", "start": 6235.32, "duration": 3.35 }, { "text": "and get at the name field, and\nput at that name field whatever", "start": 6238.67, "duration": 3.25 }, { "text": "the return value of getstring is.", "start": 6241.92, "duration": 1.549 }, { "text": "And do the same thing for dorm.", "start": 6243.469, "duration": 1.291 }, { "text": "Go into the i-th student's structure,\nand store at the dorm field", "start": 6244.76, "duration": 5.35 }, { "text": "the string that has been typed in next.", "start": 6250.11, "duration": 2.28 }, { "text": "Now let's scroll down a\nlittle lower and take a look", "start": 6252.39, "duration": 2.2 }, { "text": "at what this program proceeds to do.", "start": 6254.59, "duration": 2.25 }, { "text": "It's a very simple loop that sort\nof, just for demonstration's sake,", "start": 6256.84, "duration": 3.64 }, { "text": "says so-and-so is in such-and-such,\nprinting out the student's name", "start": 6260.48, "duration": 4.47 }, { "text": "and dorm respectively.", "start": 6264.95, "duration": 1.66 }, { "text": "In other words, if I go down here\nand compile structs-0, and then", "start": 6266.61, "duration": 3.9 }, { "text": "run structs-0-- I'm going\nto go ahead and type", "start": 6270.51, "duration": 2.56 }, { "text": "in David Mather and Rob and\nKirkland and Zumilah and Courier,", "start": 6273.07, "duration": 8.7 }, { "text": "and you see literally a\nregurgitation of what I wrote.", "start": 6281.77, "duration": 3.04 }, { "text": "But what's key here is the\nsyntax that I've introduced.", "start": 6284.81, "duration": 2.59 }, { "text": "I'm using a struct, and I'm\naccessing its properties or fields", "start": 6287.4, "duration": 3.39 }, { "text": "within by way of this dot operator.", "start": 6290.79, "duration": 2.06 }, { "text": "Just means go inside the\nstruct and get that value.", "start": 6292.85, "duration": 2.56 }, { "text": "But more cool than that is that\nnow that we understand files,", "start": 6295.41, "duration": 5.38 }, { "text": "and now that we have the ability to\nstore arbitrary structures that did not", "start": 6300.79, "duration": 3.51 }, { "text": "come with C, so to speak,\nnotice what else we can do.", "start": 6304.3, "duration": 4.24 }, { "text": "In this version of the program,\nI'm adding one final feature,", "start": 6308.54, "duration": 4.54 }, { "text": "combining today's discussion\nof pointers with this most", "start": 6313.08, "duration": 3.9 }, { "text": "recent discussion of files.", "start": 6316.98, "duration": 2.42 }, { "text": "It turns out, in C-- and\nit's a little inconsistent--", "start": 6319.4, "duration": 2.65 }, { "text": "there is a data type called FILE in all\ncapital letters, for whatever reason.", "start": 6322.05, "duration": 4.544 }, { "text": "In lower case here, I'm just saying\nhey, computer, give me a variable called", "start": 6326.594, "duration": 3.166 }, { "text": "file of type file star.", "start": 6329.76, "duration": 2.22 }, { "text": "That is, give me a\nvariable that will store", "start": 6331.98, "duration": 2.54 }, { "text": "a pointer to a file, the\naddress of a file, if you will.", "start": 6334.52, "duration": 4.04 }, { "text": "fopen is a new function that\nwe've not used before that says,", "start": 6338.56, "duration": 4.04 }, { "text": "hey, browser, open up the\nfile called students.csv", "start": 6342.6, "duration": 3.86 }, { "text": "in write mode. w means write mode.", "start": 6346.46, "duration": 2.41 }, { "text": "So I want to save a file, not open and\nread a file, but write and save a file.", "start": 6348.87, "duration": 4.75 }, { "text": "Sanity check-- is file\nnot equal to null?", "start": 6353.62, "duration": 2.021 }, { "text": "If so, we're going to proceed.", "start": 6355.641, "duration": 1.249 }, { "text": "I just want to make\nsure nothing went wrong.", "start": 6356.89, "duration": 1.42 }, { "text": "And something might go wrong, if maybe\nI don't have permission on the computer", "start": 6358.31, "duration": 3.18 }, { "text": "to create files, maybe the computer's\nout of space or something-- something", "start": 6361.49, "duration": 3.125 }, { "text": "could go wrong and null is returned per\nthe documentation for fopen or its man", "start": 6364.615, "duration": 4.545 }, { "text": "page.", "start": 6369.16, "duration": 1.01 }, { "text": "So here I have a loop.", "start": 6370.17, "duration": 1.22 }, { "text": "For i from 0 to 3-- so do\nthis 3 times-- fprintf.", "start": 6371.39, "duration": 3.99 }, { "text": "So fprintf is identical\nto printf that you've", "start": 6375.38, "duration": 2.63 }, { "text": "come to get to know in recent\nweeks, but fprintf is file printf.", "start": 6378.01, "duration": 4.12 }, { "text": "And it allows you to print strings,\nnot to the screen, but to a file.", "start": 6382.13, "duration": 4.72 }, { "text": "So its first argument\nis the name of the file", "start": 6386.85, "duration": 2.09 }, { "text": "that you've opened that\nyou want to print to.", "start": 6388.94, "duration": 1.95 }, { "text": "Second argument-- and all the others--\nare just the same as printf as always--", "start": 6390.89, "duration": 2.94 }, { "text": "a string, maybe with some\nformat codes, and then some", "start": 6393.83, "duration": 2.24 }, { "text": "values that you want to\nplug in here and here.", "start": 6396.07, "duration": 3.2 }, { "text": "So if you've ever seen a CSV file in the\nreal world-- a Comma Separated Values,", "start": 6399.27, "duration": 5.14 }, { "text": "these are very simple\nMicrosoft Excel files", "start": 6404.41, "duration": 3.47 }, { "text": "or Apple Numbers files\nor Google Spreadsheets", "start": 6407.88, "duration": 2.14 }, { "text": "can export these as well-- they are just\ntext files whereby all of the fields", "start": 6410.02, "duration": 4.19 }, { "text": "are separated by commas.", "start": 6414.21, "duration": 1.9 }, { "text": "And so we now, today, have the ability\nnot only to understand what's going on", "start": 6416.11, "duration": 3.56 }, { "text": "underneath the hood\nmemory-wise, but we now", "start": 6419.67, "duration": 2.11 }, { "text": "have the ability to use the star\noperator-- and therefore pointers more", "start": 6421.78, "duration": 4.2 }, { "text": "generally, which are required\nin order to manipulate files", "start": 6425.98, "duration": 3.39 }, { "text": "with fopen and this file data type-- and\nwith fprintf can I write information?", "start": 6429.37, "duration": 6.38 }, { "text": "Can I print information to those files?", "start": 6435.75, "duration": 2.17 }, { "text": "So for the first time ever,\nI have written a program", "start": 6437.92, "duration": 2.785 }, { "text": "that, when I run in just a moment,\nis going to have the ability", "start": 6440.705, "duration": 2.625 }, { "text": "to save information.", "start": 6443.33, "duration": 1.89 }, { "text": "I have effectively implemented the\nequivalent of the File Save menu", "start": 6445.22, "duration": 2.84 }, { "text": "for the first time.", "start": 6448.06, "duration": 0.97 }, { "text": "Every other program\nwe've written so far just", "start": 6449.03, "duration": 1.875 }, { "text": "throws its information and its\nmemory away, but not this time.", "start": 6450.905, "duration": 3.565 }, { "text": "Let me go ahead and do\nmake structs-1, and then", "start": 6454.47, "duration": 8.21 }, { "text": "dot slash structs-1, and let me go ahead\nand type in David and Mather and Rob", "start": 6462.68, "duration": 7.8 }, { "text": "and Kirkland and Zumilah and Courier.", "start": 6470.48, "duration": 4.5 }, { "text": "Enter.", "start": 6474.98, "duration": 1.08 }, { "text": "Nothing seems to happen.", "start": 6476.06, "duration": 1.62 }, { "text": "But let me go to my file browser in the\nIDE, and notice this-- students.csv.", "start": 6477.68, "duration": 6.25 }, { "text": "If I open that, I have created this\nfile-- David comma Mather, Rob comma", "start": 6483.93, "duration": 5.13 }, { "text": "Kirkland, Zamyla comma Courier.", "start": 6489.06, "duration": 1.63 }, { "text": "And if I downloaded this\nfile, double-clicked it", "start": 6490.69, "duration": 2.54 }, { "text": "as you can now if you'd like,\nand open it on your Mac or PC,", "start": 6493.23, "duration": 3.29 }, { "text": "if you have Numbers installed or Excel\ninstalled or some comparable program.", "start": 6496.52, "duration": 3.66 }, { "text": "That should open that program, because\nit will recognize the .csv extension.", "start": 6500.18, "duration": 3.65 }, { "text": "And it will display all of those\nnames and all of those dorms", "start": 6503.83, "duration": 3.44 }, { "text": "or houses in separate columns.", "start": 6507.27, "duration": 2.39 }, { "text": "Because the world decided some\ntime ago that the format known", "start": 6509.66, "duration": 3.13 }, { "text": "as CSV, Comma Separated Values,\nis just simple text file", "start": 6512.79, "duration": 3.34 }, { "text": "with commas separating values.", "start": 6516.13, "duration": 3.37 }, { "text": "And so we now have the ability\nto express all of this and more.", "start": 6519.5, "duration": 4.65 }, { "text": "So on the horizon is to solve some\nof these same kinds of problems", "start": 6524.15, "duration": 4.25 }, { "text": "and to actually implement for yourself\ncode that writes files and reads files.", "start": 6528.4, "duration": 4.24 }, { "text": "But to solve actual problems\nmotivated from the real world domain,", "start": 6532.64, "duration": 2.75 }, { "text": "to recover information\nthat's been deliberately", "start": 6535.39, "duration": 2.07 }, { "text": "scrambled or obscured,\nto recover information", "start": 6537.46, "duration": 2.43 }, { "text": "that's been accidentally\nor maliciously deleted,", "start": 6539.89, "duration": 2.23 }, { "text": "to recover things, photographs,\nmemories that you actually", "start": 6542.12, "duration": 2.85 }, { "text": "care about-- all with a\nfundamental understanding of what's", "start": 6544.97, "duration": 3.92 }, { "text": "going on inside of your own computer.", "start": 6548.89, "duration": 2.24 }, { "text": "But until then, keep an eye out for\nsuch TV shows and movies as these.", "start": 6551.13, "duration": 4.992 }, { "text": "[VIDEO PLAYBACK]", "start": 6556.122, "duration": 0.666 }, { "text": "-OK.", "start": 6556.788, "duration": 1.482 }, { "text": "Now let's get a good look at you.", "start": 6558.27, "duration": 1.976 }, { "text": "-Hold it.", "start": 6567.162, "duration": 0.988 }, { "text": "Run that back.", "start": 6568.15, "duration": 0.988 }, { "text": "-Wait a minute.", "start": 6569.138, "duration": 0.988 }, { "text": "Go right.", "start": 6570.126, "duration": 1.814 }, { "text": "-There.", "start": 6571.94, "duration": 0.5 }, { "text": "Freeze that.", "start": 6572.44, "duration": 0.5 }, { "text": "-Full screen.", "start": 6572.94, "duration": 0.792 }, { "text": "-OK, freeze that.", "start": 6573.732, "duration": 0.718 }, { "text": "-Tighten up on that, will you?", "start": 6574.45, "duration": 1.25 }, { "text": "-Vector in on that guy\nby the back of you.", "start": 6575.7, "duration": 1.93 }, { "text": "-Zoom in right here on this spot.", "start": 6577.63, "duration": 2.64 }, { "text": "-With the right equipment, the\nimage can be enlarged and sharpened.", "start": 6580.27, "duration": 3.86 }, { "text": "-What's that?", "start": 6584.13, "duration": 0.703 }, { "text": "-It's an enhancement program.", "start": 6584.833, "duration": 1.222 }, { "text": "-Can you clear that up any?", "start": 6586.055, "duration": 1.625 }, { "text": "-I don't know.", "start": 6587.68, "duration": 1.3 }, { "text": "Let's enhance it.", "start": 6588.98, "duration": 1.02 }, { "text": "-Enhance section A6.", "start": 6590.0, "duration": 2.52 }, { "text": "-I enhanced the detail and--", "start": 6592.52, "duration": 1.302 }, { "text": "-I think there's enough to enhance.", "start": 6593.822, "duration": 1.458 }, { "text": "Release it to my screen.", "start": 6595.28, "duration": 0.999 }, { "text": "-Enhance the reflection in her eye.", "start": 6596.279, "duration": 1.872 }, { "text": "-Let's run this through\nvideo enhancement.", "start": 6598.151, "duration": 1.749 }, { "text": "-Edgar, can you enhance this?", "start": 6599.9, "duration": 1.208 }, { "text": "-Hang on.", "start": 6601.108, "duration": 1.682 }, { "text": "-I've been working on this reflection.", "start": 6602.79, "duration": 2.05 }, { "text": "-Someone's reflection.", "start": 6604.84, "duration": 1.01 }, { "text": "-Reflection.", "start": 6605.85, "duration": 0.954 }, { "text": "-There's a reflection of the man's face.", "start": 6606.804, "duration": 1.666 }, { "text": "-The reflection.", "start": 6608.47, "duration": 0.666 }, { "text": "-There's a reflection.", "start": 6609.136, "duration": 1.084 }, { "text": "-Zoom in on the mirror.", "start": 6610.22, "duration": 1.35 }, { "text": "-You can see a reflection.", "start": 6611.57, "duration": 2.069 }, { "text": "-Can you enhance the image from here?", "start": 6613.639, "duration": 1.541 }, { "text": "-Can you enhance it right here?", "start": 6615.18, "duration": 1.04 }, { "text": "-Can you enhance it?", "start": 6616.22, "duration": 0.833 }, { "text": "-Can you enhance it?", "start": 6617.053, "duration": 0.907 }, { "text": "-Can we enhance this?", "start": 6617.96, "duration": 0.77 }, { "text": "-Can you enhance it?", "start": 6618.73, "duration": 0.75 }, { "text": "-Hold on a second.", "start": 6619.48, "duration": 0.75 }, { "text": "I'll enhance.", "start": 6620.23, "duration": 0.59 }, { "text": "-Zoom in on the door.", "start": 6620.82, "duration": 1.11 }, { "text": "-Times 10.", "start": 6621.93, "duration": 0.984 }, { "text": "-Zoom.", "start": 6622.914, "duration": 0.5 }, { "text": "-Move in.", "start": 6623.414, "duration": 0.956 }, { "text": "More.", "start": 6624.37, "duration": 0.67 }, { "text": "-Wait, stop.", "start": 6625.04, "duration": 0.72 }, { "text": "-Stop.", "start": 6625.76, "duration": 0.5 }, { "text": "-Pause it.", "start": 6626.26, "duration": 0.57 }, { "text": "-Rotate it 75 degrees\naround the vertical, please.", "start": 6626.83, "duration": 3.48 }, { "text": "-Stop.", "start": 6630.31, "duration": 1.326 }, { "text": "And go back to the part\nabove the doors again.", "start": 6631.636, "duration": 3.18 }, { "text": "-Got an image enhancer that can bitmap.", "start": 6634.816, "duration": 1.624 }, { "text": "-Maybe we can use the Pradeep\nSen method see into the windows.", "start": 6636.44, "duration": 3.182 }, { "text": "-This software is state of the art.", "start": 6639.622, "duration": 1.458 }, { "text": "-The eigenvalue is off.", "start": 6641.08, "duration": 1.69 }, { "text": "-With the right\ncombination of algorithms--", "start": 6642.77, "duration": 2.39 }, { "text": "-He's taken illumination\nalgorithms to the next level,", "start": 6645.16, "duration": 2.28 }, { "text": "and I can use them to\nenhance this photograph.", "start": 6647.44, "duration": 2.2 }, { "text": "-Lock on and enlarge the z-axis.", "start": 6649.64, "duration": 2.63 }, { "text": "-Enhance.", "start": 6652.27, "duration": 1.01 }, { "text": "-Enhance.", "start": 6653.28, "duration": 0.57 }, { "text": "-Enhance.", "start": 6653.85, "duration": 0.6 }, { "text": "-Freeze and enhance.", "start": 6654.45, "duration": 1.023 }, { "text": "[END PLAYBACK]", "start": 6655.473, "duration": 0.583 }, { "text": "DAVID J. MALAN: All right.", "start": 6656.056, "duration": 1.083 }, { "text": "That's it for CS50.", "start": 6657.139, "duration": 1.491 }, { "text": "We will see you next time.", "start": 6658.63, "duration": 1.737 }, { "text": "[VIDEO PLAYBACK]", "start": 6664.023, "duration": 2.567 }, { "text": "-Everyone knows you went to Yale,\nbut nobody knows what happened.", "start": 6666.59, "duration": 3.78 }, { "text": "What can you tell me about that weekend?", "start": 6670.37, "duration": 1.81 }, { "text": "What can you tell me about Rosebud?", "start": 6672.18, "duration": 2.81 }, { "text": "[DRAMATIC CHORD]", "start": 6674.99, "duration": 2.4 }, { "text": "[DRAMATIC MUSIC PLAYING]", "start": 6677.39, "duration": 24.9 }, { "text": "[END PLAYBACK]", "start": 6702.29, "duration": 1.25 } ]