Fortran调试打印语句影响程序流程
我有一个程序,它迭代一个数组,确定性地生成新的行向量,然后将其附加到数组中。
在每次迭代时,都会采用向量的范数,以确保它不是零向量。如果为零,则程序停止。
存在一个错误,第三次迭代会导致向量变为零。
在寻找这个错误时,我用调试打印语句填充了代码,打印 *,“这里”,this_var 等。这些打印语句之一(打印最新向量的范数)修复了程序。
我不喜欢打印声明。我也不喜欢这样,我不明白发生了什么。
有谁知道为什么打印语句会影响它打印的内容?
可用代码(约 400 行,大量注释)
I have a program which iterates over an array, deterministically making new row-vectors which it then appends to the array.
At each iteration the norm of the vector is taken, to ensure it's not a zero vector. If it is zero, the program stops.
There was a bug whereby the third iteration would cause the vector to go to zero.
In looking for this bug I filled the code with debugging print statements, print *,"here",this_var etc. One of these print statements (which prints the norm of the latest vector) fixed the program.
I don't like the print statement. I also don't like that I don't understand what's going on.
Does anyone have any ideas about why a print statement would affect the thing it's printing?
Code (~400 lines, lots of comments) available
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如詹姆斯在评论中指出的那样,这可能是由数组索引引起的。您应该尝试在打开数组边界检查的情况下重新编译代码(执行此操作的命令将根据您的编译器而有所不同)并运行程序。
你是绝对正确的——像这样的阴险错误绝对令人发狂。
As James pointed out in the comments, this could be caused by array indexing. You should try recompiling the code with array bounds checking turned on (the command to do this will vary depending on your compiler) and run the program.
And you're absolutely correct - insidious errors like this are absolutely maddening.
看起来您由于越界访问而覆盖了内存。编译时启用数组边界检查。这使得程序运行速度变慢,但你可以找出内存违规。
looks like you overwrite your memory because of an access that is out of bounds. compile with array bounds checking on. that makes the program run slower, but you can find out the memory violation.
有一次,我发现一个与编译器或链接器有关的错误,其结果与此类似。有问题的代码行在多个程序中都是相同的,除了这一行之外,它们都有效。在这种情况下,所讨论的行是一个算术 if。当我将其转换为多个标准 if then else 语句时,它按预期工作。这不太可能是错误,但我必须把它放在那里。
还必须对蒂姆的回答加分。编译器选项至关重要。
At one time I found an error having to do with the compiler or linker that had results similar to this. The line of code in question was the same in multiple programs and they all worked except this one. In this case the line in question was an aritmetic if. When I converted it to multiple standard if then else statements it worked as it should. It is highly unlikely that this is the error, but I must put it out there.
Also must put plusses on Tim's answer. Compiler options are paramount.
你使用子程序吗?
有时,您覆盖子例程中的某些数据,并且它不会传播回来,但结果会丢失。
子程序调用中的类型正确吗?
你使用意图输入/输出吗?
你使用固定尺寸吗?
do you use subroutines?
sometimes you overwrite some data in a subroutine and it does not get propagated back, but the result is lost.
do you have the correct types in the subroutine call?
do you use intent in/out?
do you use fixed dimensions?