Declares Cray-style pointers and their objects.
Syntax
POINTER (pointer1, pointee1) [, (pointer2, pointee2)]... |
- pointer
is a pointer.
- pointee
is a variable name or array declarator.
Description
HP Fortran 90 supports both the standard Fortran 90 POINTER
statement as well as the Cray-style POINTER
statement. The Cray-style POINTER
statement is supported for compatibility with older, FORTRAN 77
programs. The following information applies only to the Cray-style
POINTER statement;
the Fortran 90 POINTER
statement is described in “POINTER (statement and attribute)”.
The following restrictions apply to pointer:
It should be of type INTEGER(4).
If it is not, the compiler interprets its type as INTEGER(4)
regardless of other implicit or explicit type declarations.
It cannot be declared of any other data type.
Another pointer cannot point to it.
It cannot appear in a PARAMETER
or DATA statement.
It cannot be in a derived type object.
You can increase the size of pointer
with the +autodbl or +autodbl4
option; see "Option Descriptions" on page 576.
pointee may be of any type, including
an array, a derived type, a record, or a character string.
The following restrictions apply to pointee:
It cannot be a dummy argument, function
name, function value, common block element, automatic object, generic
interface block name, or derived type.
It cannot be used in a COMMON,
DATA, EQUIVALENCE,
or NAMELIST statement.
It cannot have any of the following attributes:
ALLOCATABLE,
EXTERNAL, INTENT,
INTRINSIC, OPTIONAL,
PARAMETER, POINTER,
SAVE, and TARGET.
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 ) IF (p.EQ.0) THEN PRINT *, "MALLOC failed." ELSE DO i = 1,nelem iarr(i) = i END DO PRINT *, (iarr(i),i=1,nelem) CALL FREE( p ) ENDIF RETURN END SUBROUTINE print_iarr |
Related statements
POINTER
(standard Fortran 90)
Related concepts
For related information, see the following: