What you are going to learn today:
We all need memory, right? People always give this advice: Get your computer more memory! Buy memory while it's still cheap! Use your old memory chips as samples to bring to class! Well today, we're not only going to introduce you to memory and pointers - two very dangerous topics (if wrongly dealth with, of course), but also to the concept of dynamic variables. Nothing difficult, so good luck!
What are pointers?Em, remember our analogy of having boxes to represent variables back at Day 3? Well, let's go back to that analogy to understand pointers. Now, imagine that everytime you create a box (declaring a variable), it is located at a certain position in this huge shelf called 'memory'. This position is represented by a number. Now, what happens if we want to make another variable to trace the position and value of this example variable? Ahh, we use pointers!
Pointers, my friend, are simple variables which store the location of another variable. But this alone gives us almost unlimited flexibility. You may ask: "What the hell do I want with a variable location?!" Well, by obtaining the location of a variable, you can find the current value of that variable, manipulate the variable's value, create dynamic variables, and crash your computer into the great void.
Pointer ImplementationTo implement pointers, you need to know two very sacred characters in Pascal: the caret (^) and the little-snail (@). To declare a pointer, first insert a caret in front of the data type, like this:
type TPointer: ^integer;Those lines declare a pointer to an integer. Of course, at this very moment the pointer is pointing to the great void, so we can't exectly do much with it, (unless crashing computers is your hobby). So, this is where the @ character comes in. That funny symbol returns the memory location of any variable, which you can assign the pointer to. See?
var MyPoint: TPointer; a: integer; begin MyPoint := @a; end.So, in this example, MyPoint would be pointing to variable a because we had just passed it a's memory location. Now, this location isn't exactly very interesting - what we want is the value being stored at this location. So, to access this value, you use the caret again - but this time attached to the end of the variable name:
begin a := 24; MyPoint := @a; Writeln(MyPoint^); end;See that? MyPoint now has a caret attached when you want to access the value where it's pointing to. Amazing! But this isn't the only way to use a pointer. You can carve out a shelf space and place a value there at runtime. A dynamic variable! As described before, dynamic variables don't have a name, they rely on a pointer to manipulate its value. There are two commands to implement a dynamic variable: 'new' and 'dispose'.
To use new, just pass a name of a pointer, immediately space is allocated and the pointer is automatically set to point at that memory location. It's that simple. Now, what about dispose? Well, if you carve out space, you have to fill it back, right? To do this, use the dispose command and pass it the pointer. Every dynamic variable you create has to be disposed of - otherwise you'll experience a 'memory leak' where memory seems to get less and less, even though the memory in your computer stays the same.
begin new(MyPoint); MyPoint^ := 24; Writeln(MyPoint^); dispose(MyPoint); end.See that? We created a dynamic variable, gave it the value 24, printed it, and then disposed it permanently. Isn't that fun? Pointers, dynamic variables, memory, these terms aren't that scary anymore, aren't they?
Today's Example ProgramNext up guys, is the last and final example program for Class I. Stretch your fingers - you're going to do some typing. Ahh, here we go:
1: program PointyPoint; 2: 3: type 4: TMyPoint = ^integer; 5: 6: var 7: Number, Number2: TMyPoint; 8: i: integer; 9: 10: begin 11: Number := @i; 12: i := 24; 13: Writeln(Number^); 14: 15: new(Number2); 16: Number2^ := 24; 17: Writeln(Number2^); 18: dispose(Number2); 19: end.
End Of Day 9This is the end of Class I, and well, it's been taxing. Loosen up, get a drink, go on a holiday. Ahh.. I can see it now... A lonely island in the middle of the Pacific Ocean. With the seagulls, the trees, the sand, the boat... Hey! Where's the boat?! Arrrgh! But never mind. Rest well, and we'll see you back at Class A, okay? (This was brought to you by Know-Better Travel Agencies.)