开发者

Zero sized arrays and array bounds checking

When compiled with either GNU Fortran (v4.4.3) or Sun Studio F95 (v8.3) and no array bounds checking the following program runs without error. However, when array bounds checking is switched on (gfortran -fbounds-check and f95 -C, respectively) the GNU compiled executable runs again without error, whereas the Sun Studio compiled executable gives the run-time error,

 ******  FORTRAN RUN-TIME SYSTEM  ******
Subscript out of range. Location:  line 44 column 20 of 'nosize.f90'
Subscript number 2 has va开发者_StackOverflowlue 1 in array 't$27'

That's an error in the call to sub2(), which uses an automatic array dummy argument for x. The sub1() calls run fine with either compiler and any flags.

To my knowledge this program is "legal", in that a zero sized array may be referenced like a non-zero sized array, and there is no explicit indexing of the zero length dimension of x. But is there some zero sized array slicing or automatic array subtlety that I'm missing here? And should I expect array bounds checking to behave the same across different compilers, or should I consider it a 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 any problems with intel's fortran compiler with -check bounds; and IBM's xlf, which in my experience is extremely strict, also didn't complain with -qcheck.

But more broadly, yes, there's no standard about what bounds checking should or shouldn't do. I can certainly see why some compilers would flag an assignment to a zero-length array as being bad/wrong/weird; it is a strange corner-case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜