 |
» |
|
|
 |
Pointers in Fortran 90
are more strongly typed than in other languages. While it is true
that the Fortran 90 pointer holds the address of another
variable (the target), it also holds additional
information about the target. For this reason, declaring a pointer
requires not only the POINTER attribute but also the type, kind parameter, and
(if its target is an array) rank of the target it can point to. If a pointer is declared as an array with the POINTER attribute, it is an array pointer.
As explained in “Deferred-shape
arrays”,
the declaration for an array pointer specifies its specifies rank
but not the bounds. Following is the declaration of the array pointer
ptr: REAL(KIND=16), POINTER, DIMENSION(:,:) :: ptr |
To become
assignable to an array pointer, a target must be declared with the TARGET attribute and must have the same type, kind parameter,
and rank as the array pointer. Given the previous declaration of ptr, the following are legal statements: ! declare a target with the same type, kind parameter, and ! rank as ptr REAL(KIND=16), TARGET, DIMENSION(4,3) :: x ... ptr => x ! assign x to ptr in a pointer assignment statement |
Once the assignment statement executes, you can use either ptr or x to access the same storage, effectively making ptr an alias of x. You can also
allocate storage to a pointer by means of the ALLOCATE statement. To deallocate that storage after you
are finished with it, use the DEALLOCATE statement. Although allocating storage to a pointer
does not involve a target object, the declaration of the pointer
must still specify its type, kind parameter, and (if you want to
allocate an array) rank. The ALLOCATE statement specifies the bounds for the dimensions.
Here is an example of the ALLOCATE statement used to allocate storage for ptr: INTEGER :: j = 10, k = 20 ... ! allocate storage for ptr ALLOCATE (ptr(j,k)) |
ptr can now be referenced as though it were an array,
using Fortran 90 array notation. As an extension, HP Fortran provides the Cray-style
pointer variables; for more information, see Chapter 10.
For information about aspects of pointers, refer to: “Array
pointers” for information about allocating array pointers. “Pointer
assignment” for
information about associating a pointer with a target by means of
pointer assignment. Chapter 10, “HP Fortran Statements” for
a full description of the ALLOCATE and DEALLOCATE statements as well as the POINTER and TARGET attributes.
The following section discusses pointer status and includes
an example program. Pointer
association status |  |
Certain pointer
operations can only be performed depending on the status of the
pointer. A pointer’s status is called its association
status, and it can take three forms: - Undefined
The status of a pointer is undefined
on entry to the program unit in which the pointer is declared or
if: Its target is never allocated. Its target was deallocated (except through the pointer. The target goes out of scope, causing it to become
undefined.
If the association status is undefined, the pointer must not
be referenced or deallocated. It may be nullified, assigned a target,
or allocated storage with the ALLOCATE statement. - Associated
The status of a pointer is associated
if it has been allocated storage with the ALLOCATE statement or is assigned a target. If the target
is allocatable, it must be currently allocated. If the association status is associated, the pointer may be
referenced, deallocated, nullified, or pointer assigned. - Disassociated
The
status of a pointer is disassociated if the pointer has been nullified
with the NULLIFY statement or deallocated, either by means of the DEALLOCATE statement or by being assigned to a disassociated
pointer. If the association status is disassociated, the same restrictions
apply as for a status of undefined. That is, the pointer must not
be referenced or deallocated, but it may be nullified, assigned
a target, or allocated storage with the ALLOCATE statement.
You can use
the ASSOCIATED intrinsic function to determine the association
status of a pointer; see Chapter 11 “Intrinsic
procedures” for a description of this intrinsic. The example below,
ptr_sts.f90, illustrates different pointer operations, including
calls to the ASSOCIATED intrinsic to determine pointer status. Example 5-4 ptr_sts.f90  |
PROGRAM main ! This program performs simple pointer operations, including ! calls to the ASSOCIATED intrinsic to determine status. ! ! Declare pointer as a deferred shape array with POINTER ! attribute. REAL, POINTER :: ptr(:) REAL, TARGET :: tgt(2) = (/ -2.2, -1.1 /) ! initialize target PRINT *, "Initial status of pointer:" call get_ptr_sts ptr => tgt ! pointer assignment PRINT *, "Status after pointer assignment:" call get_ptr_sts PRINT *, "Contents of target by reference to pointer:", ptr ! use an array constructor to assign to tgt by reference to ptr ptr = (/ 1.1, 2.2 /) PRINT *, “Contents of target after assignment to pointer:”, tgt NULLIFY(ptr) PRINT *, "Status after pointer is nullified:" call get_ptr_sts ALLOCATE(ptr(5)) ! allocate pointer PRINT *, "Status after pointer is allocated:" ! To learn if pointer is allocated, call the ASSOCIATED ! intrinsic without the second argument IF (ASSOCIATED(ptr)) PRINT *, " Pointer is allocated." ptr = (/ 3.3, 4.4, 5.5, 6.6, 7.7 /) ! array assignment PRINT *, ‘Contents of array pointer:’, ptr DEALLOCATE(ptr) PRINT *, “Status after array pointer is deallocated:" IF (.NOT. ASSOCIATED(ptr)) PRINT *, " Pointer is deallocated." CONTAINS ! Internal subroutine to test pointer’s association status. ! Pointers can be passed to a procedure only if its interface ! is explicit to the caller. Internal procedures have an ! explicit interface. If this were an external procedure, ! its interface would have to be declared in an interface ! block to be explicit. SUBROUTINE get_ptr_sts IF (ASSOCIATED(ptr, tgt)) THEN PRINT *, " Pointer is associated with target." ELSE PRINT *, " Pointer is disassociated from target." END IF END SUBROUTINE get_ptr_sts END PROGRAM main |
 |
Here are the command lines to compile and execute the program,
along with the output from a sample run: $ f90 ptr_sts.f90 $ a.out Initial status of pointer: Pointer is disassociated from target. Status after pointer assignment: Pointer is associated with target. Contents of target by reference to pointer: -2.2 -1.1 Contents of target after assignment to pointer: 1.1 2.2 Status after pointer is nullified: Pointer is disassociated from target. Status after pointer is allocated: Pointer is allocated. Contents of array pointer: 3.3 4.4 5.5 6.6 7.7 Status after array pointer is deallocated: Pointer is deallocated. |
|