Fortran 编译器将属性视为 'save attributes' 的差异?
Fortran compiler differences in treating properties as 'save attributes'?
我们有这个旧的 Fortran 脚本,我们正在尝试使用 Intel 的 Visual Fortran 重新编译它,但是我们得到了计算错误和与代码的旧编译版本不同的结果。
我们在下面的代码中发现了我们认为是问题的地方(它大致来自 Numerical Recipes)。
问题是每次调用都会重置 'it' 参数,但是应该在函数调用之间存储它。
我们对可能出现问题的最佳猜测是,较旧的编译器可能将 'it' 视为 'save attribute',因此将其存储在函数调用之间。
我们在这里可能是完全错误的,如果一些 Fortran 大师可以证实这一点或有更好的解释,我们会很高兴得到一些帮助!
subroutine TrapezoidalRule(Func, a, b, s, n)
*
* This routine performs the trapezoidal rule, see Numerical Recipes
*
implicit none
real*8 Func, a, b, s
Integer*4 n
external Func
*
real*8 del, x, sum
Integer*4 it, tnm, j
*
if (n .eq. 1) then
*
s=0.5d0*(b-a)*(Func(a)+Func(b))
it=1
*
else
*
tnm=it
del=(b-a)/dble(tnm)
x=a+0.5d0*del
sum=0.d0
do 11 j=1,it
*
sum=sum+Func(x)
x=x+del
*
11 continue
*
s=0.5d0*(s+(b-a)*sum/dble(tnm))
it=2*it
*
endif
*
return
end
是的,这个解释是有道理的。代码在
处访问变量 it
tnm=it
并且当 it
不是 save
时,此值未定义。
旧的编译可能根本没有使用堆栈,并且可能对所有变量都使用了静态存储。它也可能使用了堆栈,但它从未被覆盖并且值恰好在同一个地方。谁知道,我们没有要知道的信息。
对于像这样的错误代码,有一些编译器选项可以将 save
属性强制分配给所有变量(所有变量的 SAVE
从来都不是标准的!)。对于 Intel Fortran,它只是 -save
。
我们有这个旧的 Fortran 脚本,我们正在尝试使用 Intel 的 Visual Fortran 重新编译它,但是我们得到了计算错误和与代码的旧编译版本不同的结果。
我们在下面的代码中发现了我们认为是问题的地方(它大致来自 Numerical Recipes)。
问题是每次调用都会重置 'it' 参数,但是应该在函数调用之间存储它。
我们对可能出现问题的最佳猜测是,较旧的编译器可能将 'it' 视为 'save attribute',因此将其存储在函数调用之间。
我们在这里可能是完全错误的,如果一些 Fortran 大师可以证实这一点或有更好的解释,我们会很高兴得到一些帮助!
subroutine TrapezoidalRule(Func, a, b, s, n)
*
* This routine performs the trapezoidal rule, see Numerical Recipes
*
implicit none
real*8 Func, a, b, s
Integer*4 n
external Func
*
real*8 del, x, sum
Integer*4 it, tnm, j
*
if (n .eq. 1) then
*
s=0.5d0*(b-a)*(Func(a)+Func(b))
it=1
*
else
*
tnm=it
del=(b-a)/dble(tnm)
x=a+0.5d0*del
sum=0.d0
do 11 j=1,it
*
sum=sum+Func(x)
x=x+del
*
11 continue
*
s=0.5d0*(s+(b-a)*sum/dble(tnm))
it=2*it
*
endif
*
return
end
是的,这个解释是有道理的。代码在
处访问变量it
tnm=it
并且当 it
不是 save
时,此值未定义。
旧的编译可能根本没有使用堆栈,并且可能对所有变量都使用了静态存储。它也可能使用了堆栈,但它从未被覆盖并且值恰好在同一个地方。谁知道,我们没有要知道的信息。
对于像这样的错误代码,有一些编译器选项可以将 save
属性强制分配给所有变量(所有变量的 SAVE
从来都不是标准的!)。对于 Intel Fortran,它只是 -save
。