比较整数时出现非常奇怪的 FORTRAN 错误
我试图检查某些行在几个时间步内执行了多少次,这是我的代码的一部分: 1)每次写入“countd”:
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
!if (countd < 5) then
print*, "countd= ", countd
!endif
.
.
.
end function dergfm
结果:
countd= 1
.
.
.
countd= 21504
2)前4次写入“countd”:
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
if (countd < 5) then
print*, "countd= ", countd
endif
.
.
.
end function dergfm
结果:
countd= 1
countd= 2
countd= 3
countd= 4
3)写入大于5个
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
if (countd > 5) then
print*, "countd= ", countd
endif
.
.
.
end function dergfm
结果的“countd”:
[none]
看起来比较这些整数适用于.lt。但不适用于.gt。 (也不是.eq.)
I'm trying to check how many times certain lines are executed in few timesteps, here is part of my code:
1)write "countd" every time:
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
!if (countd < 5) then
print*, "countd= ", countd
!endif
.
.
.
end function dergfm
result:
countd= 1
.
.
.
countd= 21504
2)write "countd" first 4 times:
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
if (countd < 5) then
print*, "countd= ", countd
endif
.
.
.
end function dergfm
results:
countd= 1
countd= 2
countd= 3
countd= 4
3)write "countd" larger than 5
real(kind=8) function dergfm(jp,ip,lp)
integer :: jp,ip,lp,countd
real(kind=8) press
.
.
.
countd=countd+1
if (countd > 5) then
print*, "countd= ", countd
endif
.
.
.
end function dergfm
results:
[none]
It looks like comparing these integers works for .lt. but doesn't for .gt. (nor .eq.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
countd 是函数的局部变量。因此,它的值不一定在调用之间保留,除非您将“save”属性添加到声明中。尝试一下,看看代码是否开始运行。那么如何初始化 countd 呢?我将使用以下内容:
integer, save :: countd = 0
在这种情况下,“save”是可选的,因为它是由声明中的初始化暗示的。
countd is a local variable to a function. As such, its value it not necessarily retained between calls, unless you add the "save" attribute to the declaration. Try this and see if the code starts behaving. And how do you initialize countd? I would use the following:
integer, save :: countd = 0
In this case the "save" is optional because it is implied by the initialization in the declaration.
如果变量未保存,则行为未定义,并且可能会发生奇怪的事情。
对于某些编译器,COUNTD 的值将被初始化为零,并且代码
将按预期工作。对于其他编译器,每次都会得到完整的垃圾
你调用这个子程序。
其他时候(我怀疑是你的情况),变量被放在堆栈上并且可以
每次调用子例程时都会分配相同的内存。或者,它可能会得到
前 4 或 5 次分配相同的内存,然后其他一些子例程崩溃
第 6 次堆栈和 COUNTD 的值变成垃圾。
正确的答案是保存变量并用数据语句初始化它。
这是可移植的并且适用于每个系统。
If the variable was not SAVE'd, the behavior is undefined and strange things can happen.
For some compilers, the value of COUNTD will be initialized to zero and the code
will work as expected. For other compilers, you will get complete garbage every time
you call this subroutine.
Other times (which I suspect is your case), the variable is put on the stack and MAY
get assigned the same memory each time the subroutine is called. Or, it may get
assigned the same memory the first 4 or 5 times, then some other subroutine corrupts
the stack on the 6th time and the value of COUNTD becomes garbage.
The correct answer is to SAVE the variable and initialize it with a data statement.
This is portable and will work on every system.