 All right, let's get started for today. Any other questions we talked about on Monday? Anything about function resolution or dynamic or static scoping? The question on this slide is here. It says C plus 1. It looks like C. Oh, so the question is, on this slide, why is this C plus plus on the top? Any way you want to answer it? Because it's a function, it's kind of general. I don't know if that's general. Yeah? Because in C, you can't have two functions with the same name. Yeah, because in C, you can't define two functions with the same name. Why is that? Somebody? So we had it in C. The function's name is the signature. Right, so in C, the function's signature is just the name of the function. So you can't define two functions that have the same name, and you just can't do it, even if the parameter types are different. So you should actually try it. It's kind of like, even though this is pretty much exactly, we're using the C subset of C plus plus here in this example, it still doesn't compile using the C compiler because of the function resolution rules. So that's why you have to use C plus plus, and that's why it's able to differentiate between those two functions. Any other questions on what we did? Lastly, so before we continue, just as a reminder, I'm sure everybody's aware, because it's been on the syllabus, midterms next Wednesday in a week. And on Monday, we'll be doing midterm review. So basically, I'm not exactly sure how it's going to technically work, but we'll talk about problems, we'll work through problems, and the problems will be similar to the midterm on Wednesday. So anything on the midterm will be anything basically up until today. So anything that we've covered from today since we began the class is fair game for midterm. Questions? Would you like a sample test through that? Possibly. I want you guys to finish this homework assignment first, and then I'll start giving out some prep midterm prep tonight. Will you or ETA be hosting any review sessions prior to everything? So next Monday is a review session. So yes, class next Monday will be midterm review. Yeah? Will the homework be created by midterm? Will the homework be created by midterm? Yes. I'll make sure that happens. I don't know yet. Close notes, I assume. Close notes, yeah. Close notes, full time. Yeah? I don't really know, like, one cheat sheet? No, no cheat sheets, no nothing. Do you, a pencil or a pen? And a test? Do you have a pen? No calculator, you only have a calculator, right? The only reason why it's a calculator is the program notes into it, so. I was once a student as well. OK. Any other questions? Do you want that in words or like a form line? It needs to be clear. So the question is, specifying whether grammar has a predictive parser, do I want that in words or do I want that in more formally? It really, so if your answer is correct, then it's fine. If your answer is wrong and you didn't show any work, maybe you say, oh yeah, it's because in all these cases there's no intersection between first sets. Well, OK, maybe that's true, but it could be wrong. So in that case, if you show your work and say, hey, these are the exact rules where there could be a difference. And here's the first set for one rule, the first set for the other rule, and it's looked there's no intersection for all the rules. Then if you made a mistake, we can see where you made the mistake and give you like partial credit and say, oh, maybe you messed up first sets, which is why you got this question wrong. But at least you did that. You have that logic correct, so we can give you points there. OK, so we already talked about this. OK, so what we're going to cover today is assignment semantics. So we're going to go very deep and look at what exactly does it mean when we see a statement x equals y. And we're going to focus here on c semantics. And we'll get into later about different types of semantics. Probably have to midterm, but. And so what are the semantics here for x equals y? What venture, I guess? OK, so we've got that. The right-hand operator would be actually the same. There is an equal sign. What do you mean an operator? Sorry, the right-most variable would be the value of the right-most variable would be a sign for the left-hand. OK, so the value of the right-most side of the variable is going to be assigned to the value of the left-most side of the variable. So I actually counter and say that, well, it actually depends on the programming language. Because actually, so in some languages, this is actually an equality check. So x equals y has to be a quality. And you have to actually use a colon equals operator to specify that to actually do assignment. So this is what we're going to get into. And this is what all the semantics behind assignment. So what does this mean? Are we copying? Do we actually copy what's in the right-hand side? Is the left-hand side? What if they're pointers? What do we do? Do they now somehow share the same object? So do we clone this whole object? So maybe x is now a brand new copy of whatever used to be in y. So there's all types of different options that this could actually be implemented. And so what we're going to discuss here is what does this mean? What are the different ways? And how can we bring it down to know exactly what it means? So in order to do this, we've got to define four very important concepts here for assignment. So first, we've been talking about it. And we've been talking about a name. So who wants to venture a definition for a name? It's an identifier. Yeah, what is it used for? Functions, variables? So I have to think of it as a name is used to refer to some declaration. That's what we've been talking about. That's part of what we talked about in scoping. What are the rules and how do we map a name to a declaration? So we're also going to talk about now locations. And so a lot of you may be tempted to think about a location as a place in memory or a specific memory address or something like that. But really, at this point, I want you to think a little bit more generally, where a location is just some container that can hold a value. We'll get into this in a second. And so a binding now, a binding actually is the association between a name and a location. So this is when we define that x is something. Well, we'll get to it in a second. But when we declare that x is something, well, that x is stored somewhere as a location. And the fact that we declared it means that we're binding that name x to that location, whatever it is. And so then the value here is the value that is actually in that location. And even if you think about it kind of formally, it's just like an element of a set. So if it was like an integer, it would be the set of all, let's say 32-bit integers. If it's a character, it's only going to be a bit. By no. If it's a character, it's going to be all possible character values. So what we're going to use is going to help us a lot. And what you're going to get familiar with is we're going to use what's called box and circle diagrams, which sounds like we're in kindergarten and we're going to be drawing boxes and circles. But I promise it's actually going to be important and it's going to help us understand the relationship between names, bindings, locations, and values, and specifically with pointers. So what does it mean now when we have a pointer? What does that mean? How does that change these things? Does it? And everything. So it's a little example of what this is. So let's say we have an integer x declaration. So this is c. And we have the four concepts. So we have a name, a binding, a location, and a value. So what's the name here? That's first term. X. Yeah, exactly. So we have x. So we're going to represent the location by a box. So this is going to be represent abstractly the location. And so, well, we'll get into addresses in a second. But right now, just some location, right? So there are many locations in your computer or abstractly in the program. It doesn't really matter for these purposes. But what we're saying is that with this binding, we're going to bind x to this location and say x is bound to this location. Or the other way we're going to say it is that this is the location associated with the name x. So that when we refer to x or when we refer to the location associated with x is this box here. Does anyone have any questions about that? So remember now, we're thinking more abstractly. We're not thinking about memories or registers or anything like that. We're talking about there's some x. This name x is bound to some location in the program. And then the value, we're going to represent the value as a circle within this box. Hence the name box and circle diagrams. Questions about, yeah. Can there be different types of bindings? Not as we define them. So we're defining bindings as between a name and a location. So you can see here, it's binding like the name x to the location, which is the box. So can you have a location that's not bound to a name? Yes. Yes, how? That. I mean, the location just doesn't exist, but you can't get to it, right? Yeah, so the location could exist. You may not be able to get to it. Yeah, there's a lot of locations in your program on your, when your program executes that. How can we both figure out what to do? Yeah, so we'll get into that probably in a week or two. Definitely in two weeks. So yeah, so after you allocate some memory, if you don't free it and the scope leaves and you try to access like that, those locations now either don't exist or they have their names that bind them to that location. Cool, any other questions? All right, so we're gonna use these to demonstrate the assignment semantics here. So if we have, and this is gonna seem incredibly elementary, but it's very important that we understand that what we're studying here and looking at, these are choices by the programming language designer. So they made the choice as to how these semantics work. And so we need to be able to understand that to either analyze the language or to understand what this parse tree is and to understand the semantics there. Okay, so we have this declaration of some integer x and then we have the statement x equals five. So what's kind of the most precise way you could say the effect of this statement? The second statement, it puts about five and then that location has associated with the names. Cool, it's actually, you want the slides that are printed out? Yeah. Okay, good, just checking. Somebody keep an eye on it. So, exactly, so the first statement was, we saw in the last slide, right? We're defining main x and we're binding that x to some location. And so the second statement says copy the value five. So the right-hand side here is the value. Copy the value five to the location associated with the name x, right? Any questions or want to disagree with this definition? Guess we should also specify that we're talking about c so there's no operator overloading so we don't have to worry about it doing anything else weird. Okay, so when we hit that first statement so we're declaring an integer x. So there's going to be, with that circle box diagram, right? We have the name x is bound to some location and so we kind of touched on this a little bit on Monday but what's the value that's there in the location associated with x? Yeah, could be anything. We've seen it as zero. We've seen it as some really weird numbers. It's actually the implementation. It's actually undefined by the specification so the compiler can do whatever it wants. Put something in there, not put something in there. Having you random every single time you run it because it's not specified by the specification then it can be anything. Okay, so the way to think about it is that x equals to five is going to copy the value five so we have the value five. We're going to copy it to the location associated with the name x. So that means we're going to take that value. We're going to say, okay, what is the location associated with x? Like, I know it's here because x is bound to this location. So we're going to copy then five into there and now we know that after the statement x equals five is now the value in the location associated with x. Same pretty clear? Questions on that? It's going to get more complicated. So it's like you guys can press a button and something will play out. Okay, so I had a dangling pointer there. Okay, so now we're going to look at something slightly more complicated. We have an integer x, an integer y and we have the statement x is equal to y. Seems like a very simple statement. So, but what does this mean? And is this, are the semantics here different than the previous example? So if someone wants to answer the first one, what do the semantics here mean? It's not even correct. Yes, so stated kind of in a similar thing that we've been using before. Copy the value in the location associated with y to the location associated with x, right? So before we were actually copying the value five to the location associated with x, but here we're copying the value in the location associated with y to the location associated with x. So if we describe it like this, we have our x circle, is that called box and circle? Yeah, box and circle. Forget which one comes first, I guess b comes before c, so. So we have our box and circle diagram for x, our box and circle diagram for y. So at this point, does it matter what these locations are for x and y? No, no, right? We don't really care what they are. But I guess the one thing we do care is that they are distinct, right? So because these are two different decorations, we know that these are different locations. So the location of x and the location of y are different boxes. And so when this executes, right, we're gonna follow these semantics. So we're gonna say, okay, copy the value in the location associated with y to the location associated with x. We're gonna copy whatever is there in the circle of y to the circle of x. And here, right, as we discussed, well, it's nothing right now, there's nothing in there, but it could have been set before there could be many lines in between these two statements, many statements that get executed, yeah? In the value of x, why didn't you just put y? Because wouldn't that reference the- So what is y? What is y? Well, y is the name, and then, so, let's say x equals the value of y, and then you know that it would jump to y, which would then graph out that value. So I guess, so, yeah. One thing I'm hearing from you, one thing, so this is where we get into the semantics, right? So one of the ways this statement could be interpreted is, hey, bind x, the name x, to the location y, and from now on, there would be maybe a bar between x and the location y, so now they actually refer to the same location, right? So does C do that? But no, so the answer is no. We'll get to in a second, no. No, it doesn't do that, but we will see examples later where this actually does happen. But it's a different type of semantics, so I wasn't gonna make the distinction now, but this is called copy semantics. So whenever we see an equal statement, we know we're actually copying a value. So we're copying the value in the location associated with y to the location associated with x. So let's continue looking at this. So we have this int x, and then we have the statement x is equal to x. And so now what is the, what are the semantics of this? So copy the value of the location associated with x into the location associated with x. Yeah, so it's the same thing. So it doesn't really matter that we're referring to the same name here. We're still on the same semantics. We're gonna copy the value in the location, so there's one slight distinction. There's just that, and it's one of these things that's like it's important only because we're talking about semantics. So I guess I have to be a little pedantic. So I think he said copy the value of the location, right? But we're actually, we want to copy the value that's inside that location. So the circle that's in the location associated with x to the location associated with x. So can we draw this with a box circle diagram? Yes, so we're gonna draw our box of x, and then our copy is gonna say, okay, whatever's in the location associated with x, copy it to the location associated with x, which is gonna be a little self-loop, but it still happens, and that's actually the semantics of the language. Now, maybe another question could be, well, can the compiler maybe eliminate that, because it doesn't actually change anything, and the answer there would be yes. So if the compiler can prove that the semantics are gonna be the same, if it has two different options, then it can choose whichever one it thinks is more optimal. Oh, and a side note that I didn't put in the slides. There's a question on Monday about what happened if you had a global variable x, if you tried to say int x is equal to x. So I actually tried it on two different compilers on the, I think, on one of the compilers, either CentOS or Clang on the Mac. One of them issued a warning that said, hey, you're trying to use an un-declared identifier x in your assignment statement, and the other one didn't put a warning error, but neither of them actually used the global x. So, hey, it's a little bit of clarification there. So now we're gonna get into, okay, so we've looked at some examples to try to understand what's going on here. But there was, so if we look here, right, these semantics here are actually slightly different than when we had five on the right-hand side. When we had five, it was copy the value five to the location associated with x. But here we're not saying copy the value x to the location associated with x. You're saying copy the value in the location associated with x to the location associated with x. And so this gets into, we're gonna define two terms here that's gonna help us separate these things into what we're talking about, values versus locations that have values in them. And so these come with the two terms, L values and R values. So just to be perfectly clear, this is not a capital I, it's not like my I value, it's L value, like left, and it's very easy to remember because it's on the left-hand side here of the equation, of the assignment operation. And R values are on the right, yes, thank you. Yeah, okay, so what are these precisely? So when we think about it, an L value is, we can, we're not just gonna think about it in terms of like names as we saw before, or constants like five, we're gonna think about it in terms of expressions. So, and it'll be clear in a second. So the basic idea of kind of L value is kind of also from the names. The name is left, you can think about it on the left-hand side, also has location. So, and something is an L value, an expression is an L value if there's a location associated with that expression. Right, so this is why when we just have a name, when we see the name X, is it an L value? It's an expression, is it an L value? Sometimes, that's a good answer, why? You can't just always say sometimes, that's like always right, you have to back it up. So is that done there? On the left-hand side? No, so no. So the question is, is there, if we see the name X, and let's say X is declared, if it's not declared right, so let's say X is declared. So is there always a location associated with X? Yeah, because when we see a decoration, we create the name X, and we find it to some location. So we know there's always a location associated with X. Yeah? Is that a name? I think so. Is there a case where the flip would not be if there's a little, if there's a little kick? Yeah, I gotta say yes, because yeah, this is the way, essentially this is how we're defining an L value, an L value is an expression that has a location associated with it. So an R value in this case, so an R value is, an expression is an R value if the expression has a value associated with that expression, right? So when we see five, the constant five, does five have a location associated with it? No, right? So we actually, I believe we can't define a variable named five, you could actually define a variable named F-I-V-E, which would be incredibly annoying. Actually yeah, I had to maintain this code where somebody would create variables named O-N-E-1 and it would just be like, and they would even be constant and be like, why are you doing this to me? No. So you couldn't do that, we're talking about the number five, right? So number five is a constant, it has no location associated with it, it's not a variable that we've declared, but what does it have? A value, yeah, it has a value, the value five, right? So that's why we can make statements like x equals five because we know five has a value with it, we can copy the value five to the location associated with x. Oh, which I have here. Okay, so there's two different types, so we see at the top we kind of remember this, okay, L value on the left, R value on the right, so this is one type of semantics and so this is where we got the semantics for x equals five, so we're gonna copy the value in the value associated with the R value to the location in the L value, right? So the location associated with the L value. So can we ever have five equals x? Somebody says no, say y. Yeah? No, because five doesn't have a location associated with it. Right, so five doesn't have a location associated with it, so five cannot be in L value, yeah. Well, if the language allows you to do that. Yes, if the language allows you to do that because it's a terrible, terrible language, then yeah, yeah, you can probably mess with people if you do like a pound to find, and you like pound to find the number five to be the variable five, f, i, b, e, that would be a really good one. But don't do that. Yeah, okay, so yeah. It's really bad. So this is one of those things, right, where it really gets down to semantics and exactly what language we're talking about, yeah. If you did that on the line, would it replace the volume in 50? Yes, it would also, it would be 50-0, so then you'd maybe have to deal with that more or something like that, that's the line. This is like a terrible idea, don't do that. Okay, so we cannot do this. This is an r-value equals to an l-value, we can't do it because it's not semantically valid, right? We have no place to copy whatever's here on the left-hand side which is an l-value. We can't even do r-value equals r-value, right? We can't say five is assigned to five, right? Because that doesn't make any sense. So we have no place to put the five. There's no location associated with five. Questions on that? So, but we've seen another case, right? So, when we saw x equals to y, was that either of these two? No? Maybe? So no, we've seen, right, l-value is assigned to an l-value, right? So here, x and y are both l-values. They both have locations associated with them. So we can actually assign an l-value to an l-value. So what are the semantics here? Something like that, yeah? The value in the location of l-value gets assigned to the value for location and value one. Yep, yeah, so copying the value in location associated with l-2, l-value two, to the location associated with l-value one. And so this is why our precise semantics were different in the case of x equals five and x equals y. In the case of x equals five, we know that five is in r-value. So we're saying copying that value five to the location associated with x. Whereas here, when we said x equals y, we're saying copying the value in the location associated with y to the value in the location associated with x. Any questions on this before we continue? So this is kind of a very deep and fundamental and important part. So have you ever thought about, I don't know, the semantics behind assignments like this before? Yes. Yes? So once you kind of get the rules, right, then everything can kind of make sense and we can really understand what these assignment statements are doing, particularly in the case of points. Okay, so what if we have the assignment a equals b plus c? What are the semantics here? So just thinking about the assignment semantics here. Yeah, so in this case, so what are the l-values? So just thinking about the assignment statement, right? So what's the l-value in the assignment statement? A. Is A an l-value? Yeah? Yeah, so A is the l-value in this case. So what is b plus c? Is it an l-value or an r-value? Right, so we're adding b plus c and that is an r-value, right? So there's no location associated with the result of b plus c, right? b plus, so, and actually really to, truly, if you wanted to really drill down, you breathe the semantics of the addition operator in the c specification and you'd see that it would say that it returns an r-value. So you would actually know that it's an r-value here, but we can kind of know this because, well, there's no location associated with the result of b plus c, right? But if we were actually to drill down the needs of that operation, those are both l-values that we're adding together, right? So we'd get the location associated with b, the values in the location associated with b, add that value to the value in the location associated with c, and then add those results together and that would give us a new value that we would put into the location associated with a. See if this is what I put here, yeah? So this is an r-value, where it's the value of the location associated with b plus the value of the location associated with c and that is itself a value. And so, overall, the semantics here, right, we wanna copy the value associated with b plus c to the location associated with a. So questions about that? So this is why, remember, we define l-values and r-values on expressions. So here, b plus c is an expression and its result is an r-value. So you go, yeah. I guess we can see then, is it valid to say that an l-value, an operator, an l-value will always be an r-value? Depends on the operator. Yeah. So yeah, it really depends on what that's, what, on the semantics of that operator. So we know from looking at this, we're just gonna return a value of not a location, but there are operators that can turn, do all kinds of stuff. More questions? Now we get into everybody's favorite topic of pointers. So this brings us, so we've gone kind of through all of the things we need to know in order to properly understand pointers. So there's two major operators that are very important to the semantics of pointers. What are those operators? Yeah. Star and ampersand. Yeah, star and ampersand. What do they, what do they mean? Not you. Yeah. Reference and de-reference. Reference and dereference. Yeah, I like to think of the ampersand as the address of operators. It helps me, but yeah. So we're gonna first talk about the address operator, which is the ampersand. So how many arguments does this operator take? One. Yeah, exactly. So maybe you haven't thought about this before, but these are operators, right? They take an input and they return it some output. So in this case, it's a unary operator. It only has one argument. Similar, I'm trying to think of other things that are similar. Similar to like, yeah, prefix increment, prefix decrement, all those type of things, plus, plus, minus, minus. There's some other ones. Well, we'll get into them in a second. Okay, so, what are the semantics of what we can apply the address of operator to? Can we apply them to L values, R values, both, neither? Why? Yeah, so L values because it needs a location, right? So this, you think of it as returning the location associated with an L value. So we can't pass it as an R value because that an R value is a value, an R value has no location associated with it. And so this is why we can only apply it to an L value. And once again, this is one of those things that these are all defined in the specifications. These aren't things that are making up or trying to infer. These are all precisely defined. What's the result of this operation? L value, R value, both, neither, depends. It returns an R value, why? What makes you say that? I guess what we like to say to that, should I apply it to a common equation? Yeah, so what it returns is an R value. So it's returning the address of the operator, right? The location, the location of the operator, that's just a value, just the same as A plus B is just a value, right? There's no location associated with this return result. And so the result is an R value and specifically we're gonna be a little bit more particular where the result is a type T star where T is just the type of the operator. So we're not gonna get into crazy into types, but we've all dealt with them so it'll be helpful to think about them now. Yeah. So pointer is just a... Wait, just a minute, can you guys see the screen with that thing in the middle? Yeah. Okay. Everybody? All right. Pointer is just an integer with the address location stored in like an integer, right? So technically there is an L value associated with that R value, technically there is a location. So when you call the address, but when you call the address of operator, it just returns an R value. Okay, but there's still this location to where that R value came from. So they would like this, can you say address of X is equal to 1000? 1000, no. Right, and so why? There's no location associated with that. You're only returning the results there of what that address is, but you can't change that value because there's no location associated with that. Is there a question in the back? No? Okay. So, Adrian, you have a question about the type. So we're actually returning a type of... The type of the R value that we're returning is the same type. So the team here is general. So you can think in terms of generics like Java, right? So the T could be anything. Whatever we pass in as the operand, the result will have be an R value of type T star to whatever type that is. So if our T is an int star star, then the result is going to be an R value of type T star star star. That makes sense? So what are the semantics that we're kind of touching on? So what is actually returning? So the value that returns to the address operator is the address of the location associated with the L value that the ampersand was applied to. So this is what we've been saying. So this is what's returned is that address that the location is associated with. So this is why we have to pass in an L value, right? Makes sense. So because what we're returning is the address of that location. In fact, if there is no location, we can't take the R value of a location because it doesn't make sense. There is no location associated with the R value. Therefore, we can't take the address of it. Questions on the address of operator? So the next big operator in pointers, right, is what they very confusingly have is the star operator, the de-reference operator. And why is this confusing? Well, because it's also, these stars are also in the types of when you get clear on these values. So you have an operator, but stars can also be in the types of variables just to make things more confusing. How many arguments does star operator? One? Yeah, so it's a unary operator, just like the address of operator. So what kind of types does it allow as it's input? L values, R values, both, either? Both. Both why? Because you could have double pointers. So that means that you could apply. Yeah, yeah? Just L values because you can't de-reference a value. Just L values because you can't de-reference a value. Is that true? I expected you to say yes, but you just said that's what it is. What about R values? Can you de-reference an R of value? Yeah, if the value is equivalent to an address on a system, yeah, you can de-reference it. Yeah, so you can actually trot, what happens when you do that, right, may depend on the system. But yeah, it's perfectly valid, so you can actually take, you can de-reference an L value, and you can also de-reference an R value. But the important thing is it has to have a type of T star. What I look at in a second is, this makes sense because you can de-reference the address of an operator. So you can take the output of the ampersand, the address of operator, and you can de-reference that. So that return value is an R value, right, but we can apply the de-reference operator. So we gotta think about what this actually means, whatever it turns. This is a little more complicated than the address of an operator. Okay, so for the de-reference operator, if we add some variable T, and it has a type T star, then we're gonna think about the box and circle diagram for that. So we've already seen, we have some in it, we have a thing T, right? So we know we have a name T, we have a location, and we have a binding between that name, name, let's say T and an X, the name X and that location, and there's something in there. So, is this any different from any other, say this is an int star, is this different than what we saw for int X? Is it? No, there's no difference, right? It's just a name bound to a location that has a particular type, right? It's got some value in its location. So let's say it has some value in its location, we're gonna call X little t. Okay, now, the question is all about the semantics here. So what does it mean? What is this value that it has in the location associated with X? Well, because X is a type T star, XV should be the address of a location that contains a value, let's call it V of some type T, right? So this means, let's say so, you can imagine we declared everything, we set things up, whatever we're kind of talking abstractly here a little bit, but we know that there exists some location somewhere. So we have another box with another circle in it, and now for the box circle diagrams, most of the times I'm gonna write an address on the right of the box. So here, we're gonna say that this location is at address XV. It really doesn't matter what that is, but we know that there exists an address of this location, right? And so, we now know that this XV that is in the value associated with the location of X, is itself the address of some other location. So this means when we, where does star X point to? Or where, what does it reference? Yeah? Yeah, that's right. So no, close. It refers to the location. So it's referring to this box, this location that is located at whatever is stored within. So when we do reference X, now we're talking about a location. So what does that make star X? Make it an L value, R value, either full? Right, so star X. So a star operator takes in either L value or R value and returns an L value, so it returns this location. And we know where that location is the location at the address that happens to be inside X. So let's take the value out of X and that we're talking about that location that's at that address. So this, so, oh yeah. Okay, so what about where is address of the X? The address sub operator with X, okay, louder? It is XV, the address of X? XV is the address of the location containing V, right? So XV is this second box, right? But where's the address, what's the address of X? Yeah, so it's the location associated with X exactly. So to the right hand side, there's some location associated with this with V. There's an address associated with the location bound to X. And that's what this address sub operator returns. Questions on that? Can we assign an R value to X? I don't remember if we'll get into it if you're, so you use the basic copy semantics that we have. So if you have X equal something, you would say, let's say it's an R value. Copy the value with that R value into the location associated with X. So what's the location associated with X? This box, right, the address of X. Yeah, we just copy whatever that was and put it right in there. Okay, go ahead. Okay, so that's right. We have L values and R values. So we have the definitions up here so we can have some fun. Like is star X an L value? I think that already asked, I asked that tonight. Yeah, okay, so that should be a difficult question, so. Yes, yes. Yes, yes, because star X is the location associated with, well, star X, which is the location whose address is the value of the location associated with Y, which in this case is XB, right? So we know that star X says return the location of whatever is in the value of the location associated with X. And that's my new, that's the location I'm returning. So does that mean we can assign to it star X? So what happens if I do star X equals 100? Yeah, so it's gonna, not set V, I mean it's gonna replace V with 100, right? Whatever V was, it's copying that in there. So yeah, the semantics here are copy the value 100 with the location associated with star X. What's the location associated with star X? XB, exactly. Or V, it's the address, it's the location associated with the address XB. And so we can do that, and we can take 100 and have my dangling arrow find any make sense here. And then we're gonna copy that 100 to the value of the location star X. We know the location star X is the second box, which has the address XB, and so we're gonna copy 100 in there to replace that. Does that make sense? Questions? You ready to take a midterm poll of pointer questions? Yes? Somebody's really mean. Okay, so let's go through some examples. I'm not really sure why that's why it's here. Okay, it'll be fine. Okay. So we have two pointers here, so now we're gonna go through and step through some examples, which apparently have a little bit of foreshadowing with some of my animations not working properly. So we have two variables we're declaring X and Z. So we are going to create circle box diagrams, how many? Two. Two, right? One's gonna be of some box associated, bound to the name X, and the other box is gonna be bound to the name Y. So we have X is bound to some circle box, or box circle, and Z is also bound to a box circle. So now we know what we say, and this kind of goes to somebody's question. If we have the statement Z is equal to the address of X cast to an integer, what's that gonna do? So what are the semantics here behind this statement? Something on the go? We're going to the top. Let's go to the top top. Somebody up there? Amy? Hello? Yeah. Behind me, back in the blue. Yeah, so there's some location associated with X, let's call it Y, right? So the address of X operator returns Y, because that is the address associated, the address of the location associated with X. We're gonna return that, and then that's our value, so we're just gonna copy that value to the location associated with Z. And so this case, oh, this case we can copy that into the, which we just did. Good, okay. Now we have star ampersand X equals 10. Okay, so let's try and decode this. So at the very high level, what is the semantics of the assignment operator? What are values? Yeah, so you can think of it as expressions, right? So we have copy the value associated with R value, which is 10, we're gonna copy the value 10 into the location associated with star ampersand X. So we just look at something outside. Is that a valid, is that an L value? So what is the star of the dereference operator return? An L value, right? So it's gonna be the last thing that returns, so it's gonna return an L value. So we know, okay, this is proper from the assignment perspective, because we're assigning into an L value. So we're gonna copy it into location of whatever star of ampersand X returns. Okay, so let me kind of think of it forward. Oh, so this is kind of like a high level. And then we have this star ampersand X. So there's an implicit grouping here, but I didn't, so ampersand X is past the argument, and the ampersand operator is past the argument X, and that return value is past to the dereference operator. I hope that's pretty clear, this is the way you parse this syntactically. So what is ampersand X return? Y. And then what is, so we said we can dereference an R value. So what does star Y mean? It's not X, X is the name. The location associated with X, right? So not the value, because we don't like the average, because the assignment syntax, the left-hand side we need an L value, so we need some location from here. So the star operator, remember, returns the location associated with whatever it gets passed in. So what it gets passed in is the address of X, which is Y. So the star operator just takes an address and says, oh, this is the location associated with that. So it's gonna return the location associated with X. And so what are we gonna put in the value associated with the location of X? 10, yeah, we're gonna put 10 in there. So we have questions on that? So is that valid code that we just ran in our heads? Bad code. It's bad code? True, but it is, is it gonna throw any congliation errors for warnings? It should. It should? No, it will, it's syntactically correct. Yeah, so it's not only is it syntactically correct, it's also semantically correct, right? So we're assigning to an L value and we know that the star operator can accept either R values or L values for R values. So we're passing in an R value here, which is the address of X, the return of the address operator X. So we're assigning it to be 10. So yes, this is not something you would want to do because this is a, so what's the equivalent easier way of saying what we just said? Yeah, so we did star X, what would that do? Right, because before this, right, there's nothing in that value. So if we try to do star X, so we're asking the question, what's the location associated with, what's the location that has an address of whatever is inside of location associated with X? And so we can't, we can't, I mean, we're gonna get garbage because we don't know what's in here for X, exactly. But if we said X equals a 10, semantic error, right, we went over that. So take the value 10 and copy it to the location associated with X, which would be the same thing. Good question about this? We all, I mean, would another equivalent be just saying X equals star Z? X equals star Z. Or just casting the ampersand X to change something. That's a good question. That technically may be implementation dependent. I don't know that it's specifically guaranteed if you go from an address to, there's an address, remember, so the reason why we have to have this cast to an int is the address of operator returns a type of T star. Right, so the T that's passed in here is an int, right? So what we're returning here is an int star. So here we're explicitly casting that to an integer, which is probably technically implementation defined, but the way it's implemented in most compilers and most systems means you can kind of go back and forth as long as you're careful. But that's really what you're specifying here with this cast, is saying, no, no, trust me because otherwise the types aren't gonna match and it's gonna throw a warning. But I'm saying, no, no, trust me. I really want this to go to an integer. So, and in that case, right? So if we wanted to say star Z, we'd also have to cast Z because Z is just an integer and a star operator expects types of T star. It doesn't have a star in it, so there's no pointer there to do that. Okay, what if we did this? What if we did X is equal to star ampersand X? So if someone wanna decode that at the top level of the assignment, statement? Yeah. I think it's just like saying X equals X. Is it? Yes. So Y, prove it. The star is like it's value at the address of X. Yeah. So what's the address of operator gonna return? Address of X. It's gonna return the address of X and star of X. What is it? We can see it. What's the address of X? Y. Y, okay, trade. And star of Y would be 10, so. Star of Y is 10. So I mean, so star of Y returns the location associated with the address Y, which in this case is this box here. It's not X though, right? Remember X is the name. So it returns the location, it's an L value. So here we say an L value equals an L value. So what we're saying is copy the value in the location on the right-hand side to the value in the location on the left-hand side. So yes, in this case we copy X. It's equivalent to X is equal to X. But we still do all those operators. Does that make sense? So you all be able to do this. I gave you some code like this and said what's the value gonna be after this executes an X, you'll be able to still work through it. Yes, you will, especially after the next example. Okay, so now we have a more complicated example. Here we have, we're declaring an int star star called X. We're declaring an int star Y. We're declaring an integer Z. And we're saying that X is equal to malloc size of int star, Y is equal to malloc size of int. X equals the address of Y, Y equals the address of Z, Y is equal to star of X. Two seconds. What is the output of all the box circle diagrams? One, the city, two, I was gonna give somebody an extra credit, but I'm just kidding. Okay, I was gonna do that. Okay, so this is how we parse this to make sense of what's going on, right? So we use box circle diagrams in order to keep track of what's going on, what names are bound to what locations and what values are in each of these locations. So we see that there's, we're declaring a name X, so we need to click, we need to say that there's a name X and it's bound to some location. We see int star Y, same thing, we're gonna say there's a name X and we're gonna find it to some new location. And then we say there's a name Z, so we bind it to some other location. So this hopefully shows that like, so types are really about like, I don't know, the type system and all that stuff, but we don't really care what any of the types of these things are. We don't care that X is an int star star. We just know that int star star X says define a name X and bind it to some location. That's all. So we have to hear, first, what is the malloc? What are like the semantics of malloc? What does it do? What does it return? Yeah. How it makes memory of size of whatever the parameter. Okay, so yeah, so malloc specifically takes in, its input is a number, which is the number of bytes, and it's returning a, the address of those bytes are some, excuse me, yeah, so it's returning the address of those bytes, where those bytes live. So essentially, we can think about it as, well it's creating some memory in some location, we only care, but it's creating a new location, and it's actually, there is some address, we'll call it four right now, it really doesn't matter for these purposes. So is there a name associated with this location? So, but X is bound to this location, right? So the question is, is there any name bound to that location, to this location that malloc returns? Maybe a second, I've got that yet, but yeah, so no, so no, there's no name associated with this, with this return value, right? So even if I'm saying X equals this return value, it's still, the only names we have, we have three names, X, Y, and Z, and these are bound to these locations. And the important thing is, we don't have a way to change the bindings of names and locations in C. Okay, so we return this, and then, so malloc returns an R value, right? It's returning the address that, where this is located. So then, what are we doing? We say X is equal to an R value, what are the same index there? Binding it to a location? No, so we've only bound, we only bind when we, we only bind the name to a location, when we see, when we have declarations. That's why we've created these circle box, box circle diagrams for X, Y, and Z, and bound them to X, Y, and Z. Yeah? Yeah, so the semantics here are, right? Copy whatever's on the right-hand side. If it's an R value, copy that value. If it's an L value, copy the value in that location, copy that to the value of the location associated with the left-hand side. So what we're doing here, since malloc is returning an R value at the address, we're gonna copy that address into the value of the location associated with X, which just happens to be right here. So that's what's in the value of the location associated with X. So now, where does star X refer to? It's not the value, it's the, wow. Okay. The location associated with X. Yes, so it's the location of, let me try to get it right. It's the location that has the address of the value in X, in the location associated with X. So in this case, so this is just kind of like a semantic thing, right? The pointer here, the star X, doesn't refer to the value inside there, right? It's referring to this location because star X is an L value. It has a location associated with it. Okay, so then what about this? So has everybody seen why we didn't change the binding of X, right? We're not gonna bind X to this new value because X still is bound to its old location. All that we did was copy the address of what Malloch returned into the value of the location associated with X. Okay, when we do Y, so Malloch's gonna call what's gonna happen. Create another boxing circle. Yeah, we're gonna create another boxing circle. It's gonna create some location for us. So we create that. We're gonna give it some address. We'll call it eight, whatever. So then what do we copy into the location associated with Y? The address of the boxing circle we just created. Exactly, so the address of the boxing circle that we just created, right? So because Malloch returns that address, which is an R value, we're gonna copy that value into the location associated with Y. So, oh, left and right, it's important. Okay, we're gonna copy it O8 into the location associated with X, alright, with Y. Questions on these two? Alright. Okay, so yeah, so first, okay. So Y star Y, right, is referring to that location. So star Y at this point refers to the location that has the address of the value of the location associated with Y. Okay, then we have to do this instruction. So what's gonna happen here? What's the address of Y gonna return? Is it gonna return zero X eight? It's gonna return the value associated with the value associated with the location of the address in Y. No, I'm just saying that correctly. No, maybe, yeah. Yes, so it's gonna return the address of the box that's bound to Y, right? So it's saying the location associated with Y, what's the address of it? So I actually have to give them names. So, I don't know, just to show that it's almost symbolic, doesn't really mean anything. I just give them address X, address Y, address Z. So, what we have, we take, do the address operator, what kind of value does the address operator return? L value or R value? R, R value, so we have the semantics of L value equals R value, so what's the semantics here gonna mean? Copy the value, copy the R value in, yeah, copy the R value into the value associated with X. Yes, so, copy, take the R value, take the value associated with the R value, which is the value, take the value there, copy it to the location associated with X, copy it into the value of the location associated with X. So here we're gonna take, so address of Y is gonna return, what's the value that's gonna return there? 80 Y, 80 Y, so we're gonna copy 80 Y and put it into the value of the location associated with X. And so, we're gonna get rid of this, we're gonna put 80 Y in there, and so where does star X point to now? Does it still point to the new one? To the mallet memory? No, so where does it point? The location associated with Y, right? It doesn't point to Y, why is just the name? So it's gonna point to the location associated with Y. And here's another one of those things, like try really hard to make the arrows point to the boxes and not the circles, but it's something you have to remember in order to keep track of it. So what happens to that number that's left over isn't just there and not being used? Yes, that's a very good question. So we'll actually bring into that after the midterm. So we're on time now, but yeah, it's essentially garbage at this point. And so it's memory that's been allocated, but it's never been free, so it's a very bad practice. So if you had this in a loop where you're doing this and you're doing a bunch of memory, your program would eventually consume so much memory that the OS would tell it to kill itself, basically. It's allocated too much memory and it should die. Okay, so we get to the next instruction. So Y is equal to address of Z. So now what's the address of Z? R value. It's an R value, what is it? ABZ. ABZ, so we're gonna take ABZ and we're gonna put it where? In the value of the location associated with Y. So yeah, we're gonna take ABZ, we're gonna copy it, put it into the location of the address associated with Y. We're gonna put it in the value of the location associated with Y. And now where's star Y pointing? To the location associated with Z, exactly. So yeah, star Y is now pointing here. Now when we get to this, so start Y is equal to star X. What's this gonna do? Make a point. Y is going to point to the location of Y because that's where star X is pointing to. So what's star X return? A, L, Y. So the star operator returns a location, right? So star X is gonna return this box, this location, right? So what we're doing is we have L value equals L value, right? So we're gonna copy that. We say, well, in this case, what's on the right side, whatever the value is in the location associated with the right side, copy that to the value in the location associated with the left side. So here we have star X, which is this location. What's the value in here? ABZ. ABZ, copy that to the value at the location associated with Y, which is ABZ, which is in here. So this actually does nothing, change is nothing. Okay, now we have more code, because this is fun. Hot, I thought it was over. This is important. Okay, so now we said Z is equal to 10. So everyone knows where 10's gonna go, right? Yes. Yes, okay, so we go here. You confused about that? Go back and show you this. Okay, we're gonna print out star star X. So what is star star X? It's the location that contains the value 10. So star X is this location here, right? Which happens to be the location associated with Y. And if we say, what's that star of X? What does, what is the value in there? What address does that, what's the location that that address references? Well that's ABZ, which is this location. So remember, this is actually a location in L value. So yes, this will print out 10. Okay, when we set 100, Y star is equal to 100. Where's 100 gonna go? What's it gonna replace? 10. ABY? ABZ? No. 10? Yes. Yes, it's gonna replace 10 here. And then when we print out Z, what's this gonna print out? 100. 100, exactly. Okay, important concepts here, right Dan, just a second, I'll take a question. Okay, the important thing that we wanted to find is that at the end of execution here. So remember, these values are changing and as we saw X star, Y star changed, right? Executed the program and copied different values there. So what we're defining is that star Y and Z are aliases. So what does this mean? So an alias is when two L values have the same location associated with them, right? So here Y star is this box and Z, location associated with Z is this box, right? So they're aliases for the same location. So what are the other aliases here in this program? X star and NY. X star and Y? X star star and Y. X star star and Z? Good, so X star star, star Y, sorry, actually that's a typo, that should be here, Z and then X star Y. So this is important because as we saw here, in this assignment I changed, oh this is wrong too, in this assignment I changed star Y but Z was affected, when I printed out Z that value changed and the reason is because those are aliases for the same location. Okay, so that's it, everything up till now, fair game to the term, see you all in a minute. The, the shoot up.