I tried to use allocatable objects as dummy arguments, a new feature of Fortran 2003. Here is the code:
module demo
contains
subroutine initialize(array1, array2)
real, dimension(:,:), allocatable, intent(out) :: array1, array2
integer, parameter :: n = 4
allocate(array1(n, n))
allocate(array2(n, n))
array1 = 1
array2 = 0
end subroutine initialize
end module
program test
use demo
real, dimension(:,:), allocatable :: a, b
integer i, j
call initialize(a, b)
write(*,*) a
write(*,*) ''
write(*,*) b
write(*,*) ''
write(*,*) size(a,1), size(a,2), lbound(a), ubound(a)
write(*,*) size(b,1), size(b,2), lbound(b), ubound(b)
write(*,*) a(1,1)
end program
I compiled it with the following command with no compilation error:
PGF95 -g -C -Mallocatable=03 -Mbounds -Mchkstk -Mchkptr -Mchkfpstk -Mdclchk test.f90
However, I got runtime error for the last sentence “write(,) a(1,1)”:
0: Subscript out of range for array a (test.f90: 24)
subscript=1, lower bound=4446251, upper bound=8458106, dimension=1
It seems that both arrays have been returned to the main program with correct sizes and bounds. I just can’t figure out why such error occurred. Had I made any mistake here? Thanks!
Andrew