Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array First Index in Fortran

Tags:

arrays

fortran

I thought the first index of an array in Fortran is 1. But why does this code work? (Code is a modified segment of Wavewatch, http://polar.ncep.noaa.gov/waves/wavewatch/)

DO KINDEX=0, TOTAL+1
NUM    = NUM * SCALE
SIG (KINDEX) = NUM
END DO
like image 745
trailm Avatar asked Oct 19 '25 13:10

trailm


1 Answers

As you've already been told Fortran array indexing is, by default, 1-based but the programmer can choose any integer within the range of the integer kind used for index values. There is, though, another wrinkle of which you should be aware. Fortran doesn't by default, either at compile-time (where it would be impossible in many cases) or at run-time (possible but expensive), check that array index expressions are in bounds.

There is a lot of Fortran code in the wild with this problem and I've come across cases where a program has worked, apparently correctly, for many years without this being spotted. Use your compiler's options to create a version of the program which checks array bounds expressions at run-time, run it and see what happens.

Or, as you've already been told, SIG may have been declared with 0 as its lowest index.

like image 164
High Performance Mark Avatar answered Oct 21 '25 05:10

High Performance Mark