{"captions":[{"content":"[Section 4 - More Comfortable]","startTime":0,"duration":2520,"startOfParagraph":false},{"content":"[Rob Bowden - Harvard University]","startTime":2520,"duration":2330,"startOfParagraph":false},{"content":"[This is CS50. - CS50.TV]","startTime":4850,"duration":2520,"startOfParagraph":false},{"content":"We have a quiz tomorrow, in case you guys didn't know that.","startTime":8920,"duration":4430,"startOfParagraph":false},{"content":"It's basically on everything you could have seen in class or should have seen in class.","startTime":14810,"duration":6160,"startOfParagraph":false},{"content":"That includes pointers, even though they're a very recent topic.","startTime":20970,"duration":5390,"startOfParagraph":false},{"content":"You should at least understand the high levels of them.","startTime":26360,"duration":3500,"startOfParagraph":false},{"content":"Anything that was gone over in class you should understand for the quiz.","startTime":29860,"duration":4900,"startOfParagraph":false},{"content":"So if you have questions on them, you can ask them now.","startTime":34760,"duration":2560,"startOfParagraph":false},{"content":"But this is going to be a very student-led session where you guys ask questions,","startTime":37320,"duration":5960,"startOfParagraph":false},{"content":"so hopefully people have questions.","startTime":43280,"duration":1780,"startOfParagraph":false},{"content":"Does anyone have questions?","startTime":45060,"duration":2960,"startOfParagraph":false},{"content":"Yes. >>[student] Can you go over pointers again?","startTime":49770,"duration":2320,"startOfParagraph":false},{"content":"I'll go over pointers.","startTime":52090,"duration":2260,"startOfParagraph":false},{"content":"All of your variables necessarily live in memory,","startTime":54350,"duration":4830,"startOfParagraph":false},{"content":"but usually you don't worry about that and you just say x + 2 and y + 3","startTime":59180,"duration":5270,"startOfParagraph":false},{"content":"and the compiler will figure out where the things are living for you.","startTime":64450,"duration":2630,"startOfParagraph":false},{"content":"Once you're dealing with pointers, now you're explicitly using those memory addresses.","startTime":67080,"duration":5910,"startOfParagraph":false},{"content":"So a single variable will only ever live at a single address at any given time.","startTime":72990,"duration":6810,"startOfParagraph":false},{"content":"If we want to declare a pointer, what is the type going to look like?","startTime":79800,"duration":4240,"startOfParagraph":false},{"content":"I want to declare a pointer p. What does the type look like?","startTime":84040,"duration":2170,"startOfParagraph":true},{"content":"[student] int *p. >>Yeah. So int *p.","startTime":86210,"duration":7320,"startOfParagraph":false},{"content":"And how do I make it point to x? >>[student] Ampersand.","startTime":93530,"duration":4500,"startOfParagraph":false},{"content":"[Bowden] So ampersand is literally called the address of operator.","startTime":100540,"duration":4760,"startOfParagraph":false},{"content":"So when I say &x it's getting the memory address of the variable x.","startTime":105300,"duration":5160,"startOfParagraph":false},{"content":"So now I have the pointer p, and anywhere in my code I can use *p","startTime":110460,"duration":6330,"startOfParagraph":false},{"content":"or I could use x and it will be the exact same thing.","startTime":116790,"duration":6170,"startOfParagraph":false},{"content":"(*p). What is this doing? What does that star mean?","startTime":122960,"duration":6560,"startOfParagraph":false},{"content":"[student] It means a value at that point. >>Yeah.","startTime":129520,"duration":3600,"startOfParagraph":false},{"content":"So if we look at it, it can be very useful to draw out the diagrams","startTime":133120,"duration":4470,"startOfParagraph":false},{"content":"where this is a little box of memory for x, which happens to have the value 4,","startTime":137590,"duration":4640,"startOfParagraph":false},{"content":"then we have a little box of memory for p,","startTime":142230,"duration":3750,"startOfParagraph":false},{"content":"and so p points to x, so we draw an arrow from p to x.","startTime":145980,"duration":5610,"startOfParagraph":false},{"content":"So when we say *p we're saying go to the box that is p.","startTime":151590,"duration":8680,"startOfParagraph":false},{"content":"Star is follow the arrow and then do whatever you want with that box right there.","startTime":160270,"duration":6210,"startOfParagraph":false},{"content":"So I can say *p = 7; and that will go to the box that is x and change that to 7.","startTime":166480,"duration":14610,"startOfParagraph":false},{"content":"Or I could say int z = *p * 2; That's confusing because it's star, star.","startTime":181090,"duration":12450,"startOfParagraph":false},{"content":"The one star is dereferencing p, the other star is multiplying by 2.","startTime":193540,"duration":5690,"startOfParagraph":false},{"content":"Notice I could have just as well replaced the *p with x.","startTime":199230,"duration":7550,"startOfParagraph":false},{"content":"You can use them in the same way.","startTime":206780,"duration":2650,"startOfParagraph":false},{"content":"And then later on I can have p point to a completely new thing.","startTime":209430,"duration":8570,"startOfParagraph":false},{"content":"I can just say p = &z;","startTime":218000,"duration":4190,"startOfParagraph":false},{"content":"So now p no longer points to x; it points to z.","startTime":222190,"duration":2750,"startOfParagraph":false},{"content":"And any time I do *p it's the same as doing z.","startTime":224940,"duration":5570,"startOfParagraph":false},{"content":"So the useful thing about this is once we start getting into functions.","startTime":230510,"duration":5660,"startOfParagraph":false},{"content":"It's kind of useless to declare a pointer that points to something ","startTime":236170,"duration":3620,"startOfParagraph":true},{"content":"and then you're just dereferencing it ","startTime":239790,"duration":3350,"startOfParagraph":false},{"content":"when you could have used the original variable to begin with.","startTime":243140,"duration":2920,"startOfParagraph":false},{"content":"But when you get into functions--so let's say we have some function, int foo,","startTime":246060,"duration":12130,"startOfParagraph":false},{"content":"that takes a pointer and just does *p = 6;","startTime":258190,"duration":14620,"startOfParagraph":false},{"content":"Like we saw before with swap, you can't do an effective swap and a separate function","startTime":272810,"duration":7180,"startOfParagraph":false},{"content":"by just passing integers because everything in C is always passing by value.","startTime":279990,"duration":5190,"startOfParagraph":false},{"content":"Even when you're passing pointers you're passing by value.","startTime":285180,"duration":3180,"startOfParagraph":false},{"content":"It just so happens that those values are memory addresses.","startTime":288360,"duration":3580,"startOfParagraph":false},{"content":"So when I say foo(p); I'm passing the pointer into the function foo","startTime":291940,"duration":8830,"startOfParagraph":false},{"content":"and then foo is doing *p = 6;","startTime":300770,"duration":3140,"startOfParagraph":false},{"content":"So inside of that function, *p is still equivalent to x,","startTime":303910,"duration":4690,"startOfParagraph":false},{"content":"but I can't use x inside of that function because it's not scoped within that function.","startTime":308600,"duration":4120,"startOfParagraph":false},{"content":"So *p = 6 is the only way I can access a local variable from another function.","startTime":312720,"duration":6790,"startOfParagraph":false},{"content":"Or, well, pointers are the only way I can access a local variable from another function.","startTime":319510,"duration":4090,"startOfParagraph":false},{"content":"[student] Let's say you wanted to return a pointer. How exactly do you do that?","startTime":323600,"duration":8000,"startOfParagraph":false},{"content":"[Bowden] Return a pointer as in something like int y = 3; return &y? >>[student] Yeah.","startTime":331600,"duration":12670,"startOfParagraph":false},{"content":"[Bowden] Okay. You should never do this. This is bad.","startTime":344270,"duration":4210,"startOfParagraph":false},{"content":"I think I saw in these lecture slides you started seeing this whole diagram of memory","startTime":348480,"duration":11000,"startOfParagraph":false},{"content":"where up here you've got memory address 0 ","startTime":359480,"duration":3400,"startOfParagraph":false},{"content":"and down here you have memory address 4 gigs or 2 to the 32.","startTime":362880,"duration":6670,"startOfParagraph":false},{"content":"So then you've got some stuff and some stuff and then you have your stack","startTime":369550,"duration":5570,"startOfParagraph":false},{"content":"and you've got your heap, which you just started learning about, growing up.","startTime":375120,"duration":6660,"startOfParagraph":false},{"content":"[student] Isn't the heap above the stack?","startTime":381780,"duration":2610,"startOfParagraph":false},{"content":"Yeah. The heap is on top, isn't it? >>[student] Well, he put 0 on top.","startTime":384390,"duration":3370,"startOfParagraph":true},{"content":"[student] Oh, he put 0 on top. >>[student] Oh, okay.","startTime":387760,"duration":2560,"startOfParagraph":false},{"content":"Disclaimer: Anywhere with CS50 you're going to see it this way. >>[student] Okay.","startTime":390320,"duration":5740,"startOfParagraph":false},{"content":"It's just that when you're first seeing stacks,","startTime":396060,"duration":4230,"startOfParagraph":false},{"content":"like when you think of a stack you think of stacking things on top of one another.","startTime":400290,"duration":4710,"startOfParagraph":false},{"content":"So we tend to flip this around so the stack is growing up like a stack normally would","startTime":405000,"duration":5810,"startOfParagraph":false},{"content":"instead of the stack hanging down. >>[student] Don't heaps technically grow up too, though?","startTime":410810,"duration":5130,"startOfParagraph":false},{"content":"It depends on what you mean by grow up.","startTime":415940,"duration":5160,"startOfParagraph":false},{"content":"The stack and heap always grow in opposite directions.","startTime":421100,"duration":2910,"startOfParagraph":false},{"content":"A stack is always growing up in the sense that it's growing up ","startTime":424010,"duration":5410,"startOfParagraph":false},{"content":"towards higher memory addresses, and the heap is growing down","startTime":429420,"duration":3520,"startOfParagraph":false},{"content":"in that it's growing towards lower memory addresses.","startTime":432940,"duration":4320,"startOfParagraph":false},{"content":"So the top is 0 and the bottom is high memory addresses.","startTime":437260,"duration":2990,"startOfParagraph":false},{"content":"They're both growing, just in opposing directions.","startTime":440250,"duration":6140,"startOfParagraph":false},{"content":"[student] I just meant that because you said you put stack on the bottom","startTime":446390,"duration":2840,"startOfParagraph":false},{"content":"because it seems more intuitive because for the stack to start at the top of a heap,","startTime":449230,"duration":4410,"startOfParagraph":false},{"content":"heap's on top of itself too, so that's-- >>Yeah.","startTime":453640,"duration":3880,"startOfParagraph":false},{"content":"You also think of the heap as growing up and larger, but the stack more so.","startTime":457520,"duration":7440,"startOfParagraph":false},{"content":"So the stack is the one that we kind of want to show growing up.","startTime":464960,"duration":5320,"startOfParagraph":false},{"content":"But everywhere you look otherwise is going to show address 0 at the top","startTime":470280,"duration":5110,"startOfParagraph":false},{"content":"and the highest memory address at the bottom, so this is your usual view of memory.","startTime":475390,"duration":4200,"startOfParagraph":false},{"content":"Do you have a question?","startTime":479590,"duration":2510,"startOfParagraph":true},{"content":"[student] Can you tell us more about the heap?","startTime":482100,"duration":2170,"startOfParagraph":false},{"content":"Yeah. I'll get to that in a second.","startTime":484270,"duration":1910,"startOfParagraph":false},{"content":"First, going back to why returning &y is a bad thing,","startTime":486180,"duration":6040,"startOfParagraph":false},{"content":"on the stack you have a bunch of stack frames which represent all of the functions","startTime":492220,"duration":6250,"startOfParagraph":false},{"content":"which have been called.","startTime":498470,"duration":1990,"startOfParagraph":false},{"content":"So ignoring previous things, the top of your stack is always going to be the main function","startTime":500460,"duration":7530,"startOfParagraph":false},{"content":"since that's the first function that's being called.","startTime":507990,"duration":5100,"startOfParagraph":false},{"content":"And then when you call another function, the stack is going to grow down.","startTime":513090,"duration":4040,"startOfParagraph":false},{"content":"So if I call some function, foo, and it gets its own stack frame,","startTime":517130,"duration":4510,"startOfParagraph":false},{"content":"it can call some function, bar; it gets its own stack frame.","startTime":521640,"duration":5640,"startOfParagraph":false},{"content":"And bar could be recursive and it could call itself,","startTime":527280,"duration":2560,"startOfParagraph":false},{"content":"and so that second call to bar is going to get its own stack frame.","startTime":529840,"duration":4310,"startOfParagraph":false},{"content":"And so what goes in these stack frames are all of the local variables","startTime":534150,"duration":4730,"startOfParagraph":false},{"content":"and all of the function arguments that-- ","startTime":538880,"duration":4570,"startOfParagraph":false},{"content":"Any things that are locally scoped to this function go in these stack frames.","startTime":543450,"duration":5280,"startOfParagraph":false},{"content":"So that means when I said something like bar is a function,","startTime":548730,"duration":12790,"startOfParagraph":false},{"content":"I'm just going to declare an integer and then return a pointer to that integer.","startTime":561520,"duration":7750,"startOfParagraph":false},{"content":"So where does y live?","startTime":569270,"duration":4520,"startOfParagraph":false},{"content":"[student] y lives in bar. >>[Bowden] Yeah.","startTime":573790,"duration":3110,"startOfParagraph":false},{"content":"Somewhere in this little square of memory is a littler square that has y in it.","startTime":576900,"duration":8110,"startOfParagraph":false},{"content":"When I return &y, I'm returning a pointer to this little block of memory.","startTime":585010,"duration":8360,"startOfParagraph":false},{"content":"But then when a function returns, its stack frame gets popped off the stack.","startTime":593370,"duration":5030,"startOfParagraph":false},{"content":"And that's why it's called stack.","startTime":601050,"duration":2480,"startOfParagraph":false},{"content":"It's like the stack data structure, if you know what that is.","startTime":603530,"duration":3040,"startOfParagraph":false},{"content":"Or even like a stack of trays is always the example,","startTime":606570,"duration":5010,"startOfParagraph":false},{"content":"main is going to go on the bottom, then the first function you call is going to go on top of that,","startTime":611580,"duration":4480,"startOfParagraph":false},{"content":"and you can't get back to main until you return from all functions which have been called","startTime":616060,"duration":4340,"startOfParagraph":false},{"content":"that have been placed on top of it.","startTime":620400,"duration":1940,"startOfParagraph":false},{"content":"[student] So if you did do return the &y, that value is subject to change without notice.","startTime":622340,"duration":6310,"startOfParagraph":true},{"content":"Yes, it's-- >>[student] It could be overwritten. >>Yeah.","startTime":628650,"duration":2640,"startOfParagraph":false},{"content":"It's completely-- If you try and--","startTime":631290,"duration":3370,"startOfParagraph":false},{"content":"This would also be an int *bar because it's returning a pointer,","startTime":634660,"duration":3380,"startOfParagraph":false},{"content":"so its return type is int *.","startTime":638040,"duration":3270,"startOfParagraph":false},{"content":"If you try to use the return value of this function, it's undefined behavior","startTime":641310,"duration":5190,"startOfParagraph":false},{"content":"because that pointer points to bad memory. >>[student] Okay.","startTime":646500,"duration":5270,"startOfParagraph":false},{"content":"So what if, for example, you declared int *y = malloc(sizeof(int))? ","startTime":651770,"duration":9480,"startOfParagraph":false},{"content":"That's better. Yes.","startTime":661250,"duration":2490,"startOfParagraph":false},{"content":"[student] We talked about how when we drag things to our recycle bin","startTime":663740,"duration":3990,"startOfParagraph":false},{"content":"they're not actually erased; we just lose their pointers.","startTime":667730,"duration":4020,"startOfParagraph":false},{"content":"So in this case do we actually erase the value or is it still there in memory?","startTime":671750,"duration":3800,"startOfParagraph":false},{"content":"For the most part, it's going to still be there.","startTime":675550,"duration":3580,"startOfParagraph":false},{"content":"But let's say we happen to call some other function, baz.","startTime":679130,"duration":5090,"startOfParagraph":false},{"content":"Baz is going to get its own stack frame on here.","startTime":684220,"duration":4770,"startOfParagraph":false},{"content":"It's going to be overwriting all of this stuff,","startTime":688990,"duration":2480,"startOfParagraph":false},{"content":"and then if you later try and use the pointer that you got before,","startTime":691470,"duration":2710,"startOfParagraph":false},{"content":"it's not going to be the same value.","startTime":694180,"duration":1390,"startOfParagraph":false},{"content":"It's going to have changed just because you called the function baz.","startTime":695570,"duration":2580,"startOfParagraph":false},{"content":"[student] But had we not, would we still get 3?","startTime":698150,"duration":4930,"startOfParagraph":false},{"content":"[Bowden] In all likelihood, you would.","startTime":703080,"duration":1910,"startOfParagraph":false},{"content":"But you can't rely on that. C just says undefined behavior.","startTime":704990,"duration":4680,"startOfParagraph":false},{"content":"[student] Oh, it does. Okay.","startTime":709670,"duration":2250,"startOfParagraph":true},{"content":"So when you want to return a pointer, this is where malloc comes in use.","startTime":711920,"duration":6270,"startOfParagraph":false},{"content":"I'm writing actually just return malloc(3 * sizeof(int)).","startTime":720930,"duration":15030,"startOfParagraph":false},{"content":"We'll go over malloc more in a second, but the idea of malloc is all of your local variables","startTime":737360,"duration":6690,"startOfParagraph":false},{"content":"always go on the stack.","startTime":744050,"duration":2710,"startOfParagraph":false},{"content":"Anything that's malloced goes on the heap, and it will forever and always be on the heap","startTime":746760,"duration":4810,"startOfParagraph":false},{"content":"until you explicitly free it.","startTime":751570,"duration":2920,"startOfParagraph":false},{"content":"So this means that when you malloc something, it's going to survive after the function returns.","startTime":754490,"duration":7640,"startOfParagraph":false},{"content":"[student] Will it survive after the program stops running? >>No.","startTime":762130,"duration":4670,"startOfParagraph":false},{"content":"Okay, so it's going to be there until the program is all the way done running. >>Yes.","startTime":766800,"duration":6380,"startOfParagraph":false},{"content":"We can go over details of what happens when the program stops running.","startTime":773180,"duration":4330,"startOfParagraph":false},{"content":"You might need to remind me, but that is a separate thing entirely.","startTime":777510,"duration":4640,"startOfParagraph":false},{"content":"[student] So malloc creates a pointer? >>Yeah.","startTime":782150,"duration":2040,"startOfParagraph":false},{"content":"Malloc-- >>[student] I think malloc designates a block of memory that a pointer can use.","startTime":784190,"duration":8840,"startOfParagraph":false},{"content":"[Bowden] I want that diagram again. >>[student] So this function works, though?","startTime":795400,"duration":4210,"startOfParagraph":false},{"content":"[student] Yeah, malloc designates a block of memory that you can use,","startTime":799610,"duration":6820,"startOfParagraph":false},{"content":"and then it returns the address of the first block of that memory.","startTime":806430,"duration":4040,"startOfParagraph":false},{"content":"[Bowden] Yeah. So when you malloc, you're grabbing some block of memory","startTime":810470,"duration":6280,"startOfParagraph":true},{"content":"that's currently in the heap.","startTime":816750,"duration":1510,"startOfParagraph":false},{"content":"If the heap is too small, then the heap is just going to grow, and it grows in this direction.","startTime":818260,"duration":4780,"startOfParagraph":false},{"content":"So let's say the heap is too small.","startTime":823040,"duration":1610,"startOfParagraph":false},{"content":"Then it's about to grow a little bit and return a pointer to this block that just grew.","startTime":824650,"duration":5310,"startOfParagraph":false},{"content":"When you free stuff, you're making more room in the heap,","startTime":829960,"duration":5170,"startOfParagraph":false},{"content":"so then a later call to malloc can reuse that memory that you had previously freed.","startTime":835130,"duration":4900,"startOfParagraph":false},{"content":"The important thing about malloc and free is that it gives you complete control","startTime":840030,"duration":9920,"startOfParagraph":false},{"content":"over the lifetime of these memory blocks.","startTime":849950,"duration":2750,"startOfParagraph":false},{"content":"Global variables are always alive.","startTime":852700,"duration":2720,"startOfParagraph":false},{"content":"Local variables are alive within their scope.","startTime":855420,"duration":3080,"startOfParagraph":false},{"content":"As soon as you go past a curly brace, the local variables are dead.","startTime":858500,"duration":3640,"startOfParagraph":false},{"content":"Malloced memory is alive when you want it to be alive","startTime":862140,"duration":6750,"startOfParagraph":false},{"content":"and then is released when you tell it to be released.","startTime":868890,"duration":4590,"startOfParagraph":false},{"content":"Those are actually the only 3 types of memory, really.","startTime":873480,"duration":4940,"startOfParagraph":false},{"content":"There's automatic memory management, which is the stack.","startTime":878420,"duration":3420,"startOfParagraph":false},{"content":"Things happen for you automatically.","startTime":881840,"duration":2000,"startOfParagraph":false},{"content":"When you say int x, memory is allocated for int x.","startTime":883840,"duration":3070,"startOfParagraph":false},{"content":"When x goes out of scope, memory is reclaimed for x.","startTime":886910,"duration":4720,"startOfParagraph":false},{"content":"Then there's dynamic memory management, which is what malloc is,","startTime":891630,"duration":3160,"startOfParagraph":false},{"content":"which is when you have control.","startTime":894790,"duration":1950,"startOfParagraph":false},{"content":"You dynamically decide when memory should and should not be allocated.","startTime":896740,"duration":4550,"startOfParagraph":false},{"content":"And then there's static, which just means that it lives forever, ","startTime":901290,"duration":3760,"startOfParagraph":false},{"content":"which is what global variables are.","startTime":905050,"duration":1560,"startOfParagraph":false},{"content":"They're just always in memory.","startTime":906610,"duration":3630,"startOfParagraph":false},{"content":"Questions?","startTime":910960,"duration":1800,"startOfParagraph":true},{"content":"[student] Can you define a block just by using curly braces","startTime":914490,"duration":2740,"startOfParagraph":false},{"content":"but not having to have an if statement or a while statement or anything like that?","startTime":917230,"duration":3990,"startOfParagraph":false},{"content":"You can define a block as in a function, but that has curly braces too.","startTime":921220,"duration":7910,"startOfParagraph":false},{"content":"[student] So you can't just have like a random pair of curly braces in your code","startTime":929130,"duration":2970,"startOfParagraph":false},{"content":"that have local variables? >>Yes, you can.","startTime":932100,"duration":3580,"startOfParagraph":false},{"content":"Inside of int bar we could have {int y = 3;}.","startTime":935680,"duration":10220,"startOfParagraph":false},{"content":"That's supposed to be right here.","startTime":945900,"duration":2540,"startOfParagraph":false},{"content":"But that completely defines the scope of int y.","startTime":948440,"duration":4010,"startOfParagraph":false},{"content":"After that second curly brace, y cannot be used anymore.","startTime":952450,"duration":4870,"startOfParagraph":false},{"content":"You almost never do that, though.","startTime":957910,"duration":2720,"startOfParagraph":false},{"content":"Getting back to what happens when a program ends,","startTime":962940,"duration":4430,"startOfParagraph":false},{"content":"there's kind of a misconception/half lie that we give in order to just make things easier.","startTime":967370,"duration":11390,"startOfParagraph":false},{"content":"We tell you that when you allocate memory","startTime":978760,"duration":5650,"startOfParagraph":false},{"content":"you're allocating some chunk of RAM for that variable.","startTime":984410,"duration":5450,"startOfParagraph":false},{"content":"But you're not really directly touching RAM ever in your programs.","startTime":989860,"duration":4330,"startOfParagraph":false},{"content":"If you think of it, how I drew--","startTime":994190,"duration":3300,"startOfParagraph":false},{"content":"And actually, if you go through in GDB you'll see the same thing.","startTime":997490,"duration":6840,"startOfParagraph":false},{"content":"Regardless of how many times you run your program or what program you're running,","startTime":1011120,"duration":6470,"startOfParagraph":false},{"content":"the stack is always going to start--","startTime":1017590,"duration":2360,"startOfParagraph":false},{"content":"you're always going to see variables around address oxbffff something.","startTime":1019950,"duration":6560,"startOfParagraph":false},{"content":"It's usually somewhere in that region.","startTime":1026510,"duration":2960,"startOfParagraph":false},{"content":"But how can 2 programs possibly have pointers to the same memory?","startTime":1029470,"duration":9290,"startOfParagraph":false},{"content":"[student] There's some arbitrary designation of where oxbfff is supposed to be on the RAM","startTime":1040640,"duration":7010,"startOfParagraph":false},{"content":"that can actually be in different places depending on when the function was called.","startTime":1047650,"duration":3670,"startOfParagraph":false},{"content":"Yeah. The term is virtual memory.","startTime":1051320,"duration":4600,"startOfParagraph":false},{"content":"The idea is that every single process, every single program that is running on your computer","startTime":1055920,"duration":6330,"startOfParagraph":false},{"content":"has its own--let's assume 32 bits--completely independent address space.","startTime":1062250,"duration":7200,"startOfParagraph":false},{"content":"This is the address space.","startTime":1069450,"duration":2140,"startOfParagraph":false},{"content":"It has its own completely independent 4 gigabytes to use.","startTime":1071590,"duration":4630,"startOfParagraph":false},{"content":"So if you run 2 programs simultaneously, this program sees 4 gigabytes to itself,","startTime":1076220,"duration":6000,"startOfParagraph":true},{"content":"this program sees 4 gigabytes to itself, ","startTime":1082220,"duration":2650,"startOfParagraph":false},{"content":"and it's impossible for this program to dereference a pointer","startTime":1084870,"duration":2850,"startOfParagraph":false},{"content":"and end up with memory from this program.","startTime":1087720,"duration":3200,"startOfParagraph":false},{"content":"And what virtual memory is is a mapping from a processes address space","startTime":1090920,"duration":7280,"startOfParagraph":false},{"content":"to actual things on RAM.","startTime":1098200,"duration":2270,"startOfParagraph":false},{"content":"So it's up to your operating system to know that,","startTime":1100470,"duration":2470,"startOfParagraph":false},{"content":"hey, when this guy dereferences pointer oxbfff, that really means ","startTime":1102940,"duration":5140,"startOfParagraph":false},{"content":"that he wants RAM byte 1000, ","startTime":1108080,"duration":2960,"startOfParagraph":false},{"content":"whereas if this program dereferences oxbfff, he really wants RAM byte 10000.","startTime":1111040,"duration":7110,"startOfParagraph":false},{"content":"They can be arbitrarily far apart.","startTime":1118150,"duration":3440,"startOfParagraph":false},{"content":"This is even true of things within a single processes address space.","startTime":1121590,"duration":7140,"startOfParagraph":false},{"content":"So like it sees all 4 gigabytes to itself, but let's say--","startTime":1128730,"duration":6040,"startOfParagraph":false},{"content":"[student] Does every single process--","startTime":1134770,"duration":2520,"startOfParagraph":false},{"content":"Let's say you have a computer with only 4 gigabytes of RAM.","startTime":1137290,"duration":4060,"startOfParagraph":false},{"content":"Does every single process see the whole 4 gigabytes? >>Yes.","startTime":1141350,"duration":5080,"startOfParagraph":false},{"content":"But the 4 gigabytes it sees is a lie.","startTime":1146430,"duration":6630,"startOfParagraph":false},{"content":"It's just it thinks it has all this memory because it doesn't know any other process exists.","startTime":1153060,"duration":7400,"startOfParagraph":false},{"content":"It will only use as much memory as it actually needs.","startTime":1160460,"duration":7680,"startOfParagraph":false},{"content":"The operating system is not going to give RAM to this process","startTime":1168140,"duration":4200,"startOfParagraph":false},{"content":"if it's not using any memory in this entire region.","startTime":1172340,"duration":3410,"startOfParagraph":false},{"content":"It's not going to give it memory for that region.","startTime":1175750,"duration":3550,"startOfParagraph":false},{"content":"But the idea is that-- I'm trying to think of-- I can't think of an analogy.","startTime":1179300,"duration":15480,"startOfParagraph":false},{"content":"Analogies are hard.","startTime":1194780,"duration":2000,"startOfParagraph":false},{"content":"One of the issues of virtual memory or one of the things it's solving","startTime":1197740,"duration":4960,"startOfParagraph":false},{"content":"is that processes should be completely unaware of one another.","startTime":1202700,"duration":4110,"startOfParagraph":false},{"content":"And so you can write any program that just dereferences any pointer,","startTime":1206810,"duration":5330,"startOfParagraph":false},{"content":"like just write a program that says *(ox1234),","startTime":1212140,"duration":7200,"startOfParagraph":false},{"content":"and that's dereferencing memory address 1234.","startTime":1219340,"duration":3550,"startOfParagraph":false},{"content":"But it's up to the operating system to then translate what 1234 means.","startTime":1222890,"duration":5980,"startOfParagraph":true},{"content":"So if 1234 happens to be a valid memory address for this process,","startTime":1228870,"duration":5090,"startOfParagraph":false},{"content":"like it's on the stack or something, then this will return the value of that memory address","startTime":1233960,"duration":4840,"startOfParagraph":false},{"content":"as far as the process knows.","startTime":1238800,"duration":3160,"startOfParagraph":false},{"content":"But if 1234 is not a valid address, like it happens to land ","startTime":1241960,"duration":5560,"startOfParagraph":false},{"content":"in some little piece of memory here that is beyond the stack and beyond the heap","startTime":1247520,"duration":5390,"startOfParagraph":false},{"content":"and you haven't really used that, then that's when you get things like segfaults","startTime":1252910,"duration":4290,"startOfParagraph":false},{"content":"because you're touching memory that you should not be touching.","startTime":1257200,"duration":3060,"startOfParagraph":false},{"content":"This is also true--","startTime":1267180,"duration":2160,"startOfParagraph":false},{"content":"A 32-bit system, 32 bits means you have 32 bits to define a memory address.","startTime":1269340,"duration":6100,"startOfParagraph":false},{"content":"It's why pointers are 8 bytes because 32 bits are 8 bytes--or 4 bytes.","startTime":1275440,"duration":7530,"startOfParagraph":false},{"content":"Pointers are 4 bytes.","startTime":1282970,"duration":2280,"startOfParagraph":false},{"content":"So when you see a pointer like oxbfffff, that is--","startTime":1285250,"duration":8430,"startOfParagraph":false},{"content":"Within any given program you can just construct any arbitrary pointer,","startTime":1293680,"duration":6400,"startOfParagraph":false},{"content":"anywhere from ox0 to ox 8 f's--ffffffff.","startTime":1300080,"duration":6250,"startOfParagraph":false},{"content":"[student] Didn't you say they're 4 bytes? >>Yeah.","startTime":1306330,"duration":2850,"startOfParagraph":false},{"content":"[student] Then each byte will have-- >>[Bowden] Hexadecimal.","startTime":1309180,"duration":3550,"startOfParagraph":false},{"content":"Hexadecimal--5, 6, 7, 8. So pointers you're going to always see in hexadecimal.","startTime":1312730,"duration":6630,"startOfParagraph":false},{"content":"It's just how we classify pointers.","startTime":1319360,"duration":2350,"startOfParagraph":false},{"content":"Every 2 digits of hexadecimal is 1 byte.","startTime":1321710,"duration":3530,"startOfParagraph":false},{"content":"So there's going to be 8 hexadecimal digits for 4 bytes.","startTime":1325240,"duration":4360,"startOfParagraph":false},{"content":"So every single pointer on a 32-bit system is going to be 4 bytes,","startTime":1329600,"duration":4590,"startOfParagraph":false},{"content":"which means that in your process you can construct any arbitrary 4 bytes","startTime":1334190,"duration":4360,"startOfParagraph":false},{"content":"and make a pointer out of it,","startTime":1338550,"duration":2000,"startOfParagraph":false},{"content":"which means that as far as it's aware, it can address an entire 2 to the 32 bytes of memory.","startTime":1340550,"duration":12180,"startOfParagraph":false},{"content":"Even though it doesn't really have access to that,","startTime":1352730,"duration":2030,"startOfParagraph":false},{"content":"even if your computer only has 512 megabytes, it thinks it has that much memory.","startTime":1354760,"duration":5430,"startOfParagraph":false},{"content":"And the operating system is smart enough that it will only allocate what you actually need.","startTime":1360190,"duration":4740,"startOfParagraph":false},{"content":"It doesn't just go, oh, a new process: 4 gigs.","startTime":1364930,"duration":4700,"startOfParagraph":false},{"content":"Yeah. >>[student] What does the ox mean? Why do you write it?","startTime":1369630,"duration":2300,"startOfParagraph":true},{"content":"It's just the symbol for hexadecimal.","startTime":1371930,"duration":3050,"startOfParagraph":false},{"content":"When you see a number start with ox, the successive things are hexadecimal.","startTime":1374980,"duration":4610,"startOfParagraph":false},{"content":"[student] You were explaining about what happens when a program ends. >>Yes.","startTime":1381930,"duration":3830,"startOfParagraph":false},{"content":"What happens when a program ends is the operating system ","startTime":1385760,"duration":3720,"startOfParagraph":false},{"content":"just erases the mappings that it has for these addresses, and that's it.","startTime":1389480,"duration":4120,"startOfParagraph":false},{"content":"The operating system can now just give that memory to another program to use.","startTime":1393600,"duration":4170,"startOfParagraph":false},{"content":"[student] Okay. ","startTime":1397770,"duration":1720,"startOfParagraph":false},{"content":"So when you allocate something on the heap or the stack or global variables or anything,","startTime":1399490,"duration":5310,"startOfParagraph":false},{"content":"they all just disappear as soon as the program ends","startTime":1404800,"duration":2210,"startOfParagraph":false},{"content":"because the operating system is now free to give that memory to any other process.","startTime":1407010,"duration":5110,"startOfParagraph":false},{"content":"[student] Even though there are probably still values written in? >>Yeah.","startTime":1412120,"duration":3030,"startOfParagraph":false},{"content":"The values are likely still there.","startTime":1415150,"duration":2590,"startOfParagraph":false},{"content":"It's just it's going to be difficult to get at them.","startTime":1417740,"duration":3830,"startOfParagraph":false},{"content":"It's much more difficult to get at them than it is to get at a deleted file","startTime":1421570,"duration":3660,"startOfParagraph":false},{"content":"because the deleted file kind of sits there for a long time and the hard drive is a lot bigger.","startTime":1425230,"duration":6220,"startOfParagraph":false},{"content":"So it's going to overwrite different parts of memory ","startTime":1431450,"duration":2670,"startOfParagraph":false},{"content":"before it happens to overwrite the chunk of memory that that file used to be at.","startTime":1434120,"duration":4520,"startOfParagraph":false},{"content":"But main memory, RAM, you cycle through a lot faster,","startTime":1438640,"duration":5880,"startOfParagraph":false},{"content":"so it's going to very rapidly be overwritten.","startTime":1444520,"duration":3520,"startOfParagraph":false},{"content":"Questions on this or anything else?","startTime":1450300,"duration":3040,"startOfParagraph":false},{"content":"[student] I have questions about a different topic. >>Okay.","startTime":1453340,"duration":2790,"startOfParagraph":false},{"content":"Does anyone have questions on this?","startTime":1456130,"duration":2930,"startOfParagraph":false},{"content":"Okay. Different topic. >>[student] Okay.","startTime":1460170,"duration":2950,"startOfParagraph":true},{"content":"I was going through some of the practice tests,","startTime":1463120,"duration":3430,"startOfParagraph":false},{"content":"and in one of them it was talking about the sizeof","startTime":1466550,"duration":3930,"startOfParagraph":false},{"content":"and the value that it returns or different variable types. >>Yes.","startTime":1470480,"duration":5150,"startOfParagraph":false},{"content":"And it said that both int and long both return 4, so they're both 4 bytes long.","startTime":1475630,"duration":9430,"startOfParagraph":false},{"content":"Is there any difference between an int and a long, or is it the same thing?","startTime":1485060,"duration":3010,"startOfParagraph":false},{"content":"Yes, there is a difference.","startTime":1488070,"duration":2310,"startOfParagraph":false},{"content":"The C standard--","startTime":1490380,"duration":2580,"startOfParagraph":false},{"content":"I'm probably going to mess up.","startTime":1492960,"duration":1990,"startOfParagraph":false},{"content":"The C standard is just like what C is, the official documentation of C.","startTime":1494950,"duration":3850,"startOfParagraph":false},{"content":"This is what it says.","startTime":1498800,"duration":1540,"startOfParagraph":false},{"content":"So the C standard just says that a char will forever and always be 1 byte.","startTime":1500340,"duration":8310,"startOfParagraph":false},{"content":"Everything after that--a short is always just defined as being greater than or equal to a char.","startTime":1510470,"duration":8570,"startOfParagraph":false},{"content":"This might be strictly greater than, but not positive.","startTime":1519040,"duration":3970,"startOfParagraph":false},{"content":"An int is just defined as being greater than or equal to a short.","startTime":1523010,"duration":8930,"startOfParagraph":false},{"content":"And a long is just defined as being greater than or equal to an int.","startTime":1531940,"duration":4270,"startOfParagraph":false},{"content":"And a long long is greater than or equal to a long.","startTime":1536210,"duration":5390,"startOfParagraph":false},{"content":"So the only thing the C standard defines is the relative ordering of everything.","startTime":1541600,"duration":5010,"startOfParagraph":false},{"content":"The actual amount of memory that things take up is generally up to implementation,","startTime":1546610,"duration":8270,"startOfParagraph":false},{"content":"but it's pretty well defined at this point. >>[student] Okay.","startTime":1554880,"duration":2760,"startOfParagraph":false},{"content":"So shorts are almost always going to be 2 bytes.","startTime":1557640,"duration":4850,"startOfParagraph":false},{"content":"Ints are almost always going to be 4 bytes.","startTime":1564920,"duration":5030,"startOfParagraph":false},{"content":"Long longs are almost always going to be 8 bytes.","startTime":1572070,"duration":3270,"startOfParagraph":false},{"content":"And longs, it depends on whether you're using a 32-bit or a 64-bit system.","startTime":1577990,"duration":5170,"startOfParagraph":false},{"content":"So a long is going to correspond to the type of system.","startTime":1583160,"duration":4290,"startOfParagraph":false},{"content":"If you're using a 32-bit system like the Appliance, it's going to be 4 bytes.","startTime":1587450,"duration":4470,"startOfParagraph":false},{"content":"If you're using a 64-bit like a lot of recent computers, it's going to be 8 bytes.","startTime":1594530,"duration":8040,"startOfParagraph":false},{"content":"Ints are almost always 4 bytes at this point.","startTime":1602570,"duration":2660,"startOfParagraph":true},{"content":"Long longs are almost always 8 bytes.","startTime":1605230,"duration":1910,"startOfParagraph":false},{"content":"In the past, ints used to only be 2 bytes.","startTime":1607140,"duration":3160,"startOfParagraph":false},{"content":"But notice that this completely satisfies all of these relations of greater than and equal to.","startTime":1610300,"duration":6540,"startOfParagraph":false},{"content":"So long is perfectly allowed to be the same size as an integer,","startTime":1616840,"duration":4440,"startOfParagraph":false},{"content":"and it's also allowed to be the same size as a long long.","startTime":1621280,"duration":2750,"startOfParagraph":false},{"content":"And it just so happens to be that in 99.999% of systems, it is going to be equal to","startTime":1624030,"duration":7040,"startOfParagraph":false},{"content":"either an int or a long long. It just depends on 32-bit or 64-bit. >>[student] Okay.","startTime":1631070,"duration":4730,"startOfParagraph":false},{"content":"In floats, how is the decimal point designated in terms of bits?","startTime":1635800,"duration":8800,"startOfParagraph":false},{"content":"Like as binary? >>Yeah.","startTime":1644600,"duration":2560,"startOfParagraph":false},{"content":"You do not need to know that for CS50.","startTime":1647160,"duration":3410,"startOfParagraph":false},{"content":"You don't even learn that in 61.","startTime":1650570,"duration":2390,"startOfParagraph":false},{"content":"You don't learn that really in any course.","startTime":1652960,"duration":4390,"startOfParagraph":false},{"content":"It's just a representation.","startTime":1657350,"duration":5390,"startOfParagraph":false},{"content":"I forget the exact bit allotments.","startTime":1662740,"duration":2700,"startOfParagraph":false},{"content":"The idea of floating point is that you allocate a specific number of bits to represent--","startTime":1665440,"duration":7940,"startOfParagraph":false},{"content":"Basically, everything is in scientific notation.","startTime":1673380,"duration":3170,"startOfParagraph":false},{"content":"So you allocate a specific number of bits to represent the number itself, like 1.2345.","startTime":1676550,"duration":9050,"startOfParagraph":false},{"content":"I can never represent a number with more digits than 5.","startTime":1685600,"duration":4600,"startOfParagraph":false},{"content":"Then you also allocate a specific number of bits so that it tends to be like","startTime":1692200,"duration":14100,"startOfParagraph":false},{"content":"you can only go up to a certain number, like that's the largest exponent you can have,","startTime":1706300,"duration":6510,"startOfParagraph":false},{"content":"and you can only go down to a certain exponent, ","startTime":1712810,"duration":3380,"startOfParagraph":false},{"content":"like that's the smallest exponent you can have.","startTime":1716190,"duration":2580,"startOfParagraph":false},{"content":"I don't remember the exact way bits are assigned to all of these values,","startTime":1718770,"duration":5640,"startOfParagraph":true},{"content":"but a certain number of bits are dedicated to 1.2345,","startTime":1724410,"duration":3530,"startOfParagraph":false},{"content":"another certain number of bits are dedicated to the exponent,","startTime":1727940,"duration":2990,"startOfParagraph":false},{"content":"and it's only possible to represent an exponent of a certain size.","startTime":1730930,"duration":4740,"startOfParagraph":false},{"content":"[student] And a double? Is that like an extra long float? >>Yeah.","startTime":1735670,"duration":5430,"startOfParagraph":false},{"content":"It's the same thing as a float except now you're using 8 bytes instead of 4 bytes.","startTime":1741100,"duration":6840,"startOfParagraph":false},{"content":"Now you'll be able to use 9 digits or 10 digits,","startTime":1747940,"duration":4020,"startOfParagraph":false},{"content":"and this will be able to go up to 300 instead of 100. >>[student] Okay.","startTime":1751960,"duration":4670,"startOfParagraph":false},{"content":"And floats are also 4 bytes. >>Yes.","startTime":1756630,"duration":4920,"startOfParagraph":false},{"content":"Well, again, it probably depends overall on general implementation,","startTime":1761550,"duration":5970,"startOfParagraph":false},{"content":"but floats are 4 bytes, doubles are 8.","startTime":1767520,"duration":3090,"startOfParagraph":false},{"content":"Doubles are called double because they are double the size of floats.","startTime":1770610,"duration":2830,"startOfParagraph":false},{"content":"[student] Okay. And are there double doubles? >>There are not.","startTime":1773440,"duration":4940,"startOfParagraph":false},{"content":"I think-- >>[student] Like long longs? >>Yeah. I don't think so. Yes.","startTime":1778380,"duration":5280,"startOfParagraph":false},{"content":"[student] On last year's test there was a question about the main function","startTime":1783660,"duration":2290,"startOfParagraph":false},{"content":"having to be part of your program.","startTime":1785950,"duration":3540,"startOfParagraph":false},{"content":"The answer was that it doesn't have to be part of your program.","startTime":1789490,"duration":2820,"startOfParagraph":false},{"content":"In what situation? That's what I saw.","startTime":1792310,"duration":2790,"startOfParagraph":false},{"content":"[Bowden] It seems-- >>[student] What situation?","startTime":1795100,"duration":3990,"startOfParagraph":false},{"content":"Do you have the problem? >>[student] Yeah, I can definitely pull it up.","startTime":1799090,"duration":3790,"startOfParagraph":false},{"content":"It doesn't have to be, technically, but basically it's going to be.","startTime":1802880,"duration":5030,"startOfParagraph":false},{"content":"[student] I saw one on a different year's.","startTime":1807910,"duration":2120,"startOfParagraph":false},{"content":"It was like True or False: A valid-- >>Oh, a .c file?","startTime":1810030,"duration":6190,"startOfParagraph":false},{"content":"[student] Any .c file must have-- [both speaking at once - unintelligible]","startTime":1816220,"duration":2570,"startOfParagraph":false},{"content":"Okay. So that's separate.","startTime":1818790,"duration":2330,"startOfParagraph":false},{"content":"A .c file just needs to contain functions.","startTime":1821120,"duration":5680,"startOfParagraph":true},{"content":"You can compile a file into machine code, binary, whatever,","startTime":1826800,"duration":5600,"startOfParagraph":false},{"content":"without it being executable yet.","startTime":1832400,"duration":4220,"startOfParagraph":false},{"content":"A valid executable must have a main function.","startTime":1836620,"duration":2800,"startOfParagraph":false},{"content":"You can write 100 functions in 1 file but no main","startTime":1839420,"duration":6040,"startOfParagraph":false},{"content":"and then compile that down to binary,","startTime":1845460,"duration":3340,"startOfParagraph":false},{"content":"then you write another file that only has main but it calls a bunch of these functions","startTime":1848800,"duration":5660,"startOfParagraph":false},{"content":"in this binary file over here.","startTime":1854460,"duration":2260,"startOfParagraph":false},{"content":"And so when you're making the executable, that's what the linker does","startTime":1856720,"duration":4520,"startOfParagraph":false},{"content":"is it combines these 2 binary files into an executable.","startTime":1861240,"duration":4720,"startOfParagraph":false},{"content":"So a .c file does not need to have a main function at all.","startTime":1865960,"duration":5440,"startOfParagraph":false},{"content":"And on big code bases you'll see thousands of .c files and 1 main file.","startTime":1871400,"duration":7820,"startOfParagraph":false},{"content":"More questions?","startTime":1883960,"duration":2150,"startOfParagraph":false},{"content":"[student] There was another question.","startTime":1889310,"duration":2630,"startOfParagraph":false},{"content":"It said make is a compiler. True or False?","startTime":1891940,"duration":4770,"startOfParagraph":false},{"content":"And the answer was false, and I understood why it's not like Clang.","startTime":1896710,"duration":5320,"startOfParagraph":false},{"content":"But what do we call make if it's not?","startTime":1902030,"duration":2740,"startOfParagraph":false},{"content":"Make is basically just-- I can see exactly what it calls it.","startTime":1904770,"duration":5220,"startOfParagraph":false},{"content":"But it just runs commands.","startTime":1909990,"duration":2420,"startOfParagraph":false},{"content":"Make.","startTime":1913650,"duration":2000,"startOfParagraph":false},{"content":"I can pull this up. Yeah.","startTime":1918240,"duration":2630,"startOfParagraph":false},{"content":"Oh, yeah. Make also does that.","startTime":1930110,"duration":3070,"startOfParagraph":false},{"content":"This says the purpose of the make utility is to determine automatically","startTime":1933180,"duration":3990,"startOfParagraph":false},{"content":"which pieces of a large program need to be recompiled ","startTime":1937170,"duration":2440,"startOfParagraph":false},{"content":"and issue the commands to recompile them.","startTime":1939610,"duration":2740,"startOfParagraph":false},{"content":"You can make make files that are absolutely huge.","startTime":1942350,"duration":5340,"startOfParagraph":false},{"content":"Make looks at the time stamps of files and, like we said before,","startTime":1947690,"duration":5520,"startOfParagraph":false},{"content":"you can compile individual files down, and it's not until you get to the linker","startTime":1953210,"duration":3720,"startOfParagraph":false},{"content":"that they're put together into an executable.","startTime":1956930,"duration":2340,"startOfParagraph":false},{"content":"So if you have 10 different files and you make a change to 1 of them,","startTime":1959270,"duration":4540,"startOfParagraph":false},{"content":"then what make is going to do is just recompile that 1 file","startTime":1963810,"duration":4060,"startOfParagraph":false},{"content":"and then relink everything together.","startTime":1967870,"duration":2770,"startOfParagraph":false},{"content":"But it's much dumber than that.","startTime":1970640,"duration":2380,"startOfParagraph":false},{"content":"It's up to you to completely define that that's what it should be doing.","startTime":1973020,"duration":2670,"startOfParagraph":false},{"content":"It by default has the ability to recognize this time stamp stuff,","startTime":1975690,"duration":3870,"startOfParagraph":false},{"content":"but you can write a make file to do anything.","startTime":1979560,"duration":3660,"startOfParagraph":false},{"content":"You can write a make file so that when you type make it just cd's to another directory.","startTime":1983220,"duration":5930,"startOfParagraph":false},{"content":"I was getting frustrated because I tack everything inside of my Appliance","startTime":1989150,"duration":6410,"startOfParagraph":false},{"content":"and then I view the PDF from the Mac.","startTime":1995560,"duration":6180,"startOfParagraph":false},{"content":"So I go to Finder and I can do Go, Connect to Server,","startTime":2001740,"duration":8980,"startOfParagraph":true},{"content":"and the server I connect to is my Appliance, and then I open up the PDF","startTime":2010720,"duration":6230,"startOfParagraph":false},{"content":"that gets compiled by LaTeX.","startTime":2016950,"duration":3240,"startOfParagraph":false},{"content":"But I was getting frustrated because every single time I needed to refresh the PDF,","startTime":2020190,"duration":9130,"startOfParagraph":false},{"content":"I had to copy it to a specific directory that it could access","startTime":2029320,"duration":4580,"startOfParagraph":false},{"content":"and it was getting annoying.","startTime":2033900,"duration":3810,"startOfParagraph":false},{"content":"So instead I wrote a make file, which you have to define how it makes things.","startTime":2037710,"duration":4940,"startOfParagraph":false},{"content":"How you make in this is PDF LaTeX.","startTime":2042650,"duration":3480,"startOfParagraph":false},{"content":"Just like any other make file--or I guess you haven't seen the make files,","startTime":2046130,"duration":3960,"startOfParagraph":false},{"content":"but we have in the Appliance a global make file that just says,","startTime":2050090,"duration":3420,"startOfParagraph":false},{"content":"if you are compiling a C file, use Clang.","startTime":2053510,"duration":3169,"startOfParagraph":false},{"content":"And so here in my make file that I make I say,","startTime":2056679,"duration":4281,"startOfParagraph":false},{"content":"this file you're going to want to compile with PDF LaTeX.","startTime":2060960,"duration":4060,"startOfParagraph":false},{"content":"And so it's PDF LaTeX that's doing the compiling.","startTime":2065020,"duration":2869,"startOfParagraph":false},{"content":"Make is not compiling. It's just running these commands in the sequence I specified.","startTime":2067889,"duration":3991,"startOfParagraph":false},{"content":"So it runs PDF LaTeX, it copies it to the directory I want it to be copied to,","startTime":2071880,"duration":4230,"startOfParagraph":false},{"content":"it cd's to the directory and does other things,","startTime":2076110,"duration":2160,"startOfParagraph":false},{"content":"but all it does is recognize when a file changes,","startTime":2078270,"duration":4110,"startOfParagraph":false},{"content":"and if it changes, then it will run the commands that it's supposed to run","startTime":2082380,"duration":3109,"startOfParagraph":false},{"content":"when the file changes. >>[student] Okay.","startTime":2085489,"duration":3271,"startOfParagraph":false},{"content":"I don't know where the global make files are for me to check it out.","startTime":2090510,"duration":3910,"startOfParagraph":false},{"content":"Other questions? Anything from past quizzes? Any pointer things?","startTime":2097210,"duration":7080,"startOfParagraph":false},{"content":"There are subtle things with pointers like--","startTime":2106200,"duration":2530,"startOfParagraph":false},{"content":"I'm not going to be able to find a quiz question on it--","startTime":2108730,"duration":1490,"startOfParagraph":false},{"content":"but just like this sort of thing.","startTime":2110220,"duration":6030,"startOfParagraph":false},{"content":"Make sure you understand that when I say int *x *y--","startTime":2119680,"duration":4380,"startOfParagraph":false},{"content":"This isn't exactly anything here, I guess.","startTime":2124890,"duration":3240,"startOfParagraph":false},{"content":"But like *x *y, those are 2 variables that are on the stack.","startTime":2128130,"duration":4010,"startOfParagraph":false},{"content":"When I say x = malloc(sizeof(int)), x is still a variable on the stack,","startTime":2132140,"duration":5080,"startOfParagraph":false},{"content":"malloc is some block over in the heap, and we're having x point to the heap.","startTime":2137220,"duration":3960,"startOfParagraph":false},{"content":"So something on the stack points to the heap.","startTime":2141180,"duration":2720,"startOfParagraph":true},{"content":"Whenever you malloc anything, you're inevitably storing it inside of a pointer.","startTime":2143900,"duration":4200,"startOfParagraph":false},{"content":"So that pointer is on the stack, the malloced block is on the heap.","startTime":2148100,"duration":7840,"startOfParagraph":false},{"content":"A lot of people get confused and say int *x = malloc; x is on the heap.","startTime":2155940,"duration":5300,"startOfParagraph":false},{"content":"No. What x points to is on the heap.","startTime":2161240,"duration":2860,"startOfParagraph":false},{"content":"x itself is on the stack, unless for whatever reason you have x be a global variable,","startTime":2164100,"duration":4440,"startOfParagraph":false},{"content":"in which case it happens to be in another region of memory.","startTime":2168540,"duration":3420,"startOfParagraph":false},{"content":"So keeping track, these box and arrow diagrams are pretty common for the quiz.","startTime":2173450,"duration":7370,"startOfParagraph":false},{"content":"Or if it's not on quiz 0, it will be on quiz 1.","startTime":2180820,"duration":4920,"startOfParagraph":false},{"content":"You should know all of these, the steps in compiling","startTime":2187570,"duration":4370,"startOfParagraph":false},{"content":"since you had to answer questions on those. Yes.","startTime":2191940,"duration":3800,"startOfParagraph":false},{"content":"[student] Could we go over those steps-- >>Sure.","startTime":2195740,"duration":3200,"startOfParagraph":false},{"content":"Before steps and compiling we have preprocessing, ","startTime":2208340,"duration":10300,"startOfParagraph":false},{"content":"compiling, assembling, and linking.","startTime":2218640,"duration":18110,"startOfParagraph":false},{"content":"Preprocessing. What does that do?","startTime":2236750,"duration":4730,"startOfParagraph":false},{"content":"It is the easiest step in--well, not like--","startTime":2249720,"duration":2570,"startOfParagraph":false},{"content":"that doesn't mean it should be obvious, but it's the easiest step.","startTime":2252290,"duration":3480,"startOfParagraph":false},{"content":"You guys could implement it yourselves. Yeah.","startTime":2255770,"duration":2640,"startOfParagraph":false},{"content":"[student] Take what you have in your includes like this and it copies and then also defines.","startTime":2258410,"duration":5000,"startOfParagraph":false},{"content":"It looks for things like #include and #define,","startTime":2263410,"duration":5840,"startOfParagraph":false},{"content":"and it just copies and pastes what those actually mean.","startTime":2269250,"duration":4550,"startOfParagraph":false},{"content":"So when you say #include cs50.h, the preprocessor is copying and pasting cs50.h","startTime":2273800,"duration":5440,"startOfParagraph":false},{"content":"into that line.","startTime":2279240,"duration":1790,"startOfParagraph":false},{"content":"When you say #define x to be 4, the preprocessor goes through the entire program","startTime":2281030,"duration":5610,"startOfParagraph":false},{"content":"and replaces all instances of x with 4.","startTime":2286640,"duration":3760,"startOfParagraph":false},{"content":"So the preprocessor takes a valid C file and outputs a valid C file","startTime":2290400,"duration":7130,"startOfParagraph":false},{"content":"where things have been copied and pasted.","startTime":2297530,"duration":2770,"startOfParagraph":false},{"content":"So now compiling. What does that do?","startTime":2300300,"duration":3930,"startOfParagraph":false},{"content":"[student] It goes from C to binary.","startTime":2305940,"duration":2270,"startOfParagraph":false},{"content":"[Bowden] It doesn't go all the way to binary.","startTime":2308210,"duration":2760,"startOfParagraph":true},{"content":"[student] To machine code then? >>It's not machine code.","startTime":2310970,"duration":3250,"startOfParagraph":false},{"content":"[student] Assembly? >>Assembly.","startTime":2314220,"duration":1480,"startOfParagraph":false},{"content":"It goes to Assembly before it goes all the way to C code,","startTime":2315700,"duration":3190,"startOfParagraph":false},{"content":"and most languages do something like this.","startTime":2318890,"duration":6120,"startOfParagraph":false},{"content":"Pick any high-level language, and if you're going to compile it,","startTime":2327740,"duration":2850,"startOfParagraph":false},{"content":"it's likely to compile in steps.","startTime":2330590,"duration":1800,"startOfParagraph":false},{"content":"First it's going to compile Python to C, then it's going to compile C to Assembly,","startTime":2332390,"duration":5750,"startOfParagraph":false},{"content":"and then Assembly is going to get translated to binary.","startTime":2338140,"duration":3460,"startOfParagraph":false},{"content":"So compiling is going to bring it from C to Assembly.","startTime":2341600,"duration":6200,"startOfParagraph":false},{"content":"The word compiling usually means bringing it from a higher level ","startTime":2347800,"duration":4330,"startOfParagraph":false},{"content":"to a lower level programming language.","startTime":2352130,"duration":2210,"startOfParagraph":false},{"content":"So this is the only step in compilation where you start with a high-level language","startTime":2354340,"duration":4850,"startOfParagraph":false},{"content":"and end up in a low-level language, and that's why the step is called compiling.","startTime":2359190,"duration":4080,"startOfParagraph":false},{"content":"[student] During compiling, let's say that you've done #include cs50.h.","startTime":2365280,"duration":8090,"startOfParagraph":false},{"content":"Will the compiler recompile the cs50.h, like the functions that are in there,","startTime":2373370,"duration":8820,"startOfParagraph":false},{"content":"and translate that into Assembly code as well,","startTime":2382190,"duration":3090,"startOfParagraph":false},{"content":"or will it copy and paste something that's been pre-Assembly?","startTime":2385280,"duration":5550,"startOfParagraph":false},{"content":"cs50.h will pretty much never end up in Assembly.","startTime":2390830,"duration":6080,"startOfParagraph":false},{"content":"Stuff like function prototypes and things are just for you to be careful.","startTime":2399740,"duration":3940,"startOfParagraph":false},{"content":"It guarantees that the compiler can check things like you're calling functions","startTime":2403680,"duration":5590,"startOfParagraph":false},{"content":"with the right return types and the right arguments and stuff.","startTime":2409270,"duration":3640,"startOfParagraph":false},{"content":"So cs50.h will be preprocessed into the file, and then when it's compiling","startTime":2412910,"duration":5440,"startOfParagraph":true},{"content":"it's basically thrown away after it makes sure that everything is being called correctly.","startTime":2418350,"duration":3960,"startOfParagraph":false},{"content":"But the functions defined in the CS50 library, which are separate from cs50.h,","startTime":2422310,"duration":7100,"startOfParagraph":false},{"content":"those will not be separately compiled.","startTime":2429410,"duration":4200,"startOfParagraph":false},{"content":"That will actually come down in the linking step, so we'll get to that in a second.","startTime":2433610,"duration":3660,"startOfParagraph":false},{"content":"But first, what is assembling?","startTime":2437270,"duration":2830,"startOfParagraph":false},{"content":"[student] Assembly to binary? >>Yeah.","startTime":2441850,"duration":2650,"startOfParagraph":false},{"content":"Assembling. ","startTime":2446300,"duration":1890,"startOfParagraph":false},{"content":"We don't call it compiling because Assembly is pretty much a pure translation of binary.","startTime":2448190,"duration":6520,"startOfParagraph":false},{"content":"There is very little logic in going from Assembly to binary.","startTime":2454710,"duration":5520,"startOfParagraph":false},{"content":"It's just like looking up in a table, oh, we have this instruction;","startTime":2460230,"duration":2950,"startOfParagraph":false},{"content":"that corresponds to binary 01110.","startTime":2463180,"duration":3110,"startOfParagraph":false},{"content":"And so the files that assembling generally outputs are .o files.","startTime":2470200,"duration":5030,"startOfParagraph":false},{"content":"And .o files are what we were saying before, ","startTime":2475230,"duration":3790,"startOfParagraph":false},{"content":"how a file does not need to have a main function.","startTime":2479020,"duration":2550,"startOfParagraph":false},{"content":"Any file can be compiled down to a .o file as long as it's a valid C file.","startTime":2481570,"duration":6070,"startOfParagraph":false},{"content":"It can be compiled down to .o.","startTime":2487640,"duration":2660,"startOfParagraph":false},{"content":"Now, linking is what actually brings a bunch of .o files and brings them to an executable.","startTime":2490300,"duration":12730,"startOfParagraph":false},{"content":"And so what linking does is you can think of the CS50 library as a .o file.","startTime":2503030,"duration":8080,"startOfParagraph":false},{"content":"It is an already compiled binary file.","startTime":2511110,"duration":5870,"startOfParagraph":false},{"content":"And so when you compile your file, your hello.c, which calls GetString,","startTime":2516980,"duration":6550,"startOfParagraph":false},{"content":"hello.c gets compiled down to hello.o,","startTime":2523530,"duration":2830,"startOfParagraph":false},{"content":"hello.o is now in binary.","startTime":2526360,"duration":2550,"startOfParagraph":false},{"content":"It uses GetString, so it needs to go over to cs50.o,","startTime":2528910,"duration":3920,"startOfParagraph":false},{"content":"and the linker smooshes them together and copies GetString into this file","startTime":2532830,"duration":3560,"startOfParagraph":false},{"content":"and comes out with an executable that has all functions it needs.","startTime":2536390,"duration":4250,"startOfParagraph":false},{"content":"So cs50.o isn't actually an O file, but it's close enough that there is no fundamental difference.","startTime":2540640,"duration":11980,"startOfParagraph":false},{"content":"So linking just brings a bunch of files together ","startTime":2552620,"duration":4260,"startOfParagraph":false},{"content":"that separately contain all of the functions I need to use","startTime":2556880,"duration":4510,"startOfParagraph":false},{"content":"and creates the executable that will actually run.","startTime":2561390,"duration":4730,"startOfParagraph":false},{"content":"And so that's also what we were saying before","startTime":2568420,"duration":2360,"startOfParagraph":true},{"content":"where you can have 1000 .c files, you compile them all to .o files,","startTime":2570780,"duration":5190,"startOfParagraph":false},{"content":"which will probably take a while, then you change 1 .c file.","startTime":2575970,"duration":4070,"startOfParagraph":false},{"content":"You only need to recompile that 1 .c file and then relink everything else,","startTime":2580040,"duration":5440,"startOfParagraph":false},{"content":"link everything back together.","startTime":2585480,"duration":2210,"startOfParagraph":false},{"content":"[student] When we're linking we write lcs50?","startTime":2589580,"duration":1850,"startOfParagraph":false},{"content":"Yeah, so -lcs50. That flag signals to the linker that you should be linking in that library.","startTime":2591430,"duration":9080,"startOfParagraph":false},{"content":"Questions?","startTime":2606680,"duration":2230,"startOfParagraph":false},{"content":"Have we gone over binary other than that 5 seconds in the first lecture?","startTime":2621310,"duration":5550,"startOfParagraph":false},{"content":"I don't think so.","startTime":2630130,"duration":2880,"startOfParagraph":false},{"content":"You should know all of the big Os that we've gone over,","startTime":2635530,"duration":3290,"startOfParagraph":false},{"content":"and you should be able to, if we gave you a function,","startTime":2638820,"duration":3850,"startOfParagraph":false},{"content":"you should be able to say it's big O, roughly. Or well, big O is rough.","startTime":2642670,"duration":6740,"startOfParagraph":false},{"content":"So if you see nested for loops looping over the same number of things,","startTime":2649410,"duration":5890,"startOfParagraph":false},{"content":"like int i, i < n; int j, j < n-- >>[student] n squared. >>it tends to be n squared.","startTime":2655300,"duration":6960,"startOfParagraph":false},{"content":"If you have triple nested, it tends to be n cubed.","startTime":2662260,"duration":3020,"startOfParagraph":false},{"content":"So that sort of thing you should be able to point out immediately.","startTime":2665280,"duration":4050,"startOfParagraph":false},{"content":"You need to know insertion sort and bubble sort and merge sort and all of those.","startTime":2669330,"duration":4560,"startOfParagraph":false},{"content":"It's easier to understand why they are those n squared and n log n and all of that","startTime":2673890,"duration":7530,"startOfParagraph":false},{"content":"because I think there was on a quiz one year where we basically gave you","startTime":2681420,"duration":6390,"startOfParagraph":false},{"content":"an implementation of bubble sort and said, \"What is the running time of this function?\"","startTime":2687810,"duration":7240,"startOfParagraph":false},{"content":"So if you recognize it as bubble sort, then you can immediately say n squared.","startTime":2695050,"duration":5970,"startOfParagraph":false},{"content":"But if you just look at it, you don't even need to realize it's bubble sort;","startTime":2701020,"duration":4450,"startOfParagraph":false},{"content":"you can just say this is doing this and this. This is n squared.","startTime":2705470,"duration":3520,"startOfParagraph":false},{"content":"[student] Are there any tough examples you can come up with,","startTime":2712350,"duration":2360,"startOfParagraph":false},{"content":"like a similar idea of figuring out?","startTime":2714710,"duration":5660,"startOfParagraph":false},{"content":"I don't think we would give you any tough examples.","startTime":2720370,"duration":4080,"startOfParagraph":true},{"content":"The bubble sort thing is about as tough as we would go,","startTime":2724450,"duration":5730,"startOfParagraph":false},{"content":"and even that, as long as you understand that you're iterating over the array","startTime":2730180,"duration":6100,"startOfParagraph":false},{"content":"for each element in the array, which is going to be something that's n squared.","startTime":2736280,"duration":5390,"startOfParagraph":false},{"content":"There are general questions, like right here we have-- Oh.","startTime":2745370,"duration":4570,"startOfParagraph":false},{"content":"Just the other day, Doug claimed, \"I have invented an algorithm that can sort an array","startTime":2755290,"duration":3240,"startOfParagraph":false},{"content":"\"of n numbers in O(log n) time!\"","startTime":2758530,"duration":3250,"startOfParagraph":false},{"content":"So how do we know that's impossible?","startTime":2761780,"duration":3120,"startOfParagraph":false},{"content":"[inaudible student response] >>Yeah.","startTime":2764900,"duration":3950,"startOfParagraph":false},{"content":"At the very least, you have to touch each element in the array,","startTime":2768850,"duration":4860,"startOfParagraph":false},{"content":"so it's impossible to sort an array of--","startTime":2773710,"duration":2500,"startOfParagraph":false},{"content":"If everything is in unsorted order, then you're going to be touching everything in the array,","startTime":2776210,"duration":4640,"startOfParagraph":false},{"content":"so it's impossible to do it in less than O of n.","startTime":2780850,"duration":4470,"startOfParagraph":false},{"content":"[student] You showed us that example of being able to do it in O of n","startTime":2787430,"duration":2910,"startOfParagraph":false},{"content":"if you use a lot of memory. >>Yeah.","startTime":2790340,"duration":3580,"startOfParagraph":false},{"content":"And that's-- I forget what that's-- Is it counting sort?","startTime":2793920,"duration":4050,"startOfParagraph":false},{"content":"Hmm. That is an integer sorting algorithm.","startTime":2807360,"duration":3970,"startOfParagraph":false},{"content":"I was looking for the special name for this that I couldn't remember last week.","startTime":2819850,"duration":5250,"startOfParagraph":false},{"content":"Yeah. These are the types of sorts that can accomplish things in big O of n.","startTime":2825100,"duration":7900,"startOfParagraph":false},{"content":"But there are limitations, like you can only use integers up to a certain number.","startTime":2833000,"duration":5430,"startOfParagraph":false},{"content":"Plus if you're trying to sort something that's--","startTime":2840870,"duration":3690,"startOfParagraph":false},{"content":"If your array is 012, -12, 151, 4 million,","startTime":2844560,"duration":6190,"startOfParagraph":false},{"content":"then that single element is going to completely ruin the entire sorting.","startTime":2850750,"duration":4370,"startOfParagraph":false},{"content":"Questions?","startTime":2862060,"duration":1970,"startOfParagraph":true},{"content":"[student] If you have a recursive function and it just makes the recursive calls","startTime":2869480,"duration":9390,"startOfParagraph":false},{"content":"within a return statement, that's tail recursive,","startTime":2878870,"duration":3360,"startOfParagraph":false},{"content":"and so would that not use more memory during runtime","startTime":2882230,"duration":5130,"startOfParagraph":false},{"content":"or it would at least use comparable memory as an iterative solution?","startTime":2887360,"duration":5190,"startOfParagraph":false},{"content":"[Bowden] Yes.","startTime":2892550,"duration":1980,"startOfParagraph":false},{"content":"It would likely be somewhat slower, but not really.","startTime":2894530,"duration":5310,"startOfParagraph":false},{"content":"Tail recursive is pretty good.","startTime":2899840,"duration":3450,"startOfParagraph":false},{"content":"Looking again at stack frames, let's say we have main ","startTime":2903290,"duration":9350,"startOfParagraph":false},{"content":"and we have int bar(int x) or something.","startTime":2912640,"duration":10280,"startOfParagraph":false},{"content":"This isn't a perfect recursive function, but return bar(x - 1).","startTime":2922920,"duration":9390,"startOfParagraph":false},{"content":"So obviously, this is flawed. You need base cases and stuff.","startTime":2932310,"duration":5310,"startOfParagraph":false},{"content":"But the idea here is that this is tail recursive,","startTime":2937620,"duration":2740,"startOfParagraph":false},{"content":"which means when main calls bar it's going to get its stack frame.","startTime":2940360,"duration":5660,"startOfParagraph":false},{"content":"In this stack frame there's going to be a little block of memory ","startTime":2949550,"duration":2890,"startOfParagraph":false},{"content":"that corresponds to its argument x.","startTime":2952440,"duration":5050,"startOfParagraph":false},{"content":"And so let's say main happens to call bar(100);","startTime":2957490,"duration":8350,"startOfParagraph":false},{"content":"So x is going to start out as 100.","startTime":2965840,"duration":4210,"startOfParagraph":false},{"content":"If the compiler recognizes that this is a tail recursive function,","startTime":2970050,"duration":5610,"startOfParagraph":false},{"content":"then when bar makes its recursive call to bar,","startTime":2975660,"duration":2880,"startOfParagraph":false},{"content":"instead of making a new stack frame, which is where the stack starts growing largely,","startTime":2978540,"duration":6950,"startOfParagraph":false},{"content":"eventually it will run into the heap and then you get segfaults ","startTime":2985490,"duration":2730,"startOfParagraph":false},{"content":"because memory starts colliding.","startTime":2988220,"duration":3370,"startOfParagraph":false},{"content":"So instead of making its own stack frame, it can realize, ","startTime":2991590,"duration":3240,"startOfParagraph":true},{"content":"hey, I never really need to come back to this stack frame,","startTime":2994830,"duration":4250,"startOfParagraph":false},{"content":"so instead I'll just replace this argument with 99 and then start bar all over.","startTime":2999080,"duration":8960,"startOfParagraph":false},{"content":"And then it will do it again and it will reach return bar(x - 1),","startTime":3008040,"duration":3770,"startOfParagraph":false},{"content":"and instead of making a new stack frame, it will just replace its current argument with 98","startTime":3011810,"duration":5510,"startOfParagraph":false},{"content":"and then jump back to the very beginning of bar.","startTime":3017320,"duration":3420,"startOfParagraph":false},{"content":"Those operations, replacing that 1 value on the stack and jumping back to the beginning,","startTime":3023860,"duration":6570,"startOfParagraph":false},{"content":"are pretty efficient.","startTime":3030430,"duration":2000,"startOfParagraph":false},{"content":"So not only is this the same memory usage as a separate function which is iterative","startTime":3032430,"duration":9070,"startOfParagraph":false},{"content":"because you're only using 1 stack frame, but you're not suffering the downsides","startTime":3041500,"duration":3890,"startOfParagraph":false},{"content":"of having to call functions.","startTime":3045390,"duration":1850,"startOfParagraph":false},{"content":"Calling functions can be somewhat expensive because it has to do all this setup","startTime":3047240,"duration":3000,"startOfParagraph":false},{"content":"and teardown and all this stuff.","startTime":3050240,"duration":2230,"startOfParagraph":false},{"content":"So this tail recursion is good.","startTime":3052470,"duration":5690,"startOfParagraph":false},{"content":"[student] Why does it not create new steps?","startTime":3058160,"duration":3010,"startOfParagraph":false},{"content":"Because it realizes it doesn't need to.","startTime":3061170,"duration":1810,"startOfParagraph":false},{"content":"The call to bar is just returning the recursive call.","startTime":3062980,"duration":4820,"startOfParagraph":false},{"content":"So it doesn't need to do anything with the return value.","startTime":3067800,"duration":4420,"startOfParagraph":false},{"content":"It's just going to immediately return it.","startTime":3072220,"duration":2900,"startOfParagraph":false},{"content":"So it's just going to replace its own argument and start over.","startTime":3075120,"duration":5410,"startOfParagraph":false},{"content":"And also, if you don't have the tail recursive version,","startTime":3080530,"duration":5250,"startOfParagraph":false},{"content":"then you get all these bars where when this bar returns","startTime":3085780,"duration":5680,"startOfParagraph":false},{"content":"it has to return its value to this one, then that bar immediately returns","startTime":3091460,"duration":4550,"startOfParagraph":false},{"content":"and it returns its value to this one, then it's just going to immediately return","startTime":3096010,"duration":3610,"startOfParagraph":false},{"content":"and return its value to this one.","startTime":3099620,"duration":1730,"startOfParagraph":false},{"content":"So you're saving this popping all of these things off of the stack","startTime":3101350,"duration":4000,"startOfParagraph":false},{"content":"since the return value is just going to be passed all the way back up anyway.","startTime":3105350,"duration":3380,"startOfParagraph":false},{"content":"So why not just replace our argument with the updated argument and start over?","startTime":3108730,"duration":6670,"startOfParagraph":false},{"content":"If the function is not tail recursive, if you do something like--","startTime":3117460,"duration":3690,"startOfParagraph":false},{"content":"[student] if bar(x + 1). >>Yeah.","startTime":3121150,"duration":6380,"startOfParagraph":false},{"content":"So if you put it in condition, then you're doing something with the return value.","startTime":3127530,"duration":4240,"startOfParagraph":true},{"content":"Or even if you just do return 2 * bar(x - 1).","startTime":3131770,"duration":4490,"startOfParagraph":false},{"content":"So now bar(x - 1) needs to return in order for it to calculate 2 times that value,","startTime":3136260,"duration":7300,"startOfParagraph":false},{"content":"so now it does need its own separate stack frame, ","startTime":3143560,"duration":2580,"startOfParagraph":false},{"content":"and now, no matter how hard you try, you're going to need to--","startTime":3146140,"duration":5040,"startOfParagraph":false},{"content":"This isn't tail recursive.","startTime":3151180,"duration":3230,"startOfParagraph":false},{"content":"[student] Would I try to bring a recursion to aim for a tail recursion--","startTime":3154410,"duration":3180,"startOfParagraph":false},{"content":"[Bowden] In an ideal world, but in CS50 you don't have to.","startTime":3157590,"duration":3860,"startOfParagraph":false},{"content":"In order to get tail recursion, generally, you set up an additional argument","startTime":3163780,"duration":5500,"startOfParagraph":false},{"content":"where bar will take int x into y ","startTime":3169280,"duration":4270,"startOfParagraph":false},{"content":"and y corresponds to the ultimate thing you want to return.","startTime":3173550,"duration":3440,"startOfParagraph":false},{"content":"So then this you're going to be returning bar(x - 1), 2 * y.","startTime":3176990,"duration":6660,"startOfParagraph":false},{"content":"So that's just a high-level how you transform things to be tail recursive.","startTime":3183650,"duration":6160,"startOfParagraph":false},{"content":"But the extra argument--","startTime":3189810,"duration":3980,"startOfParagraph":false},{"content":"And then in the end when you reach your base case, you just return y","startTime":3193790,"duration":3620,"startOfParagraph":false},{"content":"because you've been accumulating the entire time the return value that you want.","startTime":3197410,"duration":5330,"startOfParagraph":false},{"content":"You kind of have been doing it iteratively but using recursive calls.","startTime":3202740,"duration":4540,"startOfParagraph":false},{"content":"Questions?","startTime":3212510,"duration":2390,"startOfParagraph":false},{"content":"[student] Maybe about pointer arithmetic, like when using strings. >>Sure.","startTime":3214900,"duration":4990,"startOfParagraph":false},{"content":"Pointer arithmetic. ","startTime":3219890,"duration":3720,"startOfParagraph":false},{"content":"When using strings it's easy because strings are char stars,","startTime":3223610,"duration":4830,"startOfParagraph":false},{"content":"chars are forever and always a single byte,","startTime":3228440,"duration":3420,"startOfParagraph":false},{"content":"and so pointer arithmetic is equivalent to regular arithmetic when you're dealing with strings.","startTime":3231860,"duration":5680,"startOfParagraph":false},{"content":"Let's just say char* s = \"hello\".","startTime":3237540,"duration":11250,"startOfParagraph":false},{"content":"So we have a block in memory.","startTime":3248790,"duration":2640,"startOfParagraph":false},{"content":"It needs 6 bytes because you always need the null terminator. ","startTime":3259490,"duration":2890,"startOfParagraph":false},{"content":"And char* s is going to point to the beginning of this array.","startTime":3262380,"duration":6240,"startOfParagraph":false},{"content":"So s points there.","startTime":3268620,"duration":4210,"startOfParagraph":false},{"content":"Now, this is basically how any array works,","startTime":3272830,"duration":3880,"startOfParagraph":false},{"content":"regardless of whether it was a return by malloc or whether it's on the stack.","startTime":3276710,"duration":4070,"startOfParagraph":false},{"content":"Any array is basically a pointer to the start of the array,","startTime":3280780,"duration":6330,"startOfParagraph":false},{"content":"and then any array operation, any indexing, is just going into that array a certain offset.","startTime":3287110,"duration":6530,"startOfParagraph":false},{"content":"So when I say something like s[3]; this is going to s and counting 3 chars in.","startTime":3293640,"duration":11720,"startOfParagraph":true},{"content":"So s[3], we have 0, 1, 2, 3, so s[3] is going to refer to this l.","startTime":3305360,"duration":7130,"startOfParagraph":false},{"content":"[student] And we could reach the same value by doing s + 3 and then parentheses star?","startTime":3312490,"duration":7970,"startOfParagraph":false},{"content":"Yes.","startTime":3320460,"duration":2110,"startOfParagraph":false},{"content":"This is equivalent to *(s + 3);","startTime":3322570,"duration":3440,"startOfParagraph":false},{"content":"and that is forever and always equivalent no matter what you do.","startTime":3326010,"duration":5230,"startOfParagraph":false},{"content":"You never need to use the bracket syntax.","startTime":3331240,"duration":2830,"startOfParagraph":false},{"content":"You can always use the *(s + 3) syntax.","startTime":3334070,"duration":3700,"startOfParagraph":false},{"content":"People tend to like the bracket syntax, though.","startTime":3337770,"duration":2410,"startOfParagraph":false},{"content":"[student] So all arrays are actually just pointers.","startTime":3340180,"duration":3680,"startOfParagraph":false},{"content":"There is a slight distinction when I say int x[4]; >>[student] Does that create the memory?","startTime":3343860,"duration":9770,"startOfParagraph":false},{"content":"[Bowden] That is going to create 4 ints on the stack, so 16 bytes overall.","startTime":3353630,"duration":9690,"startOfParagraph":false},{"content":"It's going to create 16 bytes on the stack.","startTime":3363320,"duration":2380,"startOfParagraph":false},{"content":"x isn't stored anywhere.","startTime":3365700,"duration":3490,"startOfParagraph":false},{"content":"It is just a symbol referring to the start of the thing.","startTime":3369190,"duration":4230,"startOfParagraph":false},{"content":"Because you declared the array inside of this function,","startTime":3373420,"duration":4260,"startOfParagraph":false},{"content":"what the compiler is going to do is just replace all instances of the variable x","startTime":3377680,"duration":4660,"startOfParagraph":false},{"content":"with where it happened to choose to put these 16 bytes.","startTime":3382340,"duration":4060,"startOfParagraph":false},{"content":"It can't do that with char* s because s is an actual pointer.","startTime":3386400,"duration":3640,"startOfParagraph":false},{"content":"It is free to then point to other things.","startTime":3390040,"duration":2340,"startOfParagraph":false},{"content":"x is a constant. You can't have it point to a different array. >>[student] Okay.","startTime":3392380,"duration":3760,"startOfParagraph":false},{"content":"But this idea, this indexing, is the same regardless of whether it's a traditional array","startTime":3396140,"duration":7280,"startOfParagraph":false},{"content":"or if it's a pointer to something or if it's a pointer to a malloced array.","startTime":3403420,"duration":4810,"startOfParagraph":false},{"content":"And in fact, it is so equivalent that that is also the same thing.","startTime":3408230,"duration":11540,"startOfParagraph":false},{"content":"It actually just translates what's inside of the brackets and what's left of the brackets,","startTime":3419770,"duration":5670,"startOfParagraph":false},{"content":"adds them together, and dereferences.","startTime":3425440,"duration":2530,"startOfParagraph":false},{"content":"So this is just as valid as *(s + 3) or s[3].","startTime":3427970,"duration":6740,"startOfParagraph":false},{"content":"[student] Can you have pointers pointing to 2-dimensional arrays?","startTime":3436210,"duration":5880,"startOfParagraph":false},{"content":"It's harder. Traditionally, no.","startTime":3442090,"duration":5290,"startOfParagraph":true},{"content":"A 2-dimensional array is just a 1-dimensional array with some convenient syntax","startTime":3447380,"duration":7340,"startOfParagraph":false},{"content":"because when I say int x[3][3], this is really just 1 array with 9 values.","startTime":3454720,"duration":19390,"startOfParagraph":false},{"content":"And so when I index, the compiler knows what I mean. ","startTime":3475500,"duration":7500,"startOfParagraph":false},{"content":"If I say x[1][2], it knows I want to go to the second row, so it's going to skip the first 3,","startTime":3483000,"duration":10090,"startOfParagraph":false},{"content":"and then it wants the second thing in that, so it's going to get this one.","startTime":3493090,"duration":4370,"startOfParagraph":false},{"content":"But it is still just a single-dimensional array.","startTime":3497460,"duration":3020,"startOfParagraph":false},{"content":"And so if I wanted to assign a pointer to that array,","startTime":3500480,"duration":3180,"startOfParagraph":false},{"content":"I would say int *p = x;","startTime":3503660,"duration":6110,"startOfParagraph":false},{"content":"The type of x is just--","startTime":3509770,"duration":3450,"startOfParagraph":false},{"content":"It's rough saying type of x since it is just a symbol and it's not an actual variable,","startTime":3513220,"duration":5060,"startOfParagraph":false},{"content":"but it is just an int *.","startTime":3518280,"duration":1860,"startOfParagraph":false},{"content":"x is just a pointer to the start of this. >>[student] Okay.","startTime":3520140,"duration":4700,"startOfParagraph":false},{"content":"And so I won't be able to access [1][2].","startTime":3524840,"duration":7720,"startOfParagraph":false},{"content":"I think there is special syntax for declaring a pointer,","startTime":3532560,"duration":5810,"startOfParagraph":false},{"content":"something ridiculous like int (*p[--something absolutely ridiculous. I don't even know.","startTime":3538370,"duration":14110,"startOfParagraph":false},{"content":"But there is a syntax for declaring pointers like with parentheses and things.","startTime":3552480,"duration":4610,"startOfParagraph":false},{"content":"It may not even let you do that.","startTime":3557090,"duration":5870,"startOfParagraph":false},{"content":"I could look back at something that would tell me the truth.","startTime":3562960,"duration":3680,"startOfParagraph":false},{"content":"I will look for it later, if there is a syntax for point. But you will never see it.","startTime":3566640,"duration":7520,"startOfParagraph":false},{"content":"And even the syntax is so archaic that if you use it, people will be baffled.","startTime":3574160,"duration":5510,"startOfParagraph":false},{"content":"Multidimensional arrays are pretty rare as it is.","startTime":3579670,"duration":3870,"startOfParagraph":false},{"content":"You pretty much--","startTime":3583540,"duration":1090,"startOfParagraph":false},{"content":"Well, if you're doing matrix things it's not going to be rare,","startTime":3584630,"duration":3860,"startOfParagraph":false},{"content":"but in C you're rarely going to be using multidimensional arrays.","startTime":3588490,"duration":8240,"startOfParagraph":false},{"content":"Yeah. >>[student] Let's say you have a really long array.","startTime":3597630,"duration":2840,"startOfParagraph":false},{"content":"So in virtual memory it would appear to be all consecutive,","startTime":3600470,"duration":3430,"startOfParagraph":true},{"content":"like the elements right next to each other,","startTime":3603900,"duration":1740,"startOfParagraph":false},{"content":"but in the physical memory, would it be possible for that to be split up? >>Yes.","startTime":3605640,"duration":3130,"startOfParagraph":false},{"content":"How virtual memory works is it just separates--","startTime":3608770,"duration":8090,"startOfParagraph":false},{"content":"The unit of allocation is a page, which tends to be 4 kilobytes,","startTime":3619220,"duration":5640,"startOfParagraph":false},{"content":"and so when a process says, hey, I want to use this memory,","startTime":3624860,"duration":4820,"startOfParagraph":false},{"content":"the operating system is going to allocate it 4 kilobytes for that little block of memory.","startTime":3629680,"duration":6290,"startOfParagraph":false},{"content":"Even if you only use a single little byte in the entire block of memory,","startTime":3635970,"duration":3130,"startOfParagraph":false},{"content":"the operating system is going to give it the full 4 kilobytes.","startTime":3639100,"duration":3750,"startOfParagraph":false},{"content":"So what this means is I could have--let's say this is my stack.","startTime":3642850,"duration":6560,"startOfParagraph":false},{"content":"This stack could be separated. My stack could be megabytes and megabytes.","startTime":3649410,"duration":3770,"startOfParagraph":false},{"content":"My stack could be huge.","startTime":3653180,"duration":1840,"startOfParagraph":false},{"content":"But the stack itself has to be split into individual pages,","startTime":3655020,"duration":5200,"startOfParagraph":false},{"content":"which if we look at over here let's say this is our RAM,","startTime":3660220,"duration":8790,"startOfParagraph":false},{"content":"if I have 2 gigabytes of RAM, this is actual address 0 like the zeroth byte of my RAM,","startTime":3669010,"duration":7590,"startOfParagraph":false},{"content":"and this is 2 gigabytes all the way down here.","startTime":3676600,"duration":5610,"startOfParagraph":false},{"content":"So this page might correspond to this block over here.","startTime":3682210,"duration":5020,"startOfParagraph":false},{"content":"This page might correspond to this block over here.","startTime":3687230,"duration":2170,"startOfParagraph":false},{"content":"This one might correspond to this one over here.","startTime":3689400,"duration":2160,"startOfParagraph":false},{"content":"So the operating system is free to assign physical memory","startTime":3691560,"duration":3980,"startOfParagraph":false},{"content":"to any individual page arbitrarily.","startTime":3695540,"duration":3780,"startOfParagraph":false},{"content":"And that means that if this border happens to straddle an array,","startTime":3699320,"duration":6860,"startOfParagraph":false},{"content":"an array happens to be left of this and right of this order of a page,","startTime":3706180,"duration":3890,"startOfParagraph":false},{"content":"then that array is going to be split in physical memory.","startTime":3710070,"duration":4390,"startOfParagraph":false},{"content":"And then when you quit the program, when the process ends,","startTime":3714460,"duration":4820,"startOfParagraph":false},{"content":"these mappings get erased and then it's free to use these little blocks for other things.","startTime":3719280,"duration":6410,"startOfParagraph":false},{"content":"More questions?","startTime":3734730,"duration":2680,"startOfParagraph":false},{"content":"[student] The pointer arithmetic. >>Oh yeah.","startTime":3737410,"duration":2550,"startOfParagraph":false},{"content":"Strings were easier, but looking at something like ints,","startTime":3739960,"duration":8450,"startOfParagraph":false},{"content":"so back to int x[4];","startTime":3748410,"duration":6590,"startOfParagraph":false},{"content":"Whether this is an array or whether it's a pointer to a malloced array of 4 integers,","startTime":3755000,"duration":6810,"startOfParagraph":false},{"content":"it's going to be treated the same way.","startTime":3761810,"duration":5250,"startOfParagraph":false},{"content":"[student] So arrays are on the heap?","startTime":3770590,"duration":2750,"startOfParagraph":false},{"content":"[Bowden] Arrays are not on the heap. >>[student] Oh.","startTime":3781400,"duration":3870,"startOfParagraph":false},{"content":"[Bowden] This type of array tends to be on the stack","startTime":3785270,"duration":3050,"startOfParagraph":true},{"content":"unless you declared it at--ignoring global variables. Don't use global variables.","startTime":3788320,"duration":3900,"startOfParagraph":false},{"content":"Inside of a function I say int x[4];","startTime":3792220,"duration":4060,"startOfParagraph":false},{"content":"It's going to create a 4-integer block on the stack for this array.","startTime":3796280,"duration":6240,"startOfParagraph":false},{"content":"But this malloc(4 * sizeof(int)); is going to go on the heap.","startTime":3802520,"duration":4440,"startOfParagraph":false},{"content":"But after this point I can use x and p in pretty much the same ways,","startTime":3806960,"duration":4910,"startOfParagraph":false},{"content":"other than the exceptions I said before about you can reassign p.","startTime":3811870,"duration":4270,"startOfParagraph":false},{"content":"Technically, their sizes are somewhat different, but that's completely irrelevant.","startTime":3816140,"duration":4820,"startOfParagraph":false},{"content":"You never actually use their sizes.","startTime":3820960,"duration":2350,"startOfParagraph":false},{"content":"The p I could say p[3] = 2; or x[3] = 2;","startTime":3828020,"duration":8790,"startOfParagraph":false},{"content":"You can use them in exactly the same ways.","startTime":3836810,"duration":2870,"startOfParagraph":false},{"content":"So pointer arithmetic now-- Yes.","startTime":3839680,"duration":1890,"startOfParagraph":false},{"content":"[student] Do you not have to do p* if you have the brackets?","startTime":3841570,"duration":5820,"startOfParagraph":false},{"content":"The brackets are an implicit dereference. >>Okay.","startTime":3847390,"duration":4330,"startOfParagraph":false},{"content":"Actually, also what you're saying with the can you get multidimensional arrays","startTime":3851720,"duration":8480,"startOfParagraph":false},{"content":"with pointers, what you can do is something like, let's say, int **pp = malloc(sizeof(int*) * 5);","startTime":3860200,"duration":42450,"startOfParagraph":false},{"content":"I'll just write it all out first.","startTime":3902650,"duration":4250,"startOfParagraph":false},{"content":"I did not want that one.","startTime":3937880,"duration":3140,"startOfParagraph":false},{"content":"Okay.","startTime":3941020,"duration":1530,"startOfParagraph":false},{"content":"What I did here is-- That should be pp[i].","startTime":3942550,"duration":6360,"startOfParagraph":false},{"content":"So pp is a pointer to a pointer.","startTime":3948910,"duration":4770,"startOfParagraph":false},{"content":"You're mallocing pp to point to an array of 5 int stars.","startTime":3953680,"duration":8740,"startOfParagraph":false},{"content":"So in memory you have on the stack pp.","startTime":3962420,"duration":8530,"startOfParagraph":false},{"content":"It's going to point to an array of 5 blocks which are all themselves pointers.","startTime":3970950,"duration":9200,"startOfParagraph":false},{"content":"And then when I malloc down here, I malloc that each of those individual pointers","startTime":3980150,"duration":8060,"startOfParagraph":false},{"content":"should point to a separate block of 4 bytes on the heap.","startTime":3988210,"duration":3870,"startOfParagraph":false},{"content":"So this points to 4 bytes.","startTime":3992080,"duration":3790,"startOfParagraph":false},{"content":"And this one points to a different 4 bytes.","startTime":3997940,"duration":2720,"startOfParagraph":false},{"content":"And all of them point to their own 4 bytes.","startTime":4000660,"duration":2540,"startOfParagraph":true},{"content":"This gives me a way of doing multidimensional things.","startTime":4003200,"duration":5880,"startOfParagraph":false},{"content":"I could say pp[3][4], but now this is not the same thing as multidimensional arrays","startTime":4009080,"duration":8950,"startOfParagraph":false},{"content":"because multidimensional arrays it translated [3][4] into a single offset into the x array.","startTime":4018030,"duration":7360,"startOfParagraph":false},{"content":"This dereferences p, accesses the third index, then dereferences that","startTime":4025390,"duration":9400,"startOfParagraph":false},{"content":"and accesses--4 would be invalid--the second index.","startTime":4034790,"duration":6000,"startOfParagraph":false},{"content":"Whereas when we had the int x[3][4] before as a multidimensional array","startTime":4044770,"duration":6660,"startOfParagraph":false},{"content":"and when you double bracket it's really only a single dereference,","startTime":4051430,"duration":4310,"startOfParagraph":false},{"content":"you're following a single pointer and then an offset,","startTime":4055740,"duration":4750,"startOfParagraph":false},{"content":"this is really 2D references.","startTime":4060490,"duration":2360,"startOfParagraph":false},{"content":"You follow 2 separate pointers.","startTime":4062850,"duration":2990,"startOfParagraph":false},{"content":"So this also technically allows you to have multidimensional arrays","startTime":4065840,"duration":4580,"startOfParagraph":false},{"content":"where each individual array is different sizes.","startTime":4070420,"duration":3130,"startOfParagraph":false},{"content":"So I think jagged multidimensional arrays is what it's called","startTime":4073550,"duration":4450,"startOfParagraph":false},{"content":"since really the first thing could point to something that has 10 elements,","startTime":4078000,"duration":3870,"startOfParagraph":false},{"content":"the second thing could point to something that has 100 elements.","startTime":4081870,"duration":3670,"startOfParagraph":false},{"content":"[student] Is there any limit to the number of pointers you can have ","startTime":4085540,"duration":5250,"startOfParagraph":false},{"content":"pointing to other pointers? >>No.","startTime":4090790,"duration":3500,"startOfParagraph":false},{"content":"You can have int *****p.","startTime":4094290,"duration":2720,"startOfParagraph":false},{"content":"Back to pointer arithmetic-- >>[student] Oh. >>Yeah.","startTime":4098050,"duration":5710,"startOfParagraph":false},{"content":"[student] If I have int ***p and then I do a dereferencing and I say p* is equal to this value,","startTime":4103760,"duration":11889,"startOfParagraph":false},{"content":"is it only going to do 1 level of dereferencing? >>Yes.","startTime":4115649,"duration":3911,"startOfParagraph":false},{"content":"So if I want to access the thing that the last pointer is pointing at--","startTime":4119560,"duration":3780,"startOfParagraph":false},{"content":"Then you do ***p. >>Okay.","startTime":4123340,"duration":2870,"startOfParagraph":false},{"content":"So this is p points to 1 block, points to another block, points to another block.","startTime":4126210,"duration":7870,"startOfParagraph":false},{"content":"Then if you do *p = something else, then you are changing this","startTime":4134080,"duration":7930,"startOfParagraph":false},{"content":"to now point to a different block. >>Okay.","startTime":4142010,"duration":11630,"startOfParagraph":false},{"content":"[Bowden] And if these were malloced, then you have now leaked memory","startTime":4153640,"duration":4009,"startOfParagraph":true},{"content":"unless you happen to have different references of these ","startTime":4157649,"duration":2781,"startOfParagraph":false},{"content":"since you can't get back to those ones that you just threw away.","startTime":4160430,"duration":4840,"startOfParagraph":false},{"content":"Pointer arithmetic.","startTime":4165270,"duration":4280,"startOfParagraph":false},{"content":"int x[4]; is going to allocate an array of 4 integers","startTime":4169550,"duration":6760,"startOfParagraph":false},{"content":"where x is going to point to the beginning of the array.","startTime":4176310,"duration":4360,"startOfParagraph":false},{"content":"So when I say something like x[1]; I want it to mean go to the second integer in the array,","startTime":4180670,"duration":9750,"startOfParagraph":false},{"content":"which would be this one.","startTime":4190420,"duration":2899,"startOfParagraph":false},{"content":"But really, that's 4 bytes into the array since this integer takes up 4 bytes.","startTime":4193319,"duration":10871,"startOfParagraph":false},{"content":"So an offset of 1 really means an offset of 1 ","startTime":4204190,"duration":4280,"startOfParagraph":false},{"content":"times the size of whatever the type of the array is.","startTime":4208470,"duration":3560,"startOfParagraph":false},{"content":"This is an array of integers, so it knows to do 1 times size of int when it wants to offset.","startTime":4212030,"duration":5140,"startOfParagraph":false},{"content":"The other syntax. Remember that this is equivalent to *(x + 1);","startTime":4217170,"duration":8090,"startOfParagraph":false},{"content":"When I say pointer + 1, what that returns is the address that the pointer is storing","startTime":4225260,"duration":9990,"startOfParagraph":false},{"content":"plus 1 times the size of the type of the pointer.","startTime":4235250,"duration":5110,"startOfParagraph":false},{"content":"So if x = ox100, then x + 1 = ox104.","startTime":4240360,"duration":19150,"startOfParagraph":false},{"content":"And you can abuse this and say something like char* c = (char*)x;","startTime":4259510,"duration":20240,"startOfParagraph":false},{"content":"and now c is going to be the same address as x.","startTime":4279750,"duration":3300,"startOfParagraph":false},{"content":"c is going to be equal to ox100,","startTime":4283050,"duration":2990,"startOfParagraph":false},{"content":"but c + 1 is going to be equal to ox101","startTime":4286040,"duration":5450,"startOfParagraph":false},{"content":"since pointer arithmetic depends on the type of the pointer that you are adding to.","startTime":4291490,"duration":6540,"startOfParagraph":false},{"content":"So c + 1, it looks at c, it's a char pointer, so it's going to add 1 times size of char,","startTime":4298030,"duration":7360,"startOfParagraph":false},{"content":"which is always going to be 1, so you get 101,","startTime":4305390,"duration":2720,"startOfParagraph":false},{"content":"whereas if I do x, which is also still 100, x + 1 is going to be 104.","startTime":4308110,"duration":6780,"startOfParagraph":false},{"content":"[student] Can you use c++ in order to advance your pointer by 1?","startTime":4316660,"duration":9680,"startOfParagraph":false},{"content":"Yes, you can.","startTime":4326340,"duration":3470,"startOfParagraph":false},{"content":"You can't do that with x because x is just a symbol, it is a constant; you can't change x.","startTime":4329810,"duration":6370,"startOfParagraph":false},{"content":"But c happens to just be a pointer, so c++ is perfectly valid and it will increment by 1.","startTime":4336180,"duration":6430,"startOfParagraph":true},{"content":"If c were just an int *, then c++ would be 104.","startTime":4342610,"duration":9830,"startOfParagraph":false},{"content":"++ does pointer arithmetic just as c + 1 would have done pointer arithmetic.","startTime":4352440,"duration":8810,"startOfParagraph":false},{"content":"This is actually how a lot of things like merge sort--","startTime":4363000,"duration":5870,"startOfParagraph":false},{"content":"Instead of creating copies of things, you can instead pass--","startTime":4369670,"duration":6040,"startOfParagraph":false},{"content":"Like if I wanted to pass this half of the array--let's erase some of this.","startTime":4375710,"duration":6690,"startOfParagraph":false},{"content":"Let's say I wanted to pass this side of the array into a function.","startTime":4384770,"duration":5750,"startOfParagraph":false},{"content":"What would I pass to that function?","startTime":4390520,"duration":2180,"startOfParagraph":false},{"content":"If I pass x, I am passing this address.","startTime":4392700,"duration":4350,"startOfParagraph":false},{"content":"But I want to pass this particular address. So what should I pass?","startTime":4397050,"duration":6730,"startOfParagraph":false},{"content":"[student] Pointer + 2?","startTime":4403780,"duration":2810,"startOfParagraph":false},{"content":"[Bowden] So x + 2. Yes.","startTime":4406590,"duration":2760,"startOfParagraph":false},{"content":"That's going to be this address.","startTime":4409350,"duration":2270,"startOfParagraph":false},{"content":"You'll also very frequently see it as x[2] and then the address of that.","startTime":4411620,"duration":11190,"startOfParagraph":false},{"content":"So you need to take the address of it because the bracket is an implicit dereference.","startTime":4422810,"duration":5040,"startOfParagraph":false},{"content":"x[2] refers to the value that is in this box, and then you want the address of that box,","startTime":4427850,"duration":5400,"startOfParagraph":false},{"content":"so you say &x[2].","startTime":4433250,"duration":3600,"startOfParagraph":false},{"content":"So that's how something in merge sort where you want to pass half the list to something","startTime":4436850,"duration":6030,"startOfParagraph":false},{"content":"you really just pass &x[2], and now as far as the recursive call is concerned,","startTime":4442880,"duration":5910,"startOfParagraph":false},{"content":"my new array starts there.","startTime":4448790,"duration":3720,"startOfParagraph":false},{"content":"Last minute questions.","startTime":4452510,"duration":2620,"startOfParagraph":false},{"content":"[student] If we don't put an ampersand or a--what's that called? >>Star?","startTime":4455130,"duration":4920,"startOfParagraph":false},{"content":"[student] Star. >>Technically, dereference operator, but-- >>[student] Dereference.","startTime":4460050,"duration":3150,"startOfParagraph":false},{"content":"If we don't put a star or an ampersand, what happens if I just say y = x and x is a pointer?","startTime":4463200,"duration":6110,"startOfParagraph":true},{"content":"What is the type of y? >>[student] I'll just say it's pointer 2.","startTime":4469310,"duration":5310,"startOfParagraph":false},{"content":"So if you just say y = x, now x and y point to the same thing. >>[student] Point to the same thing.","startTime":4474620,"duration":3650,"startOfParagraph":false},{"content":"And if x is an int pointer? >>It would complain because you can't assign pointers.","startTime":4478270,"duration":6910,"startOfParagraph":false},{"content":"[student] Okay. ","startTime":4485180,"duration":1360,"startOfParagraph":false},{"content":"Remember that pointers, even though we draw them as arrows,","startTime":4486540,"duration":5320,"startOfParagraph":false},{"content":"really all they store--int *x--really all x is storing is something like ox100,","startTime":4491860,"duration":10150,"startOfParagraph":false},{"content":"which we happen to represent as pointing to the block stored at 100.","startTime":4502010,"duration":4480,"startOfParagraph":false},{"content":"So when I say int *y = x; I'm just copying ox100 into y,","startTime":4506490,"duration":13170,"startOfParagraph":false},{"content":"which we're just going to represent as y, also pointing to ox100.","startTime":4519660,"duration":4970,"startOfParagraph":false},{"content":"And if I say int i = (int)x; then i is going to store whatever the value of ox100 is","startTime":4524630,"duration":15180,"startOfParagraph":false},{"content":"inside of it, but now it's going to be interpreted as an integer instead of a pointer.","startTime":4539810,"duration":5290,"startOfParagraph":false},{"content":"But you need the cast or else it will complain.","startTime":4545100,"duration":4210,"startOfParagraph":false},{"content":"[student] So do you mean to cast--","startTime":4549310,"duration":3990,"startOfParagraph":false},{"content":"Is it going to be casting int of x or casting int of y?","startTime":4553300,"duration":6990,"startOfParagraph":false},{"content":"[Bowden] What? ","startTime":4560290,"duration":3410,"startOfParagraph":false},{"content":"[student] Okay. After these parentheses is there going to be an x or a y there?","startTime":4563700,"duration":3990,"startOfParagraph":false},{"content":"[Bowden] Either. x and y are equivalent. >>[student] Okay.","startTime":4567690,"duration":3810,"startOfParagraph":true},{"content":"Because they're both pointers. >>Yeah.","startTime":4571500,"duration":2890,"startOfParagraph":false},{"content":"[student] So it would store the hexadecimal 100 in integer form? >>[Bowden] Yeah.","startTime":4574390,"duration":6660,"startOfParagraph":false},{"content":"But not the value of whatever it points to.","startTime":4581050,"duration":2570,"startOfParagraph":false},{"content":"[Bowden] Yeah. >>[student] So just the address in integer form. Okay.","startTime":4583620,"duration":6320,"startOfParagraph":false},{"content":"[Bowden] If you wanted to for some bizarre reason,","startTime":4589940,"duration":4780,"startOfParagraph":false},{"content":"you could exclusively deal with pointers and never deal with integers","startTime":4594720,"duration":4180,"startOfParagraph":false},{"content":"and just be like int *x = 0.","startTime":4598900,"duration":10340,"startOfParagraph":false},{"content":"Then you're going to get really confused once pointer arithmetic starts happening.","startTime":4609240,"duration":3760,"startOfParagraph":false},{"content":"So the numbers that they store are meaningless.","startTime":4613000,"duration":3570,"startOfParagraph":false},{"content":"It's just how you end up interpreting them.","startTime":4616570,"duration":2370,"startOfParagraph":false},{"content":"So I'm free to copy ox100 from an int * to an int,","startTime":4618940,"duration":3980,"startOfParagraph":false},{"content":"and I'm free to assign--you're probably going to get yelled at for not casting--","startTime":4622920,"duration":4870,"startOfParagraph":false},{"content":"I'm free to assign something like (int *)ox1234 into this arbitrary int *.","startTime":4627790,"duration":10370,"startOfParagraph":false},{"content":"So ox123 is just as valid a memory address as is &y.","startTime":4638160,"duration":7320,"startOfParagraph":false},{"content":"&y happens to return something that is pretty much ox123.","startTime":4645480,"duration":6580,"startOfParagraph":false},{"content":"[student] Would that be a really cool way to go from hexadecimal to decimal form,","startTime":4652060,"duration":3370,"startOfParagraph":false},{"content":"like if you have a pointer and you cast it as an int?","startTime":4655430,"duration":3800,"startOfParagraph":false},{"content":"[Bowden] You can really just print using like printf.","startTime":4659230,"duration":5630,"startOfParagraph":false},{"content":"Let's say I have int y = 100.","startTime":4664860,"duration":5440,"startOfParagraph":false},{"content":"So printf(%d\\n--as you should already know--print that as an integer, %x.","startTime":4670300,"duration":12400,"startOfParagraph":false},{"content":"We'll just print it as hexadecimal.","startTime":4682700,"duration":2490,"startOfParagraph":false},{"content":"So a pointer is not stored as hexadecimal,","startTime":4685190,"duration":5570,"startOfParagraph":false},{"content":"and an integer is not stored as decimal.","startTime":4690760,"duration":2200,"startOfParagraph":false},{"content":"Everything is stored as binary.","startTime":4692960,"duration":1740,"startOfParagraph":false},{"content":"It's just that we tend to show pointers as hexadecimal","startTime":4694700,"duration":3250,"startOfParagraph":false},{"content":"because we think of things in these 4-byte blocks,","startTime":4697950,"duration":5310,"startOfParagraph":false},{"content":"and memory addresses tend to be familiar.","startTime":4703260,"duration":2130,"startOfParagraph":false},{"content":"We're like, if it starts with bf, then it happens to be on the stack.","startTime":4705390,"duration":3500,"startOfParagraph":false},{"content":"So it's just our interpretation of pointers as hexadecimal.","startTime":4708890,"duration":6670,"startOfParagraph":false},{"content":"Okay. Any last questions?","startTime":4715560,"duration":3640,"startOfParagraph":false},{"content":"I'll be here for a bit after if you have anything else.","startTime":4719200,"duration":2500,"startOfParagraph":true},{"content":"And that's the end of that.","startTime":4721700,"duration":4370,"startOfParagraph":false},{"content":"[student] Yay! [applause]","startTime":4726070,"duration":2290,"startOfParagraph":true},{"content":"[CS50.TV]","startTime":4731440,"duration":1560,"startOfParagraph":true}]}