question about negative indices in fortran 77 
Author Message
 question about negative indices in fortran 77

Date: Wed, 29 Dec 2004 10:53:52 -0500
| Dear friends,
| I am working with an "inherited" program written in fortran 77. The
| program works fine as it is (both on Compaq compilers for windows and
| Intel compilers for linux), but now I'm doing some modifications and I
| need to debug it. The problem is that some (I guess very old)
| subroutines use negative indices in arrays, so whenever I compile
| with Intel Fortran 8.1 for linux with -C option, the program stops at
| these calls and doesn't arrive at the interesting parts.
| I'm checking for violations of array bounds because the program is doing
| something really weird: a variable is changing its value inside a
| subroutine where it isn't touched (neither read nor written!). The only
| thing that I've seen trigger this kind of behavior in other languages
| has always been array bounds violations (even just reading forbidden
| memory).
| Now we arrive at the interesting part. Let's say I have the following
| declaration at the beginning of a subroutine:
|       implicit real*8 (a-h,o-z)
|       real*8 e(m*n),d(m*n),c(m*n),b(m*n),a(m*n),
|      &   ef(m*n),df(m*n),cf(m*n),bf(m*n),af(m*n),
|      &   x(m*n),y(m*n),r(m*n),up(m*n),aux(m*n)
| The way I understand it, an expression like d(-i) is equivalent to
| e(m*n-i).
No, any zero or negative subscript for this code is in error, and the
outcome is unpredictable.  Typical outcomes are that he program may produce
wrong results without warning, or it may simply crash (with a
protection exception), or do anything at all.
| Using this I can translate the whole subroutine into something
| more sensible that the de{*filter*} will allow. I actually did it with one
| subroutine, and the results are identical to what I obtained before the
| translation. Now I want to do the same thing to another subroutine that
| is very similar, but I have the following problem: there is a reference
| to e(-i), but since "e" is the first array in the declaration, where is
| the memory allocated for this?
There isn't any.  The reference e(-i) is illegal for the declaration shown.
It's probably a programming error.
| Please forgive me if this is a silly question in this forum. I am mostly
| a C/C++ guy and my head tends to explode just a bit when I see code that
| depends on the details of memory layout :-)
| Thank you very much in advance,
| --
| Jose Miguel Pasini

Mon, 18 Jun 2007 20:55:39 GMT  
 [ 1 post ] 

 Relevant Pages 

1. question about negative indices in fortran 77

2. Fortran 77 simple code question

3. some fortran 77 language question

4. Fortran 77 File I/O Write and Format Question

5. Fortran 77 question

6. Fortran 77 question

7. A few basic questions about FORTRAN 77 program

8. Question on FORtran 77 standard

9. File access question (Fortran 77) - please e-mail answer

10. Question about HP 9000/700 Fortran 77

11. Fortran 77 question

12. Fortran 77 syntax question


Powered by phpBB® Forum Software