在计算之前在 print 语句中使用变量时结果会发生变化

Result changes when a variable is used in print statement before the computation

我在 fortran 子例程中有一个奇怪的行为,看起来像:

subroutine compute(a, b, c)
    real(8), dimension(:,:), intent(in) :: a
    real(8), dimension(:), intent(in)   :: b
    real(8), dimension(:), intent(in out) :: c

    !print*, c
    ! do some computation here to update c
end subroutine compute

如果我取消注释 print 语句,我会得到预期的结果。如果我保留它的评论,结果会变得非常奇怪,因为数量很大。顺便说一句,打印语句只是为了调试目的。奇怪的是,它 "solved" 的问题,但这不是一个可靠的解决方案。该子例程是大代码的一部分,我还不能从大代码的上下文中解决问题。调试器没有多大帮助。很明显问题出在其他地方,因为打印语句不应该改变计算结果。

我的问题是:有哪些可能的错误会导致这样的问题?有人遇到过类似的问题吗?

这是内存损坏的典型症状。尝试使用“-fcheck=all -Wall -g”进行编译,并修复所有警告和错误。如果这没有帮助,运行 通过 valgrind and/or 地址消毒剂。