The POINTER @ statement establishes pairs of variables and pointers.
POINTER (p1, v1) [, (p2, v2) ]
Parameter |
Description |
v1, v2 |
Pointer-based variables |
p1, p2 |
Corresponding pointers |
Each pointer contains the address of its paired variable.
A pointer-based variable is a variable paired with a pointer in a POINTER statement. A pointer-based variable is usually called just a based variable. The pointer is the integer variable that contains the address.
The use of pointers is described in "Pointers ".
Example 1: A simple POINTER statement:
POINTER (P, V)
Here, V is a pointer-based variable, and P is its associated pointer.
Example 2: Using the LOC() function to get an address:
* ptr1.f: Assign an address via LOC() POINTER (P, V) CHARACTER A*12, V*12 DATA A / 'ABCDEFGHIJKL' / P = LOC(A) PRINT *, V(5:5) END
In the above example, the CHARACTER statement allocates 12 bytes of storage for A, but no storage for V; it merely specifies the type of V because V is a pointer-based variable. You then assign the address of A to P, so now any use of V refers to A by the pointer P. The program prints an E.
Example 3: Memory allocation for pointers, by MALLOC
POINTER (P1, X), (P2, Y), (P3, Z) ... P1 = MALLOC (36) ... CALL FREE (P1) ...
In the above example, you get 36 bytes of memory from MALLOC() and then, after some other instructions, probably using that chunk of memory, tell FREE() to return those same 36 bytes to the memory manager.
Example 4: Get the area of memory and its address
POINTER (P, V) CHARACTER V*12, Z*1 P = MALLOC(12) ... END
In the above example, you obtain 12 bytes of memory from the function MALLOC() and assign the address of that block of memory to the pointer P.
Example 5: Dynamic allocation of arrays:
PROGRAM UsePointers REAL X POINTER (P, X) ... READ (*,*) Nsize ! Get the size. P = MALLOC(Nsize) ! Allocate the memory. ... CALL CALC (X, Nsize) ... END SUBROUTINE CALC (A, N) REAL A(N) ! Use the array of whatever size. RETURN END
This is a slightly more realistic example. The size might well be some large number, say, 10,000. Once that's allocated, the subroutines perform their tasks, not knowing that the array was dynamically allocated.
Example 6: One way to use pointers to make a linked list in f77:
demo% cat Linked.f STRUCTURE /NodeType/ INTEGER recnum CHARACTER*3 label INTEGER next END STRUCTURE RECORD /NodeType/ r, b POINTER (pr,r), (pb,b) pb = malloc(12) Create the base record, b. pr = pb Make pr point to b. NodeNum = 1 DO WHILE (NodeNum .LE. 4) Initialize/create records IF (NodeNum .NE. 1) pr = r.next CALL struct_creat(pr,NodeNum) NodeNum = NodeNum + 1 END DO r.next = 0 pr = pb Show all records. DO WHILE (pr .NE. 0) PRINT *, r.recnum, " ", r.label pr = r.next END DO END SUBROUTINE struct_creat(pr,Num) STRUCTURE /NodeType/ INTEGER recnum CHARACTER*3 label INTEGER next END STRUCTURE RECORD /NodeType/ r POINTER (pr,r), (pb,b) CHARACTER v*3(4)/'aaa', 'bbb', 'ccc', 'ddd'/ r.recnum = Num Initialize current record. r.label = v(Num) pb = malloc(12) Create next record. r.next = pb RETURN END
demo% f77 -silent Linked.f "Linked.f", line 6: Warning: local variable "b" never used "Linked.f", line 31: Warning: local variable "b" never used demo% a.out 1 aaa 2 bbb 3 ccc 4 ddd demo%
Remember:
Do not optimize programs using pointers like this with -O3, -O4, or -O5.
The warnings can be ignored.
This is not the normal usage of pointers described at the start of this section.