Home
Intro
LC
ACR
About

Day 9 - Pointers and Such

-=-=-=-=-=-=-=-=-=-=-

What you are going to learn today:

  • What memory and pointers are
  • How do you implement the mind-boggling pointers
  • And of course, the example program!

-=-=-=-=-=-=-=-=-=-=-

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?

Variable A - Value: 24, Memory Location: 6784; Pointer to A - Value: 6784, Memory Location: 7984 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.
    Stop! - Wait! That last part of my sentence was totally true! If you carelessly declare and use pointers, they can actually cause your computer to crash - and crash badly. Why? Well, when you first create a variable, what it contains is trash - some crap left behind by some other program. Only when you give it a value is that variable 'safe' to use. The same is with pointers, except that the results are far worse. While undeclared variables normally just return 'trash' values, undeclared pointers, when used, may access memory locations which are sensitive to the computer's inner health, causing it to crash. So, remember to give values to pointers before you use them. Otherwise, you'll face the undesirable consequences!
Well, now that you know what pointers are, we can safely tell you what dynamic variables are all about too. Simply put, dynamic variables are variables you create while the program is running. Unlike variables created with the 'var' keyword, dynamic variables are created only when needed - which means that it has no variable name, just a place to store a variable. It's something like a last-minute box that is shoved in a position - nobody bothers to stick a label on something that last-minute. So, if it has no name, how do you get its value and do operations with it? Well, did you get the answer? Yes! You use a pointer to point to that memory location so that you can access it even without a variable name. Smart boy! Now, let's get on to talk about implementation...

Pointer Implementation

To 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.

    Rem - For those who are myopic or easily confused, let us reiterate this again: A pointer name, with no characters attached or anything, returns the memory location. A pointer name with a caret attached to the back, returns the value stored at the memory location. Don't be confused about which is which. In this case, you're actually passing the memory location for the procedure to create and delete, so the pointer name should be without the caret.
Dynamic variables are very powerful features, and you'll be amazed by what they can do. Because there is no limit to how many dynamic variables you can make (except your memory, of course), they can be used when you want to store values where the length is unknown. Want to see the new and dispose commands at work? Here goes:
  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 Program

Next 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:

Type

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.

Run

24
24

Analyse

This program is very simple, and as you'll see, it's got nothing to do with some technical mumbo-jumbo and dancing on the soil, so let's continue.

  • Line 4 - Here we defind a new pointer type
  • Line 7 - We declare two pointers for use
  • Line 8 - We declare a counter
  • Line 11 - Now, the magic begins. We make Number point at variable i, so that if we access the value pointed by Number, it will return the value of i.
  • Line 12 - We now declare i to be 24
  • Line 13 - We use Number to print out i
  • Line 15 - A new dynamic variable is created
  • Line 16 - We asssign 24 to the dynamic variable pointed to by Number2
  • Line 17 - We print it out
  • Line 18 - And this make sure Number2 gets disposed of, and it's all over!

End Of Day 9

This 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.)

-=-=-=-=-=-=-=-=-=-=-

HomeIntroLCACRAbout
Back to Class I

-=-=-=-=-=-=-=-=-=-=-

This page is ThinkQuest entry 11127.
email: tq97-11127@advanced.org