Fortran debugging print statement affects program flow
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 th开发者_如何学编程e 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
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.
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?
精彩评论