LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2012, 08:44 PM   #1
s_hy
LQ Newbie
 
Registered: Jan 2012
Distribution: openSuse
Posts: 11

Rep: Reputation: Disabled
segmentation fault problem??


dear all,

i have a code with line segmentation fault error. please anyone advice me how to debug this code. thank you...

##################################################################

Program Main

IMPLICIT NONE

call fd1d

return
END PROGRAM Main


! 1d scalar wave equation with fd method and stability condition = 1
SUBROUTINE fd1d
implicit none

real,dimension(1) :: x
real :: x_delta
real :: t_delta
real :: c
real :: xlast
real :: xinit
real :: tinit
real :: tlast
real :: k
real :: x0
real,dimension(:,,allocatable :: u
real :: alpha
integer :: iinit
integer :: ilast
integer :: ninit
integer :: nlast
integer :: i
integer :: n
real, parameter :: pi = 3.141592654

allocate (u(0:200,0:600))

!initialisation
c = 300
x_delta = 0.005
t_delta = x_delta/c
alpha = c*t_delta/x_delta
xlast = 1.0
xinit = 0
iinit = 0
ilast = ((xlast-xinit)/x_delta)
tlast = 0.01
tinit = 0
ninit = 0
nlast = ((tlast-tinit)/t_delta)
x(iinit) = xinit
x(ilast) = xlast

!initial profile
k = 1000.0
x0 = 0.3
do i = iinit,ilast
u(i,0) = 0.0
u(i,-1) = 0.0
end do

open (unit=100,file='n0.dat',action='write')
do i = iinit, ilast
x(i) = x(iinit) + i*x_delta
u(i,0) = exp (-k*(x(i)-x0)**2)
u(i,-1) = exp (-k*(x(i)-x0)**2)
Print *, u(i,0)
write (*,*) u(i,0)
end do
close (unit = 100)

open (unit=110,file='n1.dat',action='write')
do n = 0,nlast
do i = iinit,ilast
u(i,n+1) = 2*(1-(alpha**2))*u(i,n)-u(i,n-1)+(alpha**2)*(u(i+1,n)+u(i-1,n))
end do !i

!boundary condition
u(0,n+1) = 0
u(ilast,n+1) = 0
!end of boundary condition
!export data
print *, u(i,n+1)
write (110,*) u(i,n+1)
end do !n
close (unit = 110)

return
end SUBROUTINE fd1d
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-05-2012, 05:28 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
gdb?

does it do assembler.

use the 'where' command.
 
Old 03-05-2012, 05:51 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by bigearsbilly View Post
gdb?

does it do assembler.

use the 'where' command.
I believe this is Fortran, which you can debug with gdb.
Kevin Barry
 
Old 03-05-2012, 08:29 PM   #4
s_hy
LQ Newbie
 
Registered: Jan 2012
Distribution: openSuse
Posts: 11

Original Poster
Rep: Reputation: Disabled
yes, this is fortran90. can anyone teach me how to use gdb??
 
Old 03-05-2012, 08:35 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It will be a lot easier to learn from an intro online than for me to try to explain it. It's very simple if you're just trying to find a segfault, as long as you compile with the -g flag (I'm assuming you're running some sort of *nix with gcc for this; if not, this probably doesn't help.)
Kevin Barry
 
Old 03-05-2012, 08:39 PM   #6
s_hy
LQ Newbie
 
Registered: Jan 2012
Distribution: openSuse
Posts: 11

Original Poster
Rep: Reputation: Disabled
i use makefile to compile this(main.f90). the online version of gdb just show how to run it with .out, but my output are main.o and subroutines.o. Have no idea how to use gdb with this kind of file. pls advice.
 
Old 03-05-2012, 10:54 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

To use gdb, compile your program with the `-g' option, like this
Code:
$ gfortran -g wave.f90
or add the option to makefile. To learn GDB read any online tutorial, this for example. Here is a sample gdb session with your program:
Code:
$ gfortran -g wave.f90 
$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /tmp/a.out...done.
(gdb) run
Starting program: /tmp/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x0804897c in fd1d () at wave.f90:58
58            u(i,-1) = 0.0
(gdb) l     # list -- type the text of the program in the vicinity of where it is presently stopped.
53            !initial profile
54            k = 1000.0
55            x0 = 0.3
56            do i = iinit,ilast
57            u(i,0) = 0.0
58            u(i,-1) = 0.0
59            end do
60            
61            open (unit=100,file='n0.dat',action='write')
62            do i = iinit, ilast
(gdb) p u    # p = print
$1 = (( 0) )
(gdb) p i
$2 = 0
(gdb) p u(0,-1)
no such vector element
(gdb) quit
A debugging session is active.

        Inferior 1 [process 20884] will be killed.

Quit anyway? (y or n) y
We see that the bug reveal itself in the line where you are trying to assign 'u(0,-1)'. You should either rethink this line or change the definition of u:
Code:
allocate (u(0:200,-1:600))
Next, when we made this change, we similarly reveal next bug in the line
Code:
(gdb) run
Starting program: /tmp/a.out 
  8.19395264E-40
  8.19395264E-40
  1.60516147E-38
  1.60516147E-38

Program received signal SIGSEGV, Segmentation fault.
0x08048a46 in fd1d () at wave.f90:64
64            u(i,0) = exp (-k*(x(i)-x0)**2)
(gdb) p i
$1 = 2
(gdb)
This time we access element 2 of array x, which has only one element... So we change definition of x to
Code:
real,dimension(200) :: x
and voila, the program works.

Hope that helps.

Last edited by firstfire; 03-05-2012 at 10:56 PM.
 
2 members found this post helpful.
Old 03-05-2012, 10:59 PM   #8
s_hy
LQ Newbie
 
Registered: Jan 2012
Distribution: openSuse
Posts: 11

Original Poster
Rep: Reputation: Disabled
wow...it's work!! great...thank you so much
 
  


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 problem odedbobi Linux - Software 1 11-20-2008 12:03 PM
Segmentation Fault - memory problem? felipebil Programming 10 11-19-2008 10:01 PM
Regex.h problem in c++, segmentation fault vargadanis Programming 4 07-14-2008 05:36 PM
problem with segmentation fault lucs Slackware 2 04-28-2005 09:14 AM
Segmentation Fault Problem luvonmik Linux - Newbie 2 02-14-2004 07:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:33 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