Segmentation fault (core dumped) in a GFortran subroutine
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Segmentation fault (core dumped) in a GFortran subroutine
Hi there. I am trying to adjust to my particular requirements a Fast Fourier Transform package I found on the web. It works but I need to do some modifications. So, at one moment I place a few lines of code into a subroutine. I need to dump an array into a file to examine it subsequently with gnuplot. It is a very simple array with 2310 members. This is the code:
Code:
10 FORMAT (E15.5)
open(unit=2,file="fft_1.dat",status="old")
IOstatus = 0
counter = 0
do while (IOstatus.eq.0)
counter = counter + 1
if (MOD(counter,2).ne.0) then
write (2,10) r (counter)
endif
enddo
close (unit=2)
What happens is the program breaks down. There are about 2600 lines of code and it is not immediately apparent at what point. If I comment the above lines out, everything works fine.
There are two ALLOCATE and two DEALLOCATE statements in that routine. I am sure if I rewrite the subroutine without them everything will work with and without this code block.
You've written an infinite loop. It shouldn't seg fault, but it should never end either. Clearly there's more at play than the small code chunk you've posted here.
99% of seg faults in fortran can be found by adding the -fbounds-check flag to your gfortran compilation.
Probably the infinite loop causes the counter variable to keep incrementing until such time it is larger than the length of the array "r", at which point the program will begin reading garbage until it tries to access an invalid memory address, which will cause a segfault generally.
Probably the infinite loop causes the counter variable to keep incrementing until such time it is larger than the length of the array "r", at which point the program will begin reading garbage until it tries to access an invalid memory address, which will cause a segfault generally.
For some reason I read that write as "r, counter", rather than "r(counter)", probably due to the oddly-placed space.
Yes, the infinite loop will eventually exceed the limits of r and dive into the ether, ultimately causing a seg fault.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.