In the implementation below, I will show you how to intialise a pointer and use it, after which I explain what is going on.
int_ptr : ^Integer;
int_ptr^ := 50;
Writeln('The pointer is pointing to a memory location storing', int_ptr^);
int_ptr^ := int_ptr^ + 1;
Writeln('Now it is increased by 1:', int_ptr^);
Writeln('The pointer has no more significance: ', int_ptr^);
In the line,
Var int_ptr : ^integer;
we are decalring an integer pointer. It so called because we are using integer as data type. A pointer may be any of the existing data types, even user defined data types. We use the (^) character to indicate a pointer declaration.
Dynamically creates and allocates space for int_ptr. Before this line, the integer pointer was simply resident in memory without pointing to any memory location. Now, after we have dynamically created the pointer, it is set to point to a memory location chosen by the OS.
int_ptr^ := 50;
stores the number 50 in the memory location pointed to by int_ptr.
Finally the integer pointer is disposed of when calling dispose(), passing the pointer as argument to the function. When a pointer is disposed of, it won't be possible to use it again (i.e. its link to the memory location that it has been previously pointing to is destroyed) unless re-created dynamically using new().
A pointer may be assigned a nil value when it is not being used for the moment but is planned to be used later. One can also test if a pointer is nil using the statement
if my_ptr = nil then ...
This will return true if my_ptr is set to point to nothing.
Pointers may in effect be set to point to the same location. This is done by using the assignment statement just as when we do an assignment of one variable to the other. When assigning two pointers of the same type, the assignment is bi-directional. Here's how we do an assignment of two similar typed pointer variables:
int_ptr1 := int_ptr2;
Assuming that both variables are integer pointers, they will now point to the same location. From now on, one either of the two integer pointers is assigned a value, they will change the memory location accordingly.
Consider the following (keeping in mind that both integer pointers are assigned to each other):
int_ptr1^ := 10;
The output of the writeln command is 10 since the memory location that is pointed to by int_ptr2 (which is also pointed to by int_ptr1) is changed by int_ptr1.
If we had to dispose int_ptr1^ before we actually assign it to int_ptr2, int_ptr1^ will lose the link to the memory location it has been previously pointing to. When it will be assigned to int_ptr2, the link is re-created and is set to point to the same location pointed to by int_ptr2.
Eventually, more than two pointers can point to the same memory location, however, despite the fact that more than one pointer can point to the same location, one should keep in mind the careful organization and memory storage of the values stored in a memory location by the pointers.
A pointer is stored in memory just like the way other variables are stored. Consider the following:
The memory location is where the variable/pointer is situated in memory. The operating system keeps track of where all these variables are located for later reference. As you can see, each memory location stores data in it. The pointer, obviously stores the memory location it is pointing to and not the value. It also indicates that it is pointing to an integer data type. When you obtain a value via a pointer, the operating system looks at the pointed location and acquires the value from there - this is called dereferencing.
A pointer stores a memory address (in hexadecimal) which is the location pointed by the latter. Pointers are very prone to illegal access of memory locations in memory and can result to what is commonly referred to as Segmentation Error (in Linux). When this happens in Windows, an illegal exception occurs and program is terminated. This is called memory protection, in which case the operating system won't allow a program to access memory outside its pre-allocated memorary range. This was not so in early versions of Windows operating system and accessing illegal memory locations would result in corrupting the windows that is loaded into memory and the whole system stalls. There is a lot of more advanced uses of pointers. This will be demonstrated in the Linked Lists article.