Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 10 HP Fortran 90 statements

ALLOCATE

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

Provides storage space for allocatable arrays and pointer targets.

Syntax

ALLOCATE (allocation-list[, STAT= scalar-integer-variable]) 
allocation-list

is a comma-separated list of allocation.

allocation

is allocate-object [(allocate-shape-spec-list)].

allocate-object

is variable-name or derived-type-component. Each allocate-object must be an allocatable array or a pointer.

allocate-shape-spec-list

is a comma-separated list of allocate-shape-spec.

allocate-shape-spec

is [lower-bound:]upper-bound. The bounds in an allocate-shape-spec must be scalar integer expressions.

STAT=scalar-integer-variable

returns the error status after the statement executes. If given, it is set to zero if the statement successfully executed, and to one of the following nonzero values if an error occurred:

1

Error occurred after the array was allocated; for example, an attempt to allocate a previously allocated array.

2

Dynamic memory allocation failure (memory not available) or invalid size (array too large).

3

Errors of both types 1 and 2 have occurred. This kind of an error can only occur if the same ALLOCATE statement is used to allocate more than one array, and both kinds of errors occur.

If there is no scalar-integer-variable, the occurrence of an error causes the program to terminate.

Description

The ALLOCATE statement creates space for allocatable arrays and targets for variables (scalars or arrays) with the POINTER attribute. The ALLOCATE and DEALLOCATE statements give the user the ability to manage space dynamically at execution time.

For allocatable arrays, an error occurs when an attempt is made to allocate an already allocated array or to deallocate an array that is not allocated. The ALLOCATED intrinsic function may be used to determine whether an allocatable array is allocated.

A pointer can be associated with a target, either with the pointer assignment statement or by use of the ALLOCATE statement. It is not an error to allocate an already associated pointer; its old target connection is replaced by a connection to the newly allocated space. However, if the previous target was allocated and no other pointer became associated with it, the space is no longer accessible.

Examples

In the following example, a complex array with the POINTER attribute is declared. Target space is allocated to it at run-time, the amount being determined by two integer values read in. Later in the program, the space is recovered by use of the DEALLOCATE statement.

COMPLEX, POINTER :: hermitian (:, :)
READ *, m, n
ALLOCATE (hermitian (m, n))
DEALLOCATE (hermitian, STAT = ierr)

In the next example, a real allocatable array is declared. The amount of space allocated to it depends on how much is available.

! Rank-2 allocatable array
REAL, ALLOCATABLE :: intense(:,:)

CALL init_i_j(i, j)
DO
ALLOCATE (intense(i, j), STAT = ierr4)
! ierr4 will be positive if there is not enough space to
! allocate this array
IF (ierr4 == 0) EXIT
i = i/2; j = j/2
END DO

The derived type node in the next example is the basis of a binary tree structure. It consists of a real value component (val) and two pointer components, left and right, both of type node. The variable top (of type node) is declared, and space is allocated for targets for the pointers top%left and top%right.

The ALLOCATE and DEALLOCATE statements and pointer variables of type node make it possible to allocate space for nodes in such a tree structure, traverse it as required, and then recover the space when it is no longer needed.

TYPE node
REAL val
TYPE(node), POINTER :: left, right ! Pointer components
END TYPE node
TYPE(node) top
ALLOCATE (top % left, top % right)

In the final example, two CHARACTER arrays, para and key, are declared with the POINTER attribute. para is allocated space; key is made to point at a section of para.

! Pointers to char arrays
CHARACTER, POINTER :: para(:), key(:)

CALL init_k_m(k, m)
ALLOCATE (para(1000))
key => para (k : k + m)

Related statements

ALLOCATABLE (statement and attribute), DEALLOCATE, NULLIFY, and POINTER (statement and attribute)

Related concepts

For related information, see the following:

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.