02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Description<br />

<strong>HP</strong> <strong>Fortran</strong> statements<br />

ALLOCATE<br />

The ALLOCATE statement creates space for allocatable arrays and targets for variables (scalars<br />

or arrays) with the POINTER attribute. The ALLOCATE and DEALLOCATE statements give the<br />

user the ability to manage space dynamically at execution time.<br />

For allocatable arrays, an error occurs when an attempt is made to allocate an already<br />

allocated array or to deallocate an array that is not allocated. The ALLOCATED intrinsic<br />

function may be used to determine whether an allocatable array is allocated.<br />

A pointer can be associated with a target, either with the pointer assignment statement or by<br />

use of the ALLOCATE statement. It is not an error to allocate an already associated pointer; its<br />

old target connection is replaced by a connection to the newly allocated space. However, if the<br />

previous target was allocated and no other pointer became associated with it, the space is no<br />

longer accessible.<br />

Examples<br />

In the following example, a complex array with the POINTER attribute is declared. Target<br />

space is allocated to it at run-time, the amount being determined by two integer values read<br />

in. Later in the program, the space is recovered by use of the DEALLOCATE statement.<br />

COMPLEX, POINTER :: hermitian (:, :)<br />

READ *, m, n<br />

ALLOCATE (hermitian (m, n))<br />

DEALLOCATE (hermitian, STAT = ierr)<br />

In the next example, a real allocatable array is declared. The amount of space allocated to it<br />

depends on how much is available.<br />

! Rank-2 allocatable array<br />

REAL, ALLOCATABLE :: intense(:,:)<br />

CALL init_i_j(i, j)<br />

DO<br />

ALLOCATE (intense(i, j), STAT = ierr4)<br />

! ierr4 will be positive if there is not enough space to<br />

! allocate this array<br />

IF (ierr4 == 0) EXIT<br />

i = i/2; j = j/2<br />

END DO<br />

The derived type node in the next example is the basis of a binary tree structure. It consists of<br />

a real value component (val) and two pointer components, left and right, bothoftypenode.<br />

The variable top (of type node) is declared, and space is allocated for targets for the pointers<br />

top%left and top%right.<br />

Chapter 10 243

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!