Q : legacy of negative index 
Author Message
 Q : legacy of negative index

Hello,

Is it legal (f77 and f90) to use a negative index in an assumed size
array ?

It works fine with ordinary unix compilers (hp, cray, sgi, sun,...)
but it is rejected by the NAG f90 compiler.

Anyway, is there a workaround to fix it ?

See an example below.

Regards,
Arnaud Desitter

      program negati
      implicit none
      real a(30)
      integer i
      do 10, i=1,30
        a(i)=i
10    continue
      call tst1(a(10))
      end

      subroutine tst1(a1)
      implicit none
      real a1(*)
      integer i
c problem ??
      write(*,*) a1(-1)
      do 10, i=(1-10+1),(30-10+1)
        write(*,*) a1(i)
10    continue
      return
      end



Mon, 25 Oct 1999 03:00:00 GMT  
 Q : legacy of negative index



Quote:
> Hello,

> Is it legal (f77 and f90) to use a negative index in an assumed size
> array ?

        Illegal, but indirectly  so.   With  an  assumed size array, the
    bounds  are  not  specified.   _Therefore_, a lower bound  of  1  is
    assumed.  Thus a negative (or zero) index is an  attempt  to  access
    the array outside its (implicitly) declared bounds.  Illegal.

Quote:
> It works fine with ordinary unix compilers (hp, cray, sgi, sun,...)
> but it is rejected by the NAG f90 compiler.

        Compilers/programs are not  _required_  to issue diagnostics for
    non-conforming  programs.  The term "works fine" is always  suspect.
    I expect you mean the program didn't crash...

Quote:
> Anyway, is there a workaround to fix it ?

        I'm sorry,  what's  the  problem? :-)  If  anything  needs to be
    fixed, it's the non-conforming program that tries to access an array
    outside its declared bounds.

Quote:
> See an example below.
[...]
>       program negati
>       implicit none
>       real a(30)
>       integer i
>       do 10, i=1,30
>         a(i)=i
> 10    continue
>       call tst1(a(10))
>       end

>       subroutine tst1(a1)
>       implicit none
>       real a1(*)
>       integer i
> c problem ??
>       write(*,*) a1(-1)
>       do 10, i=(1-10+1),(30-10+1)
>         write(*,*) a1(i)
> 10    continue
>       return
>       end

        If you  really  want  to  access  the  array  `a1'  with indices
    spanning  -9  to  +20, then pass those indices  to  the  subroutine.
    Something like this:

        SUBROUTINE TST2 (A1, L, U)
        IMPLICIT NODE
        INTEGER  L,U
        REAL     A1(L:U)
        INTEGER  I

        WRITE (*,*) A1(-1)
        DO 10, i=L,U
          write(*,*) a1(i)
  10    continue
        RETURN
        END

    Of course, now your calling  routine  needs  to be changed to supply
    the correct arguments:

        CALL TST2 (A, -9, 20)

    However, what you're trying to  do  is pass a _starting_ address and
    access  elements that _precede_ that address.  This is not  allowed.
    Not that, as you call it, "legacy"  code  doesn't  use  these  dirty
    tricks, but they are dirty tricks and should be eliminated.

        -Ken
--

 SLAC, P.O.Box 4349, MS 46  |  DECnet:   45537::FAIRFIELD (45537=SLACVX)
 Stanford, CA   94309       |  Voice:    415-926-2924    FAX: 415-926-3515
 -------------------------------------------------------------------------
 These opinions are mine, not SLAC's, Stanford's, nor the DOE's...



Mon, 25 Oct 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. negative array index

2. Negative array indices

3. question about negative indices in fortran 77

4. question about negative indices in fortran 77

5. Built-in func to calculate negative indices??

6. several Qs

7. Qs on CLUBMGR, Template BUG ???

8. Qs

9. Qs about language nomenclature

10. New newbies Qs on Ruby

11. Newbie Qs: startup time, Module.constants, chdir, Ruby distro's library

12. Qs on Ruby and DbC/Smalltalk

 

 
Powered by phpBB® Forum Software