LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03:44 PM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

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, 08:32 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 11:38 AM   #3
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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, 11:45 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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, 11:51 AM   #5
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

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, 07:58 PM   #6
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

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


Reply



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 02:35 AM
Segmentation Fault (core dumped) doing su - Blinker_Fluid Solaris / OpenSolaris 5 10-07-2008 06:04 PM
Segmentation fault (core dumped) nasim751 Programming 2 01-28-2008 02:56 PM
Segmentation fault (core dumped) nasim751 Programming 1 01-27-2008 09:18 PM
Segmentation fault (core dumped) eytan *BSD 3 04-27-2005 08:38 PM

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

All times are GMT -5. The time now is 09:38 PM.

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
Open Source Consulting | Domain Registration