Zero Arrays and Checking Array Boundaries

When compiling with GNU Fortran (v4.4.3) or Sun Studio F95 (v8.3) and without array limitations, the following program is checked without errors. However, when you enable array bounds checking ( gfortran -fbounds-check and f95 -C , respectively), the GNU executable executes again without errors, while the compiled Sun Studio executable gives an error at runtime.

  ****** 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' 

This is an error when calling sub2() , which uses the argument of the automatic array argument for x . The sub1() call is executed normally with any compiler and any flags.

As far as I know, this program is โ€œlegitimateโ€ because a zero-sized array can be referenced as an array of non-zero size, and there is no explicit indexing of a zero-length dimension x . But is there some kind of zero size array or automatic array complexity that I'm missing here? And should I expect that checking the bounds of the array will behave the same for different compilers, or should I consider this extension for the provider?

 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 
+4
source share
1 answer

This does not give any problems with the fortr compiler with the -check options; and IBM xlf, which in my experience is extremely strict, also did not complain about -qcheck.

But more broadly, yes, there is no standard on what border checks should or should not do. I can, of course, understand why some compilers will mark assignment to an array of zero length as bad / wrong / strange; this is a strange corner case.

+3
source

Source: https://habr.com/ru/post/1347704/


All Articles