Converted document

FORTRAN 90+: POINTERS

A pointer is used to reference other pieces of memory so that if operations must be performed on the memory it might be possible to avoid moving the memory around, instead calling it using the pointer in a less computationally costly way.
Pointer Declaration
Pointers are declared using the same format that all other variables, they are just given the pointer attribute. To illustrate,
REAL, POINTER :: ptor
REAL, DIMENSION(:,:), POINTER :: ptoa
INTEGER, POINTER :: ptoi
CHARACTER, POINTER :: ptoc
Pointers also have a type (REAL, INTEGER, LOGICAL, etc.) attribute. This is important to know as a pointer can only point to variables with the same declaration type. Another important point is that pointers that will be referencing arrays always have a deferred-shape array specification. This means that the DIMENSION statement will always be followed by (:,:) instead of numbers. Also note that pointers can not have the attributes ALLOCATABLE or PARAMETER.
TARGET DECLARATION
In order for a variable to be targeted by a pointer they must have the TARGET attribute. This is done when declaring the variables:
REAL, TARGET :: var1, var2
REAL, DIMENSION(5,3), TARGET :: var3, var4
INTEGER, TARGET :: var5
CHARACTER, TARGET :: var6
The only rule for targets are that can only be associated with a pointers of the same type.
POINTER STATUS
The status of a pointer can be determined using the ASSOCIATED command. This command returns either a .TRUE., a .FALSE., or an undefined error result. The format to use the ASSOCIATED( ) command is
ASSOCIATED(pointer_name, variable) !the variable is where
          !the logical is assigned.
A result of .TRUE. means that the pointer has a target. A .FALSE. means that the pointer is disassociated. The last of option of an error is obviously undesirable, so it is advisable to make a pointer disassociated when it is created. This is done with the NULLIFY statement. The format for this command is
NULLIFY(pointer_name)
This command simply breaks the connection, but it does not free up the space used by the target. If the programmer wants to free up the space used by the target, the DEALLOCATE(pointer_name, STAT=ierr) statement should be uses. This releases the memory used for the target.This is an important task in large data intensive programs, but it means that the target is no longer usable. Both of these methods leave the target and pointer in a nullified state. Thus, they will not return an error if the ASSOCIATED statement is used on them.
POINTER ASSIGNMENT
To assign a pointer to a target, an equals to or greater than symbol is applied:
Pointer_name => variable_name
It is also possible to assign a pointer to another pointer. The result is that the two pointer names refer to the same data set and can be used interchangable.
Pointers can also be used to set the value of an object referenced to that of another variable. This is done by setting a pointer (left hand side) equal to a variable or another pointer:
x= 3.141592
ptrr2=x
ptrr=>y
ptrr=x !y=x is the result
ptrr=ptrr2 !same result
If ’x’ is changed, and the first method of assignment is used [ptrr=x] then the value of y does not change because it is not directly associated with the value of x except in that one instance. In other words, neither ’y’ nor ’ptrr’ points to ’x’.
Next Page →
Next Page →