Question:
FORTRAN Debugging Question?
2010-07-22 14:36:44 UTC
I am having a very strange issue... I have a rather long fortran code with several subroutines, and I have this exact same code in multiple directories on the same sever (I'm 100% it is exactly the same code -- I even copied it over to double check). In one directory, the code is running exactly as expected; however, in another directory I am getting a segmentation fault. I have tracked down to at least two arrays. The indices of both these arrays are being changed to random out of bound integers. The problem is I traced down the place where both of their values change, and it is within a subroutine that neither of these indices were passed into in the first place. I do use implicit declaration and set all variables to zero when compiling (with the gfortran compiler). This definitely seems to be a memory issue, but I can't possibly imagine why it is different in the two directory structures -- any suggestions???
Three answers:
Vincent G
2010-07-23 10:30:15 UTC
This looks like a COMMON block mismapping, although memory collision are not only limited to COMMON blocks.

Given that your code runs in different directories, is it possible that the directory path and/or input file names are obtained and saved in internal variable? Is it possible that the variable used to store that information is defined as being too small for the data that is put in there (especially true where assignment is handled by a subroutine which does not have the defined dimension of the passed parameter as a passed parameter itself)? Then storing something that is larger than the defined memory attributed to that parameter will 'invade' the next memory word, which could be your array index.



Did you try to run the program (in both locations) using the exact same input data?



(By the way, implicit declarations is not a very good idea. Zero and one digits can easily be mistaken for the letter o, lower case L and uppercase i, I have seen happen several times over the years. An "implicit none" with all variable explicitly defined will reliably catch those errors at compilation.)



Final word of caution: (as one of the IT rep used to repeat so often a quarter century ago) "an optimizer shifts code around". That means that the memory assigned to the variable of subroutine can be adjacent to those of a totally different subroutine (even more so for the variables passed as parameters in a CALL), as the optimizer would move the blocks around to achieve best performance. The only way to assure that memory slots are next to one another is to define them in a COMMON block, where all the variables have to be mapped next to one another (but different instances of a COMMON block may not define variables of the same size, leading either to extraordinary computational trickery for those who know exactly what they are doing; or to complete and untrackable disaster for those who don't).
Scott H
2010-07-24 13:21:29 UTC
It sounds like memory corruption problem due to an array or string variable writing to an area outside of its defined bounds. When compiling with gfortran, try adding the -fbounds-check flag to check for these errors.
?
2016-12-08 21:17:06 UTC
I consider the 1st poster. you in many circumstances could set compiler recommendations so as that it produces the assistance mandatory to grant considered one of those hint. tell the compiler which you quite prefer a hyperlink map & an entire compiler itemizing. additionally, with the Fortran that I used to apply i ought to set a compiler change so as that I had to declare and sort all variable till now use. This eliminates between the main faults of Fortran.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...