 hours and we are talking about problem four from the practice midterm one and so the question is when we look at this and we talked about static scoping and dynamic scoping do we start at the top do we start at main when do we start at things right that was the question so the basic so the big difference so you do the same things essentially right so execution is the same right so we know how the semantics of execution in normal program right you start at main you start executing to the lines and when you see a function call you go execute that function when that is a function call you go execute that and you go back go up the call stack right I think that's that's pretty standard so in both cases the way you're doing the execution is exactly the same the difference is when you construct the call stack or the sorry the symbol table so do you construct it statically beforehand or do you construct it dynamically at runtime and that's the two differences there between the names of static you're doing you're constructing the symbol table statically versus dynamic you're doing it dynamically so it would help if I do an example okay so for this so I'll just do a very simple example that's very that's similar so we have foo I or G so I'm cheating here and not declaring things and I'm using a nicer language than C so I don't think the printouts even and so I have main I equals five and what we're doing it's called G and then call F made okay so statically we can look and see that so we can see that there's two declarations right so here's here's the big difference so we didn't really talk about it okay we didn't talk about it but statically the big difference so static with static scoping I build the symbol table but I build it up statically as I go through and parse this function this program so I start at the top so I'm going through and I'm parsing and trying to understand the symbols so I see there's a declaration of an integer I so in my table I create an integer I and it doesn't have a value because I'm not executing it now I'm just looking at declarations right I'm just statically going to the program now when I hit a little bit ahead towards future compiler optimizations if the compiler knows that this is always a certain value then it may be able to use that later on but don't worry about that now so and I know this scope is this whole thing right then I go into this F and I start parsing this function F and I see oh this is a new scope so we're creating a new scope here and I see that there's a declaration for an integer I and so that way I know that this I right here that we're referencing maps to that declaration of I because I can look it up in my symbol table I see where is this symbol I declared oh it's this same eye that's declared here so let's say each of these has a like the location of the declaration so that's how I know oh that I is that I and not the global I and I can know that statically and then so I'm just going through let's say resolving the names of all these things in my symbol table because I want to check to see if you're using a name before it was declared so when I get out of here and I go outside of this block now I get rid of that name because it's out of scope right it no longer exists here then I go to G and I say okay G references some I what I is this oh I look it up in my symbol table and I see oh all three of these eyes all reference the same eye up here right the global I and so okay I know that's good that's declared so I know that's statically what it references then we go here nothing changes nothing was declared here and now here I look up the eye in the static symbol table and I see oh that's this I appear and I also look up G and F and I can see where they get called with everything so I do that now before I start executing the program and then boom it's done this never changes throughout program execution so what I decided at compile time right static I'm sat analyzing this I know that this I always refers to this site and specifically these eyes here always refer to that global eye and are never going to refer to anything else that makes sense so we're not calling any functions because we're not executing exactly we're not doing anything we're not executing anything we just want to statically determine that yes these names uses are referring to these declarations which is all statically and then once it's done statically then we start executing and we say okay let's start executing from here so boom so now I set this to five which is this global value to five I go into G G increments the global value to now six so we print out print out six G returns then we go into F we set this thing to be 10 recall G again G increments and I remember this I is always this I same I so only variables not functions are declared statically so functions are also declared statically yeah so because the big thing is we need to decide here when we see this call to G we need to resolve this call to one of the G's that was defined previously now I'm I didn't technically define this one above this one because I wrote it by hand right so in C this would be actually an error because statically we'd get to here we'd see this name G we'd look it up in our simple table and I only know about an I and an F I don't know any names G so that's definitely an error but I could fix that with like a little declaration there okay so I print out six now I got to here set that to 10 called G we're in G from this F and now we're getting ready that to seven which we did we're gonna print out seven then come back and then we leave F and then we leave me so we print it out six and then seven make sense questions on all right yeah yeah so now dynamic scoping right so in dynamic scoping so there's a couple ways so the main way to think about it is we're resolving names at runtime while the program is executing them we resolve the names using the dynamic symbol table at runtime right so now essentially we're executing so now we're not going through it first we're going through it and executing it so we go through here we see a declaration for I so we create an I in the global in this scope scope is here they go here we just be really complete we're gonna create entry for G to say that there's some function G that's being created we going here to say there's some function F being created do we execute F no that's not how this man right we start execution at the main function we're going through and seeing that oh yeah there exists a function F there exists a function ah here's the definition of G so here I probably have like say hey this is the body of G and this is the body or the other way around it's faster is to just changes so I get into there and then I get into main and say okay this is where I start executing at aha the main function finally okay so start execution so set I equals to five so I need to look up and say what is this name resolved to right so I look it up here and I see oh well here's an I great I'm gonna set its value to be five then we're gonna call G and I'd be like what is this name G right and so I look it up here and say oh this G refers to this function so I actually know I can start executing here now I set I is equal to I plus one so I try to resolve these eyes what are they resolved to please this one right yeah this I appear in the global symbol table so it's gonna update that to six and then we're gonna print it out so we'll print out six and then G returns now we start executing F so now we see a new declaration for a new integer in this scope so now we're gonna create a new entry in our in our simple table of I that doesn't have any scope or that doesn't have any value yet but we see this line and so we're gonna execute I we're gonna set I to be ten right and so we know which I this refers to because we look it up in the table and look it up on the top and we see that I and we see that that okay that's where I'm putting that 10 then we call G and once again we would actually look up G in this dynamic scoping table to say what where does where's the body of this function that I'm calling and I'd see oh it's here but okay good so I start executing here so now I want to resolve this I what do I was what is it value does it resolve to say here 10 plus 10 yeah so we're gonna take 10 plus one add it to I so it's gonna be 11 we're gonna print out 11 then G is going to return and now F's going to return and remember this this scope here that we created here is only valid inside this block so we know once we leave it we have to get rid of it and pop it off essentially just get rid of that whole scope so F returns and when it does this goes to so if we were to were to print out I here we print out six because that one's gone and no longer basically exists you said you look at the simple table bottom the top yes yes because that's how shadowing works so that's why statically when you look here when you want to resolve this I to a name you look from the closest scope out and so that's why I think like bottom up you're creating these scopes so yeah each of these scopes is more is closer to where you're either executing or analyzing if you're doing it statically so here I'd say aha this I goes to this local I just to find here boom okay so I know that result that is refers to that I but once I get out of here when I'm doing it statically I get rid of that and now these eyes all refer to this global I make sense right problem five number two okay yeah so problem five is about code with the pointers and the okay so I'll draw like a simplified version of this on the board okay any questions on dynamic scoping semicolons and changing it slightly because it's going to affect anything in pointer B and we're saying basically a is equal to malloc something that will hold the size of an integer the same thing then we set star a to be 42 then we're gonna set malloc more memory but in B we do star B equal to star a key is equal to the address of a address of B okay and what we want to know is from one question one states basically at this point it's point one this point is point two so basically draw all the circle box diagrams for point one draw the circle box diagrams for point two so every kind of they want to do this one the point one yeah okay but specifically asked for and B so the box so first thing we know whenever we have a declaration right we're binding that name a so we have a name a right and it's a declaration so we know there's gonna be some location for that with a value in it so the location of the box the values the circle and this line is gonna bind that name a to that location so all that's done when we say in star a exactly yeah and this is part of C semantics right there's really no way to say well I think I'm pretty sure in C semantics there's no way to say like I just have something called a so you declare an a without binding it to anything yeah there's always when you declare something there's always something bound to that okay and the other thing that the question specifically says is that the address at wherever this is malloc that the address of the location this returns is called one just calling okay so we have our a we have our B so these are after these two declarations now here the semantics are malloc is going to create think of it as a new location for us so it creates a new box for us and that box has a value associated with it we don't have anything quite in it yet but we know on the right hand side that this the address where this location is called one but so this is a case where the malloc creates this but there's no name bound to this this meant this location right we can't right if you just like you could just say malloc stuff and there's your programs creating locations in memory right but there's no way to access those and to get to those okay so what is the result of doing this execution so we execute this what are the semantics here the value of you can like so much error no the value of a is memorable right yes we're recording though so you should just become a YouTube celebrity if you're not already okay so malloc returns an R value and the R value returned by malloc is the address of the location that it creates right so it returns the value one which is an R value and the semantics here says when we have an L so a is definitely an L value right because the location associated with a so the semantics of L value equals is assigned an R value is take the value from that R value and put it in the location that is associated with that L about put it in the value that is inside the location associated with that L value right so it's a long way of saying we have a with there's a location associated with a and it has a value in there so we copy the return value of malloc which is the R value one we copy that into the value that is in the location associated with a one represents an address it's a way of referring to a location so in the yes exactly so yeah so it's the but it doesn't so it's actually if you read the C standard doesn't have to be like exactly memory addresses like the compiler could know that I don't they could have weird mappings but in effect what it is is this is the physical like the it's the virtual address what the program thinks that addresses that's where that program that this lives but we don't have to think about that that's what the box circle diagrams are it's a show that it's actually like more abstract than it doesn't are this thing could return negative numbers here it could return positive numbers it doesn't yeah it doesn't matter what it is it could be symbolic yes yeah okay so that's after this execution and then so after yes of course so can we consider then that the location associated with a is pointing to the other location so here's the way I think about it I think about where does so this is the thing is you want to kind of get these ideas of that a even though a has type in star all that is is just a location with a value in it there's nothing special about in star versus it an int is gonna look exactly like this when you look at it the memory it's just a location that has a value associated with it well let's get there in a second so so you can the question really kind of is what is star a that's where the question of pointers or pointing or dereferencing so if you dereference a right so this the dereference takes in either an r value or an l value and it returns the location so it's like the l value that is the location associated with that so if there's an l value in here it says what in that value in there was the location that has that address so we know here so the way we can represent that is actually an arrow from the value which is the circle to the box exactly so this means that star a returns this box that location and so come in handy in a second so then we do this oh any other questions on this line okay then we execute this line so the malloc was the malloc do some memory creates yeah it creates some location somewhere with some value and we know its name is to that's what the address is or it's not names all right but it's addresses to and then this assignment so was the assignments semantics do here so the value of the location associated with B so we copy that r value which is to which is what malloc returns copy that into the value which is inside the location associated with B and that's why we put two in there so now in effect star B is here questions we are treating whatever is in the value associated with that we're treating that value as an address and we're returning the location associated with that address no the location is the box the address oh yeah metadata associated with right exactly so yeah so dereferencing it is saying okay take whatever is in that value look up that address and return the location that has that address so it returns the box so that's why when we do something like here right at point one when we say star a equals 42 so here we have what's this l value r value r value returns the box returns the box no it returned the star returns the box so specifically the star always returns l value excuse me so it's important for things like here right because if it returns the value right so the semantics say we can have one of two things we either have an l value is equal to an r value or an l value is equal to another right those the only two semantics we have so if star a returned an r value we wouldn't be able to use it on the left hand side here so this is why it's all consistent because here we have an r value of 42 which is just the value 42 so we have these semantics here l value is equal to r value so this says copy whatever the value is associated with that r value take that and copy it in