HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
POINTER (Cray-style extension)
Chapter 10426
Pointees that are arrays with nonconstant bounds can be used only in subroutines and
functions, not in main programs.
Variables used in an array-bound expression that appears in a POINTER statement must
be either subprogram formal arguments or common block variables. The value of the
expression cannot change after subprogram entry.
You associate memory with a pointer by assigning it the address of an object. Typically, this is
done with the libU77 function, LOC. The LOC function returns the address of its argument,
which can be assigned to a pointer. The following example assigns 0 to the pointee i:
INTEGER i, j
POINTER (p, i)
p = LOC(j)
j = 0
You can also use the MALLOC intrinsic to allocate memory from the heap and assign its return
value to a pointer. Once you are done with the allocated memory, you should use the FREE
intrinsic to release the memory so that it is available for reuse.
If you are using the pointer to manipulate a device that resides at a fixed address, you can
assign the address to the pointer, using either an integer constant or integer expression.
Under certain circumstances, Cray-style pointers can cause erratic program
behavior—especially if the program has been optimized. To ensure correct behavior, observe
the following:
Subroutines and functions must not save the address of any of their arguments between
calls.
A function must not return the address of any of its arguments.
Only those variables whose addresses are explicitly taken with the LOC function must be
referenced through a pointer.
Examples
In the following example, the intrinsic MALLOC returns either the address of the block of
memory it allocated or 0 if MALLOC was unable to allocate enough memory. The formal
argument nelem contains the number of array elements and is multiplied by 4 to obtain the
number of bytes that MALLOC is to allocate. The FREE intrinsic returns memory to the heap for
reuse.
SUBROUTINE print_iarr(nelem)
POINTER (p, iarr(nelem))
p = MALLOC( 4*nelem )