I am not sure if this is a compiler bug or how Fortran is supposed to work, but if I allocate an allocatable array, then later deallocate it, then the SIZE function returns the size it was previously allocated with.
PROGRAM ALLOCATOR ! DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE:: X ! ALLOCATE(X(10)) ! WRITE(*,*) SIZE(X),ALLOCATED(X) ! >> 10 T ! DEALLOCATE(X) ! WRITE(*,*) SIZE(X),ALLOCATED(X) ! >> 10 F ! END PROGRAM
Should it not return zero since the array no longer has any size?
Such things are only reliable if the array has been allocated. Not a bug as such. It is at the same level as local variables in subroutines and functions not retaining their values between calls, even if they appear to (unless they have the SAVE attribute). The same goes for pointer variables.
As Arjen says, your program violates the Fortran standard by asking for the SIZE of an unallocated array. Here are the standard's words about the argument to SIZE:
shall be an array of any type. It shall not be an unallocated allocatable variable or a pointer that is not associated.
It is always possible to override (not technically) and use an alternative routine if it is necessary to make decisions based on the size of an array.
PROGRAM ALLOCATOR DOUBLE PRECISION, DIMENSION(:), ALLOCATABLE:: X ! ALLOCATE(X(10)) ! WRITE(*,*) SIZEO(X),ALLOCATED(X) ! >> 10 T ! DEALLOCATE(X) ! WRITE(*,*) SIZEO(X),ALLOCATED(X) ! >> 0 F CONTAINS ! FUNCTION SIZEO(VAR) RESULT(ASIZE) DOUBLE PRECISION, ALLOCATABLE:: VAR(:) INTEGER :: ASIZE IF (.NOT.(ALLOCATED(VAR))) THEN ASIZE = 0 ELSE ASIZE = SIZE(VAR) END IF END FUNCTION SIZEO ! END PROGRAM