fortran - Zero sized arrays and array bounds checking -


when compiled either gnu fortran (v4.4.3) or sun studio f95 (v8.3) , no array bounds checking following program runs without error. however, when array bounds checking switched on (gfortran -fbounds-check , f95 -c, respectively) gnu compiled executable runs again without error, whereas sun studio compiled executable gives run-time error,

 ******  fortran run-time system  ****** subscript out of range. location:  line 44 column 20 of 'nosize.f90' subscript number 2 has value 1 in array 't$27' 

that's error in call sub2(), uses automatic array dummy argument x. sub1() calls run fine either compiler , flags.

to knowledge program "legal", in 0 sized array may referenced non-zero sized array, , there no explicit indexing of 0 length dimension of x. there 0 sized array slicing or automatic array subtlety i'm missing here? , should expect array bounds checking behave same across different compilers, or should consider vendor-specific extension?

module subs   implicit none contains       subroutine sub1(x)     implicit none     real :: x(:,:)     print*,'------------------------------------'     print*,shape(x)     print*,size(x)   end subroutine sub1    subroutine sub2(n1,n3,x)     implicit none     integer,intent(in) :: n1, n3     real :: x(n1,n3)     print*,'------------------------------------'     print*,shape(x)     print*,size(x)   end subroutine sub2 end module subs   program nosize   use subs   implicit none       integer :: n1 = 2, n2 = 2, n3 = 0   real,allocatable :: x(:,:,:)    allocate(x(n1,n2,n3))   x(:,:,:) = -99.9    print*,'allocated? ',allocated(x)   print*,'shape =',shape(x)   print*,'size  =',size(x)   print*,'x     =',x    call sub1(x(:,1,:))   call sub2(n1,n3,x(:,1,:))  end program nosize 

it doesn't give problems intel's fortran compiler -check bounds; , ibm's xlf, in experience extremely strict, didn't complain -qcheck.

but more broadly, yes, there's no standard bounds checking should or shouldn't do. can see why compilers would flag assignment zero-length array being bad/wrong/weird; strange corner-case.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -