Hi, on Windows 10 my fortran90 program get a Segmentation fault error on simple assignments like this “X(1,letter,combin) = Y(letter, combin)” inside a do cycle. This happens at the second execution of this cycle.
Running my .exe more times, I verifyed that the error arise in different moments, I mean for different values of letter and combin, sometimes earlyer sometimes later. Moreover, at this point sometimes the program gives the Segmentation fault error, sometimes simply ends (doesn’t execute the further commands) without error. I can’t understand why, and I don’t know how to fix this problem.
Quite often it happens when writing outside of the array, because of out of bounds index(es). You should enable runtime checks with some compiler switches (e.g. -check
with Intel Fortran) and warnings (-warn all
). If it doesn’t help, try posting here a minimal reproducible example.
I still do not understand why the compiler can not generate the exact reason for the error! Why do we need to guess it always?
Which compiler are you using? “segmentation fault” is not a term Windows uses, where it is called “access violation”. However, the concepts are the same - a reference to a memory address that does not exist or is protected from access.
The symptoms you describe suggest that you have stack corruption, which can result in unpredictable behavior. The actual cause of the error can be many instructions previous, so an error message at the point of failure isn’t always helpful.
What I do when encountering a situation such as this is first compile everything without optimization, and ideally with debugging enabled. If the error disappears, then selectively enable optimization on source files until you find the one (hopefully) that triggers the error. You can then start to narrow down the problem.
I’d also suggest a trial build with all run-time checks (array bounds, etc.) enabled and see if it turns up anything.
@Shahid It’s not that they can not: they do not, by design. Runtime checks (such as bound checking) can hurt the performances and are not enabled by default. You can generally enable them during development/debugging phases with appropriate compiler switches.
Thank you for your help.
I’m using the compiler GNU Fortran (GCC) 13.1.0
The exact error on my screen is “Program received signal SIGSEGV: Segmentation fault - invalid memory reference.”
I’ll try again using useful compiler options.
OK - since you’re using this under WSL, it behaves like Linux. I think gfortran offers a traceback option, which would at least tell you where in the program the segfault arises. Running under gdb may also provide a clue. As I wrote earlier, the actual cause may be quite a while earlier in the program.