# Runtime segmentation fault error (randomic?)

**URL:** <https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368>\
**Category:** Homework\
**Created:** [August 18, 2023, 10:32am UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368 "2023-08-18T10:32:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![theobroma](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@theobroma](https://fortran-lang.discourse.group/u/theobroma)\
**Post date:** [August 18, 2023, 10:32am UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/1 "2023-08-18T10:32:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 18, 2023, 11:06am UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/2 "2023-08-18T11:06:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [August 18, 2023, 1:48pm UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/3 "2023-08-18T13:48:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 18, 2023, 2:39pm UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/4 "2023-08-18T14:39:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [August 18, 2023, 3:19pm UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/5 "2023-08-18T15:19:21Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![theobroma](https://avatars.discourse-cdn.com/v4/letter/t/b5a626/32.png) [@theobroma](https://fortran-lang.discourse.group/u/theobroma)\
**Post date:** [August 19, 2023, 9:06am UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/6 "2023-08-19T09:06:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [August 19, 2023, 2:13pm UTC](https://fortran-lang.discourse.group/t/runtime-segmentation-fault-error-randomic/6368/7 "2023-08-19T14:13:12Z")

</div>

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.
