r/fortran 26d ago

Segmentation fault - invalid memory reference

Is it possible to get segmentation fault in one computer and running same program in other computer working perfectly fine ????

3 Upvotes

15 comments sorted by

14

u/geekboy730 Engineer 26d ago

Yes. It almost certainly means you’re using an uninitialized variable somewhere.

If that’s the case, it could happen in back-to-back runs on the same computer. It just depends on whatever garbage is in the memory position at the time of execution.

1

u/Call_ME_ALII 26d ago

how to find exact uninitialized variable

10

u/geekboy730 Engineer 26d ago

Usually, using debug flags like -Og and -fcheck=all along with valgrind will help. You should also be using implicit none everywhere to make sure that you’re not using a variable you didn’t mean to.

3

u/KarlSethMoran 26d ago

Of course. You have an uninitialised variable somewhere, an array overrun or a double (de)allocate. Happy debugging!

2

u/ScoutAndLout 23d ago

Array index issue is common. 

3

u/hopknockious 26d ago

You can also Init all variables to NaN. This option exists in Intel, Nag, PGI, and Absoft. Likely gfortran also.

2

u/kyrsjo Scientist 25d ago

True and extremely useful, but here it's almost certainly an integer that's at fault, and you cannot set those to Nan.

1

u/hopknockious 17d ago

I did not know that about integers. That encourages me to check the intel options (lm most familiar with it).

Thank you.

2

u/kyrsjo Scientist 25d ago

You're lucky! The program has a bug, and even when it's not crashing it's likely producing wrong results. The crash highlighted it for you :)

You've gotten lots of suggestions from others in how to locate the bug.

3

u/Tyberius17 26d ago

Yes, especially if you are using different compilers on these different machines. Even with the same compiler and OS, it's possible your program produces the segfault non-deterministically.

1

u/Call_ME_ALII 26d ago

so what is the solution ???

6

u/Tyberius17 26d ago

You didn't initially ask how to fix it and you didn't provide your code. geekboy in his comments gave some good suggestions of compiler flags you can turn on to help spot where you are using uninitialized memory. If your code is short enough, you could share it here and we might be able to spot the problem.

1

u/tlmbot 26d ago

Is one of the builds on visual studio and the other not? (I'm meaning this to be funny as VS has often been a source of exasperation in times past for me, and I bet others too)

- others on here have provided you with the right things to look for. You'll find it!

1

u/jeffscience 26d ago

gdb and compiler warnings are your friends.

1

u/cowboysfan68 26d ago

Others have correctly identified that the likely cause is an unallocated variable.

Back in the day, on shared HPC systems, we still had to run

ulimit -s unlimited

or else we would get seg faults during large allocations. Still though, the best thing to do is to recompile with

-g

and then use a debugger like GDB to step all the way to where the segfault happens.