views:

53

answers:

4

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

+3  A: 

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.

Tim Whitcomb
A: 
Dave
I'm hoping that since he tagged the question with "Fortran 90" that arithmetic if statements are nowhere to be found...
Tim Whitcomb
+1  A: 

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.

f.jamitzky
A: 

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?

f.jamitzky