LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-20-2014, 04:44 PM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 444

Rep: Reputation: Disabled
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.

Why do I have this problem? Thanks, A.
 
Old 12-22-2014, 09:32 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,557

Rep: Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116
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.
 
1 members found this post helpful.
Old 12-22-2014, 12:38 PM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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.
 
1 members found this post helpful.
Old 12-22-2014, 12:45 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,557

Rep: Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116
Quote:
Originally Posted by btmiller View Post
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.
 
1 members found this post helpful.
Old 12-22-2014, 12:51 PM   #5
AlexBB
Member
 
Registered: Mar 2014
Posts: 444

Original Poster
Rep: Reputation: Disabled
I made the space between the r and "(" myself, manually. You are correct: I should have put IOstatus into the read statement. O' my!!! Thank you.
 
Old 12-22-2014, 08:58 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Distribution: Debian
Posts: 1,186

Rep: Reputation: 243Reputation: 243Reputation: 243
It might be easier to just print as separate lines and re-direct the printer to file output.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault (Core dumped) ramshank Linux - Newbie 4 04-24-2014 03:35 AM
Segmentation Fault (core dumped) doing su - Blinker_Fluid Solaris / OpenSolaris 5 10-07-2008 07:04 PM
Segmentation fault (core dumped) nasim751 Programming 2 01-28-2008 03:56 PM
Segmentation fault (core dumped) nasim751 Programming 1 01-27-2008 10:18 PM
Segmentation fault (core dumped) eytan *BSD 3 04-27-2005 09:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration