Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
10 Views

Array SIZE>0 When Not ALLOCATED

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?

 

Thanks

0 Kudos
3 Replies
Highlighted
Valued Contributor II
10 Views

Such things are only reliable

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.

0 Kudos
Highlighted
Black Belt
10 Views

As Arjen says, your program

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.

Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran
0 Kudos
Highlighted
Beginner
10 Views

It is always possible to

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

 

0 Kudos