04 Data and C


[MUSIC]. Okay, now, that we've seen how to organize an address data in memory, we're going to look at how we manipulate that data, in a language like C, that let's us directly manipulate the contents of memory. Okay, in the C language, just like in many programming languages, we have many variable declarations. In this case at the top here, we're looking at two variables, x and y. They're both of type integer, and that's how we describe them in C. Int x,y And the semicolon to end that declaration statement. So, this is going to go and find two locations in memory, in which to store our two integers. given that they're two integers, they'll take up one word each or four bytes, per number. Now, we can also declare pointers in C, or addresses, of variables, and we do that using the star. A so in this case you'll see a statement that stays in star PTR well PTR is the name of a variable that points to an integer. So it doesn't actually have the value of the integer it points to the place in memory where that integer is so it will store an address rather than a value. And this is again or because we're interpreting it that way. And we're telling C that that's what we'll be doing. Okay, so now we can, also we have an assignment statement to a pointer. And in order to get an address to go put into a pointer, we have the ampersand operator. So, you'll notice here in this third statement, ptr equals ampersand x. That ampersand x means I don't want the value of x, I want the address of x. And let's put that into the ptr variable which, of course, points to an integer so this type matches. We've gotten the address of an integer and putting it into the pointer to an integer. Okay. Now to use the value that a pointer points to we also have a second use for the star operator. in these examples, this first statement, you'll notice that we, doing what we just did before, we're getting the address of x, putting it into ptr. Our next statement then, can add 1 to the value pointed to by the pointer, ptr. What this is, what this construct says is take the value in the pointer ptr, interpret it as an address. Go there in memory and get the value that's stored there. And then we're going to add 1 to that value and put the result in y. This is the same as if we had written y equals x plus 1. Okay. So so far, doesn't seem too useful. But let's take a look at the next line. In this case, we can say that ptr points to y, now. The address of y. And when we use that same statement again, add 1 to whatever ptr is pointing to. Then that's the same as saying y equals y plus 1 now instead of x. But you notice this statement didn't change at all. It stayed the same. This is going to allows us to do more general functions in C than we would have otherwise been able to do if we were restricted to only use the name of the variables. Okay. Last example on this slide. star ampersand x. What is that equivalent to? Well again, &x is just the address of x. Okay. But when we dereference that address, using the star, we now get the value at that address. Well, what is the value with that address, well that's just x. So, in fact star ampersand x is the same as writing just x, okay. Good. Now lets go on to some more interesting examples. Well for one thing we can add values to pointers. So in this case we're using a pointer as if it were a variable. And just saying add one to that value. But in reality pointer is being interpreted by c as being an address. So, when we say add 1, C says wait a minute. You are pointing to an integer that's what ptr is a variable that points to integers. So, if you are adding 1 to that you really don't mean you want to add 1, you really mean you want to add 4. And why 4 because that's the size of an int you are not going to want to just add 1 to that address and point to the next byte that doesn't make much sense. You probably want to point to the next integer or the thing following that integer in memory, so we are going to add 4 instead for you, okay. Well this can be very useful, but it also can be quite dangerous when we don't know exactly what is following that Variable in memory. So if we know what's immediately after x, that's great. Maybe it's the value y. But there is no guarantee that, in fact, y was placed in that location in memory. Alright. Just remember that, and we'll be coming back to that several more times. Alright. In general, what we haven't C is in, is an assignment statement, is a left-hand side and a right-hand side, on the two sides of the equal sign. The left-hand side has to evaluate to a memory location, a place where we can put a value. And, the right-hand side, has to evaluate to a value, the value we're going to go put there. And because we also have pointer variables, that value could be an address. So, let's look at a series of examples here that'll highlight this. What I'm showing here on this slide, is again a memory map that goes from location 0 to location 24 hex. I've placed the variable x at the location 4, and the variable y at the location 18. you'll notice that y is initially this hex value. that corresponds to 3CD02700. And you'll notice that it's in reverse byte order in the memory here. And the reason I've done that is because from now on we're going to be talking about little endian machines. And so we're going to but the least significant byte in the first byte position and the most significant byte in the last. So, you'll be seeing a lot of these reverse order bytes for our integers. Okay, so that's the value of y stored there and the value of x is 0. let's take a look at a sample statement and see. This case, we're saying x is equal to y plus three. Well, let's take a look at the right hand side. It says go get the value of y and add the, the number 3 to it. And then we're going to take that result and go put it where x is stored. Okay, the memory location of x on the left hand side. So what we expect to see happen then is to take this value here. Add 3, and have it appear up here. Okay, and that is, in fact, what happens. You'll notice, that that least significant byte is the one that had the, the, that changed, when we added the 3. Okay. Another example, this time you'll notice our declaration is a little bit different. We've declared now rather than the, an integer x. An integer pointer x. So, we're going to interpret the values stored at x at location 4 as an address. In this case, you'll notice, our assignment statement also looks a little different. Now we have to get an address to go put there. Well, what we're going to do is get the address of y. And add 3 to that or are we really adding 3? Well, remember because C knows this is an address to an integer, instead of adding 3 it is going to add 12, 4 times 3, because that's the size of an integer. So it's going to go Get that address of y 18. And then add 12 which in hex is just c. The result of 24 is now going to be stored at the variable x which is a pointer variable. So it can store addresses and so we see the number 24 appear at that address. for x. Okay lets move on to another example. In this case we're saying lets take the value stored at x interpret it as an address. Okay. Go to that location in memory and put the value of y there. Let's try that again. On the right hand side, you see just the value of y. That's that hex value stored at location 18. And we're going to go and place it not at x, as we did in the previous example. But at the address pointed to by x, that extra star in the front is a de-ference. It is saying don't interpret that x is the address but rather go to that location, find the address and then go there. In this case what's that x. Well, it's that at that number 24 that we put there previously that address. so we're going to go and place the value at the address 24. So we'll see it appear at this last location in memory. And that's in fact what happens.

Wyszukiwarka

Podobne podstrony:
EV (Electric Vehicle) and Hybrid Drive Systems
Madonna Goodnight And Thank You
Found And Downloaded by Amigo
2002 09 Creating Virtual Worlds with Pov Ray and the Right Front End
Functional Origins of Religious Concepts Ontological and Strategic Selection in Evolved Minds
Found And Downloaded by Amigo
Beyerl P The Symbols And Magick of Tarot
find?tors and use?sesCB35F0
Found And Downloaded by Amigo
Advantages and disadvantages of computers
info Gios PDF Splitter And Merger 1 11
tn shopping and services
american realism and naturalism 1
SHSpec 74 6608C04 Dianetics, Scientology, and Society

więcej podobnych podstron