DEALLOCATE

Purpose

The DEALLOCATE statement dynamically deallocates allocatable objects and pointer targets. A specified pointer becomes disassociated, while any other pointers associated with the target become undefined.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-DEALLOCATE--------------------------------------------------->

>--(--allocate_object_list--+-----------------------------+--+-------------------------------------+--)-><
                            '-,--STAT-- = --stat_variable-'  |                                 (1) |      
                                                             '-,--ERRMSG-- = --errmsg_variable-----'      

Notes:
  1. Fortran 2003
allocate_object
is a data pointer or an allocatable object
stat_variable
is a scalar integer variable
errmsg_variable
is a scalar character variable

Rules

An allocatable object that appears in a DEALLOCATE statement must be currently allocated.

When the result of a referenced function is allocatable, or has a structure with allocatable subobjects, that result and any allocated allocatable subobjects are deallocated after execution of the innermost executable construct containing the function reference.

An allocatable object with the TARGET attribute cannot be deallocated through an associated pointer. Deallocation of such an object causes the association status of any associated pointer to become undefined. An allocatable object that has an undefined allocation status cannot be subsequently referenced, defined, allocated, or deallocated. Successful execution of a DEALLOCATE statement causes the allocation status of an allocatable object to become not allocated.

An object being deallocated will be finalized first. When a variable of derived type is deallocated, any allocated subobject with the ALLOCATABLE attribute is also deallocated. If an allocatable component is a subojbect of a finalizable object, that object is finalized before the component is automatically deallocated.

When an intrinsic assignment statement is executed, any allocated subobject of the variable is deallocated before the assignment takes place.

A pointer that appears in a DEALLOCATE statement must be associated with a whole target that was created with an ALLOCATE statement. Deallocation of a pointer target causes the association status of any other pointer associated with all or part of the target to become undefined.

Tips

Use the DEALLOCATE statement instead of the NULLIFY statement if no other pointer is associated with the allocated memory.

Deallocate memory that a pointer function has allocated.

If the STAT= specifier is not present and an error condition occurs during execution of the statement, the program terminates. If the STAT= specifier is present, stat_variable is assigned one of the following values:

Stat value Error condition
0 No error
1 Error in system routine attempting to do deallocation
2 An invalid data object has been specified for deallocation
3 Both error conditions 1 and 2 have occurred

If an error condition occurs during execution of the DEALLOCATE statement, an explanatory message is assigned to errmsg_variable. If no such condition occurs, the value of errmsg_variable is not changed.

An allocate_object must not depend on the value, bounds, allocation status, or association status of another allocate_object in the same DEALLOCATE statement; nor does it depend on the value of the stat_variable or errmsg_variable in the same DEALLOCATE statement.

stat_variable and errmsg_variable must not be deallocated within the same DEALLOCATE statement. The variable must not depend on the value, bounds, allocation status, or association status of any allocate_object in the same DEALLOCATE statement.

Examples

INTEGER, ALLOCATABLE :: A(:,:)
INTEGER X,Y
      .
      .
      .
ALLOCATE (A(X,Y))
      .
      .
      .
DEALLOCATE (A,STAT=I)
END

Related information