Manual Reference Pages  - move_alloc (3fortran)

NAME

MOVE_ALLOC(3) - [MEMORY] Move allocation from one object to another

SYNOPSIS

call move_alloc(from, to [,stat] [,errmsg] )

         subroutine move_alloc(from, to)

type(TYPE(kind=**)),intent(inout),allocatable :: from(..) type(TYPE(kind=**)),intent(out),allocatable :: to(..) integer(kind=**),intent(out) :: stat character(len=*),intent(inout) :: errmsg

CHARACTERISTICS

o FROM may be of any type and kind.
o TO shall be of the same type, kind and rank as FROM.

DESCRIPTION

MOVE_ALLOC(3) moves the allocation from FROM to TO. FROM will become deallocated in the process.

This is potentially more efficient than other methods of assigning the values in FROM to TO and explicitly deallocating FROM, which are far more likely to require a temporary object or a copy of the elements of the array.

OPTIONS

o FROM : The data object to be moved to TO and deallocated.
o TO : The destination data object to move the allocated data object FROM to. Typically, it is a different shape than FROM.
o STAT : If STAT is present and execution is successful, it is assigned the value zero. : If an error condition occurs,
o if **stat** is absent, error termination is initiated;
o otherwise, if **from** is a coarray and the current team contains a stopped image, **stat** is assigned the value STAT\_STOPPED\_IMAGE from the intrinsic module ISO\_FORTRAN\_ENV;
o otherwise, if **from** is a coarray and the current team contains a failed image, and no other error condition occurs, **stat** is assigned the value STAT\_FAILED\_IMAGE from the intrinsic module ISO\_FORTRAN\_ENV;
o otherwise, **stat** is assigned a processor-dependent positive value that differs from that of STAT\_STOPPED\_IMAGE or STAT\_FAILED\_IMAGE.
o ERRMSG : If the ERRMSG argument is present and an error condition occurs, it is assigned an explanatory message. If no error condition occurs, the definition status and value of ERRMSG are unchanged.

EXAMPLES

Basic sample program to allocate a bigger grid

    program demo_move_alloc
    implicit none
    ! Example to allocate a bigger GRID
    real, allocatable :: grid(:), tempgrid(:)
    integer :: n, i

! initialize small GRID n = 3 allocate (grid(1:n)) grid = [ (real (i), i=1,n) ]

! initialize TEMPGRID which will be used to replace GRID allocate (tempgrid(1:2*n)) ! Allocate bigger grid tempgrid(::2) = grid ! Distribute values to new locations tempgrid(2::2) = grid + 0.5 ! initialize other values

! move TEMPGRID to GRID call MOVE_ALLOC (from=tempgrid, to=grid)

! TEMPGRID should no longer be allocated ! and GRID should be the size TEMPGRID was if (size (grid) /= 2*n .or. allocated (tempgrid)) then print *, "Failure in move_alloc!" endif print *, allocated(grid), allocated(tempgrid) print ’(99f8.3)’, grid end program demo_move_alloc

Results:

    T F

1.000 1.500 2.000 2.500 3.000 3.500

STANDARD

Fortran 2003, STAT and ERRMSG options added 2018

SEE ALSO

ALLOCATED(3)

fortran-lang intrinsic descriptions


Nemo Release 3.1 move_alloc (3fortran) April 28, 2024
Generated by manServer 1.08 from 634c9433-d682-4928-a1b5-53e01b9c51da using man macros.