7

I've having trouble with some old code used for research that I would like to compile using the Intel Fortran compiler. In a particular subroutine, I get segmentation faults unless I add in a write statement that just outputs the value of the loop index.

do j=1,ne

SOME STUFF

write(*,*) 'j=', j

end

What could be causing my error such that this write statement would fix my segmentation fault? (Note: j is declared as an integer)

thanks, keely

2
  • 2
    Can you strip it down to a minimal, self-contained example that still crashes? Otherwise it's going to be tough to diagnose; there is probably undefined behavior somewhere in "SOME STUFF", such that the segfault is staved off by some side effect of the write. Basically your classic "heisenbug".... Commented Aug 26, 2009 at 0:00
  • I'm having the exact same issue. I can see how it could be due different optimizations being used in the compilation. That makes me think that there is still an issue with the code, even if it's no longer crashing. Commented Oct 15, 2019 at 17:40

1 Answer 1

7

Classic ways of causing this type of error which is 'fixed' by inserting write statements:

  1. walking off the end of an array -- use your compiler to switch on bounds-checking and debugging options to check for this;

  2. disagreement between arguments provided to a sub-program and arguments expected. Again, use your compiler if possible, your eyes otherwise.

Odds are 5-to-1 that one of these is the cause.

Sign up to request clarification or add additional context in comments.

3 Comments

Why on earth would inserting a write statement fix these runtime errors?
It doesn't, it 'fixes' them. The inverted commas are important, used here to indicate that the 'solution' is illusory. The true cause of the error is likely to have been one of the two matters covered.
I sensed that, however the question remains what's going on under the hood that a write statemant can change the runtime behaviour at all in such a case (apart from writing to the unit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.