Thursday, February 26, 2009

Beauty of Computing : Memory, Addresses, and Pointers

Computers are interesting tools which can do a lot of things. They can add, subtract, multiply, divide, and do basically any type of mathematical calculation. One of the central parts of a computer is memory. You can think of a computer's memory as a gigantic row of boxes. Let's say there are 65,000 rows. Each row contains something.

For example, let's say row 1-100 contains your program. It is etched and stored in row 1-100. Each row contains an instruction like, ADD apple AND oranges STORE in NumberOfFruits. The very first instruction of your program is in row 1. The next instruction is in row 2 and so on until you get to row 100.

Row 101 to row 200 is were you store you can store your variables. The number of apples you have is stored in row 150, the number of oranges you have is stored in row 151, and the NumberOfFruits you have is stored in row 152.

Now, let's say we run our program one step at a time starting from row 1. The program does what row 1 says and then goes to row 2. The program does what row 2 says and going to row 25. At row 25, the program says to do the "Add Apple and Fruit" parts. In order to do this, the computer goes to row 150 and get's the number of apples and row 151 and gets the number of oranges. The computer does its thing and then stores the NumberOfFruits in row 152. Lucky for us, once this is done, row 26 tells the program to start over and read row 1 again and this continues on and on. So this program doesn't do much.

However, doesn't all these numbers make you confused? Let's say we do this. We'll call the row apples are in (150) "AppleRow", the row oranges are in (151) "OrangeRow", and the row NumberOfFruits are in (152) "NumberofFruitsRows". These are just nicknames for us so instead of always typing #150 or #152, we just type the name above. When we type the name above, we have the row location. Just for fun, let's also call where our "Add Apple and Fruit" (row 25) "AddAppleAndFruitFunctionRow".

Now, we can do lots of things. Not only can we see the number of items "Apple" but we can find the exact row something is in "AppleRow". We can even know the exact row our "Add Apple and Fruit" function is!!!! Once we have this knowledge, we can now move stuff around.

So, this is all I'll talk about here, but it will give you a start on memory, address, and pointers.

Oh yes, to help you out, replace any mention of the word "ROW" with "Address". Oh, and the name's we called things (AppleRow, OrangeRow, NumberofFruitsRows) you can call those pointers. As for the pointer to the function "Add Apple and Fruit", we'll just call that a functional pointer.

So I'll leave you with that. That wasn't so hard wasn't it? So where does the hard part come in? Oh you mean the absolutely horrible syntax some languages use *coughs* C *coughs. Those '*' and '&' can really hurt you if you aren't careful. Don't forget the funny jargon like dereference, pass by reference, reference.

As for the data types....we'll save that for another time if anyone wants to know more.

(Aside: Please note this is a very very simplified view of what's happening inside a computer. If you want to know more, I recommend looking up "Von Neumann Machine")

0 comments:

Post a Comment

 
Copyright 2008 - 2009 by WDO Enterprises LLC - All Rights Reserved