[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::fortran

Title:Digital Fortran
Notice:Read notes 1.* for important information
Moderator:QUARK::LIONEL
Created:Thu Jun 01 1995
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:1333
Total number of notes:6734

1267.0. "Array bounds checking problem?" by COMICS::EDWARDSN (Dulce et decorum est pro PDP program) Tue Apr 22 1997 10:14

A customer has asked for some explanation of the following behaviour and 
I don't think I can answer it. He thinks that there may be something 
wrong with assumed array sizes interfering with something in his code.
I just don't know what to think. It's looks a bit strange to me.
i.e. the bound program gets further when the array is SMALLER!

I'm confused.

Neil.



$ CREATE MAIN.FOR
        PROGRAM MAINPROG
        IMPLICIT NONE
        INCLUDE 'LIMITS.INC'
        INTEGER I,J
        REAL X (LIM1, LIM2)
        CALL SUBPROG (X)

        DO I=1, LIM2+1
          X (1, I) = 0.0
        ENDDO
        STOP
        END
$ CREATE SUB.FOR
        SUBROUTINE SUBPROG (Y)
        IMPLICIT NONE
        INCLUDE 'LIMITS.INC'
        INTEGER M,N
        REAL Y (LIM1, LIM2)
        WRITE (*,*) ' Subprog array limits = ', lim1, lim2
        DO m = 1, lim1+1
          DO n= 1, lim2+1
            WRITE (*,*) ' accessing array with m, n = ', m,n
            y (m,n) = m + n
            WRITE (*,*) ' array access ok for m,n = ', m,n
          ENDDO
        ENDDO
        RETURN
        END
$ CREATE LIMITS.INC
        integer lim1, lim2
        parameter (lim1=3,lim2=2)
$ FORT/DEB/NOOPT/CHECK=ALL MAIN
$ FORT/DEB/NOOPT/CHECK=ALL SUB
$ LINK MAIN,SUB
$ R MAIN
$ CREATE LIMITS.INC
        integer lim1, lim2
        parameter (lim1=3,lim2=2)
$ FORT/DEB/NOOPT/CHECK=ALL MAIN
$ FORT/DEB/NOOPT/CHECK=ALL SUB
$ LINK MAIN,SUB
$ R MAIN
T.RTitleUserPersonal
Name
DateLines
1267.1QUARK::LIONELFree advice is worth every centTue Apr 22 1997 10:214
The contents of LIMITS.INC is the same in both cases - what should it be?
I can't tell what the behavior is that you don't understand.

				Steve
1267.2Perhaps this is the answerWIBBIN::NOYCEPulling weeds, pickin' stonesTue Apr 22 1997 11:195
Also, what platform (VAX or Alpha) and what version of FORTRAN?

VAX FORTRAN compilers prior to V5 (I think) only checked whether the
final computed address was within the bounds of the array.  V5 and later,
and (I think) all Alpha FORTRAN compilers, check each individual subscript.
1267.3Humblest apologies.COMICS::EDWARDSNDulce et decorum est pro PDP programWed Apr 23 1997 04:4813
Sorry, my mistake, cut and paste error.

Second case should be:

$ CREATE LIMITS.INC
        integer lim1, lim2
        parameter (lim1=3,lim2=1)

and this is with Fortran FORT V6.5-188 on OpenVMS VAX 7.1

I'm having a bad week... make that month.

Neil.
1267.4QUARK::LIONELFree advice is worth every centWed Apr 23 1997 10:334
Ah - that's because a last bound of 1 is treated the same as * and is not
bounds-checked.

				Steve
1267.5TLE::EKLUNDAlways smiling on the inside!Wed Apr 23 1997 10:5614
    	It is fairly common practice to use a last bound of 1
    when the actual bound is something else.  The last bound is
    not needed in the address computation, and so many lazy
    programmers just give 1, as if they had given *.  In the 1966
    Fortran Standard, * had not yet been invented.  Because
    of this, we specifically do NOT check the upper bound of
    the last subscript if the bound is EITHER * or 1.  We check
    every other subscript against the actual lower and upper
    bounds - at compile time if possible, otherwise at run time
    (all assuming /check=bound was specified).
    
    Cheers!
    Dave Eklund
    
1267.6Is there any way of turning this feature off?COMICS::EDWARDSNDulce et decorum est pro PDP programThu Apr 24 1997 10:435
Can this be turned off?
Would it be possible to request a product enhancement to allow 
for the turning off of this feature.

Neil.
1267.7QUARK::LIONELFree advice is worth every centThu Apr 24 1997 11:174
Sure, you can request anything.  Why is this important?  Does your customer
regularly declare arrays with bounds of 1:1?  What for?

				Steve
1267.8You have a point.COMICS::EDWARDSNDulce et decorum est pro PDP programThu Apr 24 1997 11:4517
After pausing to laugh long and hard I realize that it does, indeed 
seem to be a rather peculiar request.

I think that the small example mirrors what they are doing in terms
of their simulation model. I can only assume that the data they collect
may have a number of passes through and that the collected data lives
in an array which is dimensioned by the last number.
Thus, the simulation will always be run at least once and maybe many
times. 
Hence the bound checking, since they want to check on only one run 
since to perform 2 runs would take much longer to make sure that there
are no bounds inconsistencies in this area.

I think that's what he's doing, I can't be sure. It could even be something
wierd involving parallel universes, but it's probably best not to pry.


1267.9Adjustable arrays to the rescueQUARK::LIONELFree advice is worth every centThu Apr 24 1997 12:0629
Suggest the following as an alternative, if this is important to them.  This
will check all the bounds no matter what they are:

        PROGRAM MAINPROG
        IMPLICIT NONE
        INCLUDE 'LIMITS.INC'
        INTEGER I,J
        REAL X (LIM1, LIM2)
        CALL SUBPROG (X,LIM1,LIM2)

        DO I=1, LIM2+1
          X (1, I) = 0.0
        ENDDO
        STOP
        END
        SUBROUTINE SUBPROG (Y,LIM1,LIM2)
        IMPLICIT NONE
        INTEGER M,N,LIM1,LIM2
        REAL Y (LIM1, LIM2)
        WRITE (*,*) ' Subprog array limits = ', lim1, lim2
        DO m = 1, lim1+1
          DO n= 1, lim2+1
            WRITE (*,*) ' accessing array with m, n = ', m,n
            y (m,n) = m + n
            WRITE (*,*) ' array access ok for m,n = ', m,n
          ENDDO
        ENDDO
        RETURN
        END