 Cool. All right, so we're here in office hours November 16th two days before the midterm two So we're talking about how to draw the stack out for instance here at location one So we have a function main that defines an array. It's called C of 0 5 and 10 has another local variable D Which hat is calling foo and passing in C of a it calls foo foo does a bunch of stuff Then we're printing out a C and D inside foo foo sets a to be zero A is gonna be a plus one B plus is equal to B plus one if B is less than 20 then we call foo again Otherwise we go to location one and we're gonna call Return B from foo. So the question is how to draw the stack at location one, right? Yeah before we begin Is the assumption that we're doing pass by value always on this one's unless otherwise It should always be identified if it's not identified you should ask okay, so yes pass by value Because I actually yeah, that would kind of change how you drew the stack. Yeah So I would say you're probably safe and saying that will not be on there, but You never know throw a little curve balls in there Okay, so we know what's the top most function frame that's gonna be on the stack Main right because that's where we operate I want to something on the stack Does main have any parameters? So what things go on the stack? Parameters and what kind of variables? Local variables exactly right local to the function Exactly, okay, so what local variables do we have here? In C and in D. There are two local variables question No, so where would the global go if we were tracking the entire stack would it just go up in the stack of above main? Clearly off the stack not in the stack. I mean not well It's in memory somewhere that's a memory wise would it be some it's technically below It's below the heap by the code segment, but Oh, yeah, so that's why we don't draw it because it's really not in what we care about for the stack Exactly, so yeah, so this a to write that sets a somewhere wherever it is that global variable to be to Because right the stack is always changing but a the location of a never changes throughout the entire program execution So therefore we're never gonna change right All right, so we have C and D as the as the variables inside of main So we know we have C and C is an array. So we'll just kind of draw it like we have it here. So it's 0 510 Then we have D Or maybe all You have D. Do we know what the value of D is? No, right because we're gonna call foo Right, so we have no idea what the value of D is Okay, we're gonna call foo and we're doing pass by value. So we have C of a what's the value CA? What's a? to Exactly to and then what's C to? 10 So we're gonna call function foo and pass in 10. So this is all Main's function frame right right now No, we're going over an example of function frames Here, okay, so we call foo So now we have a new function frame on here. Does foo have any parameters? B. What's the value now of B? 10 because that's what we passed in exactly. So that's gonna be 10 How many local variables? Exactly a Which has the value zero at the start Cool, and then we just go through and execute this. So we say okay a is equal to a plus one So which a is this? Internal a not this global a right exactly. So we said a is equal to a plus one or change that to be one B is equal to b plus one So it's gonna be what 11 should we show that it changes or we just give you the final result All I want is the final result, but yeah, it's good if you show what changes because that would be That'll be good. Yeah, it'll help us try and now that I see I Made a little mistake here. We're gonna change this to be like 12 13 it was 20 now it's 13 Yes, exactly because I don't want to draw those stacks Okay, so we just incremented B now we say is B less than 13 Yeah, so now we're gonna call a function foo and pass in what for its parameter B 11 perfect. So we know we're making a function call So we know this is is which function frame All right. Now. We know we have a new function frame. It has its parameter What's the name of its parameter? D. What's the value and then has a local variable value zero so this is This next invocation of foo and So it sets a to zero which we're done it increments a So it says one and increments B. So now it's 12 That says this be less than 13 Yep, so we're gonna go in here Calling foo again So as every see why this statement here when we're in this function frame of this B Does not affect this B Right because we have passed by value here So this parameter is a brand new copy of that variable inside this call for foos B So then B, okay, so we just called foo again So now what's this so we have we know we have the parameter B and the local variable a so What's the value of the parameter B tool and the value of a? Zero increment a Increment B. I remember we like increment this be not any of these other bees, right because it's just Because we're only this foo can only access the variables here This be here to 13. We say is B less than 13 No, so we go to the other one. We say location one. Bam. So this is our stack So we only do it up to location Exactly. So on the first execution of location one, so you don't need to do it anymore after that or anything All you need to do is to make sure it's up to there So if let's say we have another parameter underneath in the main where it says that D foo if we had let's say int B and it equals Delta and it's a parameter that accepts C to Does that mean we never hit that because we already hit the location Correct. So here in main we would have space for a local variable B, but we don't know what the value We don't know what the value is yet Okay, right because we haven't hit this function, but we know that that space exists for that There is a local variable called B here. So on the main do we leave D empty then because yep Exactly Yep Yeah, because so right because main called foo, which called foo, which called foo and none of these functions has returned yet So if this function were to return and then we were to print out the stack, right? We'd only print out these two Or if These were switched and we called Delta Delta did something and then we called these foo Delta wouldn't be on the stack because it's not called Right, so it's kind of like if you walked up from this location here and said, okay Who called me who called me who called me all everything there should be on the stack So let's say instead of having come in there's a seat having foo being called within foo again We would just have Return instead of else or instead of if foo B We just go return and then else return in the location one if we had indeed foo and then int Be foo again with a different number and then would we not care on let about those stacks that we hit until we Exactly wherever you want to print out the stack at that point Only those functions that are on that stack matter. Okay, so I could have a print F. I could print out a here. I Could do whatever I wanted in any of the any function calls here and they may change these values So you need to make sure you can execute them and update the values correctly But these don't change the stack at this point here because they execute They change the stack and then they return and that goes away So if we on the test for example We had it where you had that kind of mentality where the program goes that direction If we wrote everything out, but we just axed out the ones that we don't need that's considered fine Or should we just have the final so I would probably I would do that That's a good way to show your work and I would also then have your final one and you mark clearly like this is the final Okay, I always think about who's reading Because if it's like then you're like well But I really meant that this one was the one I wanted or something or if those marks got like a little bit People do crazy things right if I got like that right and now you're like well But that's I really meant that that should be gone But whatever it could be all kinds of all kinds of ambiguity that we want to reduce Yeah, if you want like scrap like work here do this do this and I'd be like like even you could do something like final Answer and then like a stack Anything to make it clear that like this is But like also when you do this be careful because I see some people and it kills me we're like they do this But then they mess up the copy here There's points for that Kills me but like this is you're saying telling me that the that chance are right so like I cool Yeah, so for the midterm what we have to account for is local variables Anything else that we have to explicitly put onto the frame. Oh and you're drawing the stacks like this. Yeah, no, I mean Yeah, I mean, I don't think I'd have you do like EVPs save EVPs But you should be able to answer questions about those things if I ask you questions about EVPs or Instruction printers quickly change the example so that Okay, you don't have to go through three right right. We'll just do we'll do one one would be fine Okay, I'll keep that up unless you're not unless that's not fair game for the exam Of course, it's fair game for the exam Maybe I just don't want to do it because I'm worried that you'll that it's too close It's too close to the exam. Exactly. Yeah That you said as long as we create it then that doesn't matter, right? That's true And you want us to do well? I do That's definitely true, but I don't want you to do well in the exam because I gave you the exam Studied exactly what was on it, right? So many things you gotta imagine like, you know There's the whole scope of the class is this big nebulous space I can only test on a certain part But you don't know exactly which part because I want you to know everything But I can't enforce that you know everything's the test would be too long So I have to not tell you exactly what I'm testing so that we'll study everything You don't necessarily have to test us on generalization. So this would be generalizing the concepts Well, it's just a different application, but so exactly actually this is a really good question because it's It doesn't get into a lot of other things we talked about. Okay, so I have B So let's say I have like before in a is equal to zero and then I have a is equal to like a plus star B and then Star B is equal to a Return a something like that We're super simple. So we'll print out the stack here. Yeah. Oh the other nice thing with this what like learning This helps actually debugging because when you're in any good debugger You can you can print out the stack trace of function calls and you can actually if you're debugging in here You can say go up to the next function Inspect the variables go up to the next function inspect the variables and go back down so you can see kind of where the problem is This is actually really handy. Okay, so we have main This I think we keep the same right Address Yeah, yeah, I can do C plus a but or just see yeah, I like that. So we're using that one. Okay So let's print out the stack here. Okay, so we have main It's me in the first function frame. We have main We have C oh five ten And we have some D. I don't know yet and now we call function foo, right? Okay, but now we gotta be so we have C a so what's a To exactly so C to is this Is 10 but now we're seeing the address of C to so it's gonna give us the address of C 10 So yeah, if I didn't tell you a specific address, I would probably do it Symbolically so you could say you give it a location address of C to So then So now I have function foo. It has one parameter B What's the value and B? Which is Right it's a pointer We know the value that's inside the pointer is an address to some memory and that's all that's all pointers are And then we have a is 10 a is zero And that's all that so this would be foo So then we say a is equal to zero a plus star B So a is zero. What's star B? Ten so zero plus ten is ten So I'm gonna put ten here Then I say star B is equal to a so a the r value is ten Where's the location star B? Here so oh man, it's the same. No, okay Okay, so this is ten plus one is eleven So I take eleven and put it at the location associated with star B. So star B points here So this would change this to be eleven And that would be the stack right here See that calls different and you said you might may or may not test on it with C. Decal basically it kind of be flipped wouldn't it like B would actually be at the bottom and a would actually be at the top if we were using C No, the parameters are always in C. Decal the parameters are always on top Because the calling function pushes the parameters, right? So they have to be above the local parameters. Well, yeah, so technically B would be above the line for food, right? Because you're pushing it for me Main yeah, it's kind of a semantic thing to me I like to think of like who's responsible for that memory So as soon as main calls foo even though main like created the space for that memory It's really foos responsibility like that foo can access that memory. Let's put it that way Right foo can get to that parameter even though does it based off the ESP, right off of the base pointer. Yes Yeah, so technically the base pointer That moves and then right So the EEP is here exactly so to go get local variables you go minus and to go get parameters you go plus Because in here, there's also saved base pointer and saved in structure pointer, right? So that's what you gotta go up. So those are all I would say part of foo's frame Yeah, I did this originally for the homework trying to do all of that and so it looks very different Yeah Similar similar What if B was star-star If B was star-star Where'd that be error? It would be an error because this is in star Right, so that'd be a type error. So you have to Yeah, you'd have to define a pointer an int star and then take that address operator Pass in there or pass in to find an int star star and pass that in Just one pointer No questions on stack stuff before we switch over to Hindley-Millner type of inference. Are you saying that could you create it? Yeah, I was just thinking. Both of them. It doesn't really have to do with stacks per se as much as some clarification between the difference You know by reference name It was such a good example for that before we changed it too Yeah, we can maybe change it back So the I mean it's the only well, there's a couple tricky things and pass by value It's very Right, it's what you're used to Right, the value is copied and that's the thing here that you got to make sure is like on all of these You're passing B to foo Which makes a new copy of a new value B in that frame and copies that value there pass by reference means that You are So probably the best way I think it's similar to the second example Yeah, it's kind of the box circle diagrams. So to think like Okay in pass by value you make a new box So every time this function is called Right here we call C of a so here pass by value, right C of a is so C of a is a box and 012 is 10 so that value 10 gets copied on function of location here So all these bees reference that 10 so this is completely separate from our copy of so we have a box And let's say it's a really big box Because it has the values 0 5 Right, so this is take the value associated with C2, which is 10 The value associated with C2 is 10 copy that and put that into bees box So these are completely separate right so when we set B is equal to a plus one it changes this But doesn't change C at all So pass by value now pass by reference the difference is What we say is B instead of having its own box is bound to what we pass in So it says okay, well C2 so I'm going to draw a different box like C Bracket to bound to a box that has the value 10 Right, so it passed by reference what I'm doing is I'm binding B to that location So location I pass in is C2, which is that same thing here? I'm drawing them separately because it's kind of hard to do it like that So on function of location B just gets bound here So that way every time this B is referenced we say B. Okay, look up B What's the value associated with B? Well, it's 10 and here set the value of a plus one Take the value a plus one set it to the locate Put it in the value of the location associated with B Well, the location associated be with this here We should update this put a one there boom and this goes away that B disappears Right, but that memory was outside. So that's why this updates the value outside That's my name is trickier in that We don't actually evaluate This expression to get an L value or an R value. What we do is we look at all of the Uses of the formal parameters. So here's formal parameter formal parameter or formal parameter and what we do So we do we do scoping rules here at the invocation So we say this C bound to this C and this a bound to this global a Right, so that's where I mean that's statically where these are. That's how we've been executing it, right? So when we invoke foo and we say, okay, call foo Do pass my name. So we subset change all of these underscore bees or these bees with an underscore under them with this expression Just as it's this expression executed right here so we we basically execute an equivalent foo where we have in a spaces In a is equal to zero a is equal to a plus C bracket a So what all of these a's referred to the local right exactly like we've been doing it So these are all scoped to this local a boom boom Pass by name does not change any of that. So all of these a's still reference that one and And all of these expressions are exactly as if they were executed here at function vocation So now what is this a reference? The global a exactly Boom and this C the C for me Exactly just like here So it's just as if it's executing here same scoping rules and everything But it executes for each time you see the formal parameter here. So this would pass in So I think it'd be actually the same as pass by value so it changes it to 11 But the point is that you're changing you're calling executing that expression every time We didn't change a the global a exactly because we can't because we find a local a Just good But you could do something like a plus plus right something crazy like that Now this would be every time you did it it would change a this global a Yeah, I probably wouldn't do that because I hate the semantics of this operator Yeah, exactly you could yes, I was gonna say Think we're gonna move on to Hindley Milner type difference if that's good But first let's pause this so somebody can create an example