Type or Class for a Pointer in a Linked List in Fortran -
i confused usage of type
, class
in defining linked list type bound procedure in fortran. following simple program causes segmentation fault in get_num_nodes
function. however, if change declaration of pointer next
class
type
, program ends normally.
the segmentation fault occurs when build ifort
(version 14.0.2). if build gfortran (version 4.8.4 , 7.2.0), both cases of class
, type
end normally.
what wrong using class
in declaring next
pointer? how can understand problem? or, wrong in different part of code?
tll.f90
module tll implicit none private public llnode, add_node_at_head type llnode private integer :: idx !type(llnode), pointer :: next => null() ! .. using declaration type works. class(llnode), pointer :: next => null() ! .. declaration class causes segmentation fault ! ifort. contains procedure :: get_num_nodes end type llnode ! ================================================================ contains ! ================================================================ ! **************************************************************** subroutine add_node_at_head(p_head, idx_arg) ! **************************************************************** type(llnode), pointer, intent(inout) :: p_head integer, intent(in) :: idx_arg ! type(llnode), pointer :: p_new_head write(*,*) 'entered add_node_at_head..' allocate(p_new_head) p_new_head%idx = idx_arg p_new_head%next => p_head p_head => p_new_head write(*,*) '.. leaving add_node_at_head' end subroutine add_node_at_head ! **************************************************************** function get_num_nodes(self) result(num_nodes) ! **************************************************************** integer :: num_nodes class(llnode), target, intent(in) :: self ! class(llnode), pointer :: p_node write(*,*) 'entered get_num_nodes ..' num_nodes = 0 p_node => self while (associated(p_node)) num_nodes = num_nodes+1 p_node => p_node%next end write(*,*) '.. leaving get_num_nodes' end function get_num_nodes end module tll
main.f90
program main use tll, : llnode, add_node_at_head implicit none type(llnode), pointer :: p_head=>null() integer :: num_nodes call add_node_at_head(p_head, 10) num_nodes= p_head%get_num_nodes() write(*,*) 'num_nodes=', num_nodes write(*,*) 'normal end.' end program main
compilation , execution:
$ ifort -c tll.f90 $ ifort -c main.f90 $ ifort -o exe main.o tll.o $ ./exe entered add_node_at_head.. .. leaving add_node_at_head entered get_num_nodes .. forrtl: severe (174): sigsegv, segmentation fault occurred image pc routine line source exe 000000000046df09 unknown unknown unknown exe 000000000046c7de unknown unknown unknown exe 000000000043e3b2 unknown unknown unknown exe 0000000000422fc3 unknown unknown unknown exe 0000000000402a3b unknown unknown unknown libpthread.so.0 00002b8fdbb48330 unknown unknown unknown exe 00000000004026d2 unknown unknown unknown exe 0000000000402402 unknown unknown unknown exe 0000000000402336 unknown unknown unknown libc.so.6 00002b8fdbd77f45 unknown unknown unknown exe 0000000000402229 unknown unknown unknown $
Comments
Post a Comment