innoSPG
innoSPG

Reputation: 4656

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

I have a strange behavior in a fortran subroutine that looks like:

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

If I uncomment the print statement, I get the expected result. If I keep it commented the result becomes very strange with huge numbers. By the way, the print statement got there just for debugging purpose. Strangely, it "solved" the problem, but that is not a reliable solution. The subroutine is part of a large code and I have not yet been able to take the problem out of the context of the large code. The debugger did not help much. It is obvious that the problem is somewhere else, because a print statement is not supposed to change the result of a computation.

My question is: what are the possible mistakes that can lead to such a problem? Had someone had similar problem?

Upvotes: 4

Views: 2103

Answers (1)

janneb
janneb

Reputation: 37248

This is a typical symptom of memory corruption. Try compiling with "-fcheck=all -Wall -g", and fix all warnings and errors. If that doesn't help, run via valgrind and/or address sanitizer.

Upvotes: 4

Related Questions