问题
I am creating graph using the adjacency list method. Each node is represented as a node which points to other nodes connected to it. The following is my code
program main
use graphs
implicit none
type(node),pointer :: start
type(node), dimension(:), pointer :: grp
integer :: n, ios=0, a,b, i
open(1, file='test6.txt', status='OLD', action='READ')
read(1,*,iostat=ios) n
allocate(start)
allocate(grp(n))
do, i=1,n
grp(i)%val=i
enddo
do while(ios==0)
read(1,*, iostat=ios)a,b
if(ios.NE.0)then
exit
endif
start => grp(a)
call add(start, b)
start => grp(b)
call add(start, a)
end do
end program main
The module graph is as follows
module graphs
type node
integer :: val
type(node), pointer :: next
end type node
contains
subroutine add(strt, nxn)
implicit none
type(node), pointer :: strt, new_node, lst
integer :: nxn
allocate(new_node)
allocate(lst)
lst => strt
new_node%val = nxn
new_node%next => NULL()
do while(associated(lst%next))
lst => lst%next
enddo
lst%next => new_node
end subroutine add
end module graphs
File test6.txt
is as follows
43
1 2
1 10
2 3
2 11
3 4
4 5
5 6
6 7
7 8
8 9
3 12
4 13
5 14
6 15
7 16
8 17
9 18
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
10 19
11 19
12 20
I am getting the following error
Program received signal SIGSEGV: Segmentation fault - invalid memory
reference
Backtrace for this error:
#0 0x7f786df6bef7 in ???
#1 0x7f786df6b12d in ???
#2 0x7f786dbbc4af in ???
#3 0x401db0 in __graphs_MOD_add
at /home/nav/MS project/new/grph.f90:18
#4 0x400f48 in ???
#5 0x400f85 in ???
#6 0x7f786dba782f in ???
#7 0x400a18 in ???
#8 0xffffffffffffffff in ???
Segmentation fault (core dumped)
The above program is running smooth for small graphs but not working for large graphs. I am not able to get what am I doing wrong? I am using the gfortran compiler.
回答1:
Nowhere in your code you set the starting %next
pointer to null. So when you come to
do while(associated(lst%next))
the address where lst%next
points to is undefined. You are not allowed to ask whether it is associated, because the result associated()
will return is also undefined. See also this classical resource for some more explanation http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html#5
The best cure is to the default initialization of pointer components
type node
integer :: val
type(node), pointer :: next => null()
end type node
Make it a habit to always set pointer components to null.
来源:https://stackoverflow.com/questions/44399446/segmentation-fault-in-fortran-while-constructing-graph-using-adjacency-lists