我正在尝试使用链表进行选择排序。但是,我收到分段错误。
"">
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7f191e86a32a
#1 0x7f191e869503
#2 0x7f191e49cf1f
#3 0x7f191e9e9330
#4 0x7f191e9ed0b4
#5 0x7f191e9e423c
#6 0x7f191e9e46dc
#7 0x55ed5c77be79
#8 0x55ed5c77bfa6
#9 0x7f191e47fb96
#10 0x55ed5c77b9c9
#11 0xffffffffffffffff
Segmentation fault (core dumped)
"">
我确信这是我尝试在我的"输出"do 循环之间写出数据时引起的。我发现看到我如何搞砸记忆是很困难的。
任何帮助都非常感谢!提前非常感谢!
program insertion_sort
implicit none
! Derived data type to store integer values in
type int_value
integer :: value
type (int_value), pointer :: next_value
end type
! Data dictionary: declare variable types & definitions
type (int_value), pointer :: head ! Pointer to head of list
character(20) :: filename ! input data file name
integer :: istat ! status: 0 for succes
integer :: nvals = 0 ! Number of data read
type (int_value), pointer :: ptr ! Ptr to new value
type (int_value), pointer :: ptr1 ! Temp ptr for search
type (int_value), pointer :: ptr2 ! Temp ptr for search
type (int_value), pointer :: tail ! Pointer to tail of list
integer :: temp ! Temporary variable
! Get the name of the file containing the input data
write (*,*) 'Enter the file name with the data to be sorted'
read (*,'(A20)') filename
! Open input data file
Open (Unit=9, file=filename, status='OLD', action='read', &
iostat=istat )
! Was the open succesful
fileopen: if ( istat == 0 ) then ! Open succesful
! The file was opened succesfully, so read the data value
! to sort, allocate a variable for it, and locate the proper
! point to insert the new value into the list
input: do
read (9, *, iostat=istat) temp ! Get value
if ( istat /=0 ) exit ! Exit on end of data
nvals = nvals + 1 ! Bump count
allocate (ptr, STAT=istat) ! Get value
ptr%value = temp ! Store number
! Now find out where to put it in the list.
new: if (.not. associated(head)) then ! No values in list
head => ptr ! Place at front
tail => head ! Tail pts to new value
nullify (ptr%next_value) ! nullify next ptr
else
! Values alreadd in list. Check for location.
front: if ( ptr%value < head%value ) then
! Add at front of list
ptr%next_value => head
head => ptr
else if ( ptr%value >= tail%value ) then
! Add at end of list
tail%next_value => ptr
tail => ptr
nullify (tail%next_value)
else
! Find place to add value
ptr1 => head
ptr2 => ptr1%next_value
search: do
if ( (ptr%value >= ptr1%value) .and. &
(ptr%value < ptr2%value) ) then
! Insert value here
ptr%next_value => ptr2
ptr1%next_value = ptr
exit search
end if
ptr1 => ptr2
ptr2 => ptr2%next_value
end do search
end if front
end if new
end do input
! WHERE I BELIEVE THE SEGMENTATION FAULT OCCURS
! Now write out the data.
ptr => head
output: do
if ( .not. associated(ptr) ) exit ! Pointer valid?
write (*, '(I10)') ptr%value ! Yes: Write value
ptr => ptr%next_value ! Get next pointer
deallocate(ptr, StAT=istat)
end do output
else fileopen
! Else file open failed. Tell user.
write (*, '(A,I6)') 'File open failed--status =', istat
end if fileopen
end program insertion_sort
只是为了跟进。我已经完成了代码,现在可以工作了。通过在开始时使头部指针无效来避免分段错误。以前的代码在其 if 语句中也存在问题,因为它之前没有考虑最后一个或第一个数字是最高或最低。
program insertion_sort
implicit none
! Derived data type to store integer values in
type int_value
real :: value
type (int_value), pointer :: p
end type
! Data dictionary: declare variable types & definitions
type (int_value), pointer :: head => null() ! Pointer to head of list
character(20) :: filename ! input data file name
integer :: istat ! status: 0 for succes
integer :: nvals = 0, i = 0 ! Number of data read
type (int_value), pointer :: ptr ! Ptr to new value
type (int_value), pointer :: ptr1 ! Temp ptr for search
type (int_value), pointer :: ptr2 ! Temp ptr for search
type (int_value), pointer :: tail ! Pointer to tail of list
real :: temp ! Temporary variable
character(80) :: msg ! I/O Message
! Get the name of the file containing the input data
write (*,*) 'Enter the file name with the data to be sorted'
read (*,'(A20)') filename
! Open input data file
Open (Unit=9, file=filename, status='OLD', action='read', &
iostat=istat, iomsg=msg )
! Was the open succesful
fileopen: if ( istat == 0 ) then ! Open succesful
! The file was opened succesfully, so read the data value
! to sort, allocate a variable for it, and locate the proper
! point to insert the new value into the list
input: do
read (9, *, iostat=istat) temp ! Get value
if ( istat /=0 ) exit ! Exit on end of data
nvals = nvals + 1 ! Bump count
allocate (ptr, STAT=istat) ! Get value
ptr%value = temp ! Store number
! Now find out where to put it in the list.
new: if (.not. associated(head)) then ! No values in list
head => ptr ! Place at front
tail => head ! Tail pts to new value
nullify (ptr%p) ! nullify next ptr
else
! Values alreadd in list. Check for location.
front: if ( ptr%value <= head%value ) then
! Add at front of list
ptr%p => head
head => ptr
else if ( ptr%value >= tail%value ) then
! Add at end of list
tail%p => ptr
tail => ptr
nullify (tail%p)
else
! Find place to add value
ptr1 => head
ptr2 => ptr1%p
search: do
if ( (ptr%value >= ptr1%value) .and. &
(ptr%value <= ptr2%value) ) then
! Insert value here
ptr%p => ptr2
ptr1%p = ptr
exit search
end if
ptr1 => ptr2
ptr2 => ptr2%p
end do search
end if front
end if new
end do input
!Now write out the data.
ptr => head
output: do
if ( .not. associated(ptr) ) exit ! Pointer valid?
write (*, '(F10.4)') ptr%value ! Yes: Write value
ptr => ptr%p ! Get next pointer
end do output
deallocate(ptr,STAT=istat)
else fileopen
! Else file open failed. Tell user.
write (*, '(A,I6)') 'File open failed--status =', istat
end if fileopen
end program insertion_sort