Intel® Fortran Compiler 17.0 Developer Guide and Reference

MOVE_ALLOC

Intrinsic Subroutine (Generic): Moves an allocation from one allocatable object to another. Intrinsic subroutines cannot be passed as actual arguments.

CALL MOVE_ALLOC (from,to)

from

(Input; output) Can be of any type, type parameters, corank, and rank; it must be allocatable.

to

(Output) Must be type compatible with from and have the same rank and corank; it must be allocatable. to must be polymorphic if from is polymorphic. Each nondeferred type parameter of the declared type of to must have the same value as the corresponding type parameter of the declared type of from. For more information about type compatibility, see the description in CLASS.

If to is currently allocated, it is deallocated. If from is allocated, to becomes allocated with the same type, type parameters, array bounds, and values as from. Lastly, from is deallocated.

If to has the TARGET attribute, any pointer associated with from at the time of the call to MOVE_ALLOC becomes correspondingly associated with to. If to does not have the TARGET attribute, the pointer association status of any pointer associated with from on entry becomes undefined.

During execution of MOVE_ALLOC, the internal descriptor contents are copied from from to to, so that the storage pointed to by to is the storage that from used to point to.

Typically, MOVE_ALLOC is used to provide an efficient way to reallocate a variable to a larger size without copying the data twice.

Example

The following shows an example of how to increase the allocated size of X and keep the old values with only one copy of the old values. Using only assignment, a temporary variable named Y will be allocated and X assigned to Y. Then X will be deallocated and reallocated to be twice the size; then Y will be assigned to the first half of X. Finally, the temporary Y is deallocated.


    ! This program uses MOVE_ALLOC to make an allocated array X bigger and
    ! keep the old values of X in the variable X. Only one copy of the old values
    ! of X is needed.
        integer :: I, N = 2
        real, allocatable :: X(:), Y(:)
        allocate (X(N), Y(2*N))         ! Y is twice as big as X
        X = (/(I,I=1,N)/)       ! put "old values" into X
        Y = -1                  ! put different "old values" into Y
        print *, ' allocated of X is ', allocated (X)
        print *, ' allocated of Y is ', allocated (Y)
        print *, ' old X is ', X
        print *, ' old Y is ', Y
        Y (1:N) = X             ! copy all of X into the first half of Y
                                ! this is the only copying of values required
        print *, ' new Y is ', Y
        call move_alloc (Y, X)  ! X is now twice as big as it was, Y is
                                ! deallocated, the values were not copied from Y to X
        print *, ' allocated of X is ', allocated (X)
        print *, ' allocated of Y is ', allocated (Y)
        print *, ' new X is ', X
        end

The following shows the output for the above example:

    allocated of X is  T
    allocated of Y is  T
    old X is    1.000000       2.000000
    old Y is   -1.000000      -1.000000      -1.000000      -1.000000
    new Y is    1.000000       2.000000      -1.000000      -1.000000
    allocated of X is  T
    allocated of Y is  F
    new X is    1.000000       2.000000      -1.000000      -1.000000