ALLOCATED(3) - [ARRAY:INQUIRY] Allocation status of an allocatable entity
result = allocated(array|scalar)
logical function allocated(array,scalar)type(TYPE(kind=**)),allocatable,optional :: array(..) type(TYPE(kind=**)),allocatable,optional :: scalar
o a kind designated as ** may be any supported kind for the type o ARRAY may be any allocatable array object of any type. o SCALAR may be any allocatable scalar of any type. o the result is a default logical scalar
ALLOCATED(3) checks the allocation status of both arrays and scalars.
At least one and only one of ARRAY or SCALAR must be specified.
o ENTITY : the allocatable object to test.
If the argument is allocated then the result is .true.; otherwise, it returns .false..
Sample program:
program demo_allocated use,intrinsic :: iso_fortran_env, only : dp=>real64,sp=>real32 implicit none real(kind=sp), allocatable :: x(:) character(len=256) :: message integer :: istat ! basics if( allocated(x)) then write(*,*)do things if allocated else write(*,*)do things if not allocated endifResults:! if already allocated, deallocate if ( allocated(x) ) deallocate(x,STAT=istat, ERRMSG=message ) if(istat.ne.0)then write(*,*)trim(message) stop endif
! only if not allocated, allocate if ( .not. allocated(x) ) allocate(x(20))
! allocation and intent(out) call intentout(x) write(*,*)note it is deallocated!,allocated(x)
contains
subroutine intentout(arr) ! note that if arr has intent(out) and is allocatable, ! arr is deallocated on entry real(kind=sp),intent(out),allocatable :: arr(:) write(*,*)note it was allocated in calling program,allocated(arr) end subroutine intentout
end program demo_allocated
> do things if not allocated > note it was allocated in calling program F > note it is deallocated! F
Fortran 95. allocatable scalar entities were added in Fortran 2003.
MOVE_ALLOC(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | allocated (3fortran) | November 02, 2024 |