gpt4 book ai didi

fortran - 计算前在打印语句中使用变量时,结果会发生变化

转载 作者:行者123 更新时间:2023-12-01 13:49:20 25 4
gpt4 key购买 nike

我在 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

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

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

最佳答案

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

关于fortran - 计算前在打印语句中使用变量时,结果会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com