Fortran中的数组第一索引 [英] Array First Index in Fortran

查看:510
本文介绍了Fortran中的数组第一索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为Fortran中数组的第一个索引为1.但是为什么此代码起作用? (代码是Wavewatch的修改后的部分, http://polar.ncep.noaa.gov/waves/wavewatch/)

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

推荐答案

您已经知道,Fortran数组索引默认情况下基于1,但是程序员可以在所使用的整数类型范围内选择任何整数用于索引值.但是,您还应该注意其中的另一道皱纹.默认情况下,无论是在编译时(在许多情况下是不可能的)还是在运行时(可能但昂贵),Fortran均不检查数组索引表达式是否有界.

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.

有很多Fortran代码在这个问题上,而我遇到的情况是,一个程序显然已经正确运行了很多年而没有发现它的情况.使用编译器的选项创建该程序的版本,该版本在运行时检查数组边界表达式,然后运行并查看会发生什么情况.

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.

或者,正如您已经知道的,SIG可能已声明为以0作为其最低索引.

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

这篇关于Fortran中的数组第一索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆