# DEALLOCATE

Statement: Frees the storage allocated for allocatable variables and nonprocedure pointer targets (and causes the pointers to become disassociated).

DEALLOCATE (object[,object]...[, dealloc-opt])

object | Is a structure component or the name of a variable, and must be a pointer or allocatable variable.
dealloc-opt | (Output) Is one of the following:
STAT=stat-var | stat-var is a scalar integer variable in which the status of the deallocation is stored.
ERRMSG=err-var | err-var is a scalar default character value in which an error condition is stored if such a condition occurs.

## Description

If a STAT= variable or ERRMSG= variable is specified, it must not be deallocated in the DEALLOCATE statement in which it appears. If the deallocation is successful, the STAT= variable is set to zero and the ERRMSG= variable is unchanged. If the deallocation is not successful, an error condition occurs, the STAT= variable is set to a positive integer value (representing the run-time error), and the ERRMSG= variable is defined with a descriptive message about the error condition. If no STAT= variable is specified and an error condition occurs, error termination is initiated.

If an ALLOCATE or DEALLOCATE statement with a coarray allocatable object is executed when one or more images has initiated termination of execution, the STAT= variable becomes defined with the processor-dependent positive integer value of the constant STAT_STOPPED_IMAGE from the intrinsic module ISO_FORTRAN_ENV. Otherwise, if an allocatable object is a coarray and one or more images of the current team has failed, the STAT= variable becomes defined with the processor-dependent positive integer value of the constant STAT_FAILED_IMAGE from the intrinsic module ISO_FORTRAN_ENV.

If any other error condition occurs during execution of the ALLOCATE or DEALLOCATE statement, the STAT= variable becomes defined with a processor-dependent positive integer value different from STAT_STOPPED_IMAGE or STAT_FAILED_IMAGE.

If an ALLOCATE or DEALLOCATE statement with a coarray allocatable object is executed when one or more images of the current team has failed, each allocatable object is successfully allocated or deallocated on the active images of the current team. If any other error occurs, the allocation status of each allocatable object is processor dependent:

- Successfully allocated allocatable objects have the allocation status of allocated, or associated if the allocate object is has the POINTER attribute.

- Successfully deallocated allocatable objects have the allocation status of deallocated, or disassociated if the allocatable object has the POINTER attribute.

- An allocatable object that was not successfully allocated or deallocated has its previous allocation status, or its previous association status if it has the POINTER attribute.

It is recommended that all explicitly allocated storage be explicitly deallocated when it is no longer needed.

To disassociate a pointer that was not associated with the ALLOCATE statement, use the NULLIFY statement.

For a list of run-time errors, see Error Handling in the Compiler Reference.

## Example

The following example shows deallocation of an allocatable array:

    INTEGER ALLOC_ERR
    REAL, ALLOCATABLE :: A(:), B(:,:)
    ...
    ALLOCATE (A(10), B(-2:8,1:5))
    ...
    DEALLOCATE(A, B, STAT = ALLOC_ERR)

The following shows another example:

    INTEGER, ALLOCATABLE :: dataset(:,:,:)
    INTEGER reactor, level, points, error
    DATA reactor, level, points / 10, 50, 10 /
    ALLOCATE (dataset(1:reactor,1:level,1:points), STAT = error)
    DEALLOCATE (dataset, STAT = error)

Parent topic: C to D

## See Also

ALLOCATE
NULLIFY
Arrays
Dynamic Allocation
ISO_FORTRAN_ENV Module