如果引用数组元素的索引实际上超出了它的假定范围,我很困惑 Fortran 如何处理这种情况。
下面是一个简单的代码来说明这个问题:
PROGRAM test_matrix_out
USE mod_writearray
IMPLICIT NONE
INTEGER :: i,j,m,n
REAL :: k
REAL, Dimension(:,:),ALLOCATABLE :: A
m = 3
n = 4
ALLOCATE(A(m,n))
k = 1
DO i=1,m
DO j=1,n
A(i,j)=k
k=k+1
ENDDO
ENDDO
CALL writearray(A)
WRITE(*,*)
WRITE(*,*) A(1,:)
WRITE(*,*)
WRITE(*,*) A(2,:)
WRITE(*,*)
WRITE(*,*) A(0,:)
WRITE(*,*)
WRITE(*,*) A(4,:)
WRITE(*,*)
WRITE(*,*) A(5,:)
WRITE(*,*)
WRITE(*,*) A(100,:)
WRITE(*,*)
WRITE(*,*) A(:,1)
WRITE(*,*)
WRITE(*,*) A(:,2)
WRITE(*,*)
WRITE(*,*) A(:,0)
WRITE(*,*)
WRITE(*,*) A(:,4)
WRITE(*,*)
WRITE(*,*) A(:,5)
WRITE(*,*)
WRITE(*,*) A(:,100)
DEALLOCATE(A)
END PROGRAM test_matrix_out
它给了我以下结果:
1.000000 2.000000 3.000000 4.000000
5.000000 6.000000 7.000000 8.000000
0.0000000E+00 9.000000 10.00000 11.00000
2.000000 3.000000 4.000000 0.0000000E+00
6.000000 7.000000 8.000000 0.0000000E+00
0.0000000E+00 0.0000000E+00 0.0000000E+00 0.0000000E+00
1.000000 5.000000 9.000000
2.000000 6.000000 10.00000
-1.0097448E-28 8.9776148E-39 0.0000000E+00
4.000000 8.000000 12.00000
0.0000000E+00 0.0000000E+00 0.0000000E+00
-3.3631163E-44 1.4293244E-43 0.0000000E+00
为什么会这样?