我正在学习现代Fortran,我在Windows 10 64bit上使用TDM-GCC编译器。 在正确编译我的代码后,我在尝试运行输出.exe
时收到此消息Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 ffffffffffffffff
#1 ffffffffffffffff
#2 ffffffffffffffff
#3 ffffffffffffffff
#4 ffffffffffffffff
#5 ffffffffffffffff
#6 ffffffffffffffff
#7 ffffffffffffffff
#8 ffffffffffffffff
#9 ffffffffffffffff
#10 ffffffffffffffff
#11 ffffffffffffffff
#12 ffffffffffffffff
#13 ffffffffffffffff
#14 ffffffffffffffff
#15 ffffffffffffffff
#16 ffffffffffffffff
#17 ffffffffffffffff
#18 ffffffffffffffff
我知道问题可能是由于读取I / O文件引起的。这是代码(它不是我的,它来自课程老师,他告诉我它是运行时错误,但不知道为什么我在读取文件时出错):
program golden_ratio
! experiments with the golden ratio iterative relation
implicit none
integer, parameter :: rk = kind(1.0d0)
real(rk) :: phi, phi_old
real(rk) :: phi_start, tol
integer :: i, max_iter
open(11,FILE='goldenfile.in',STATUS='old')
read(11,*) phi_start, tol, max_iter
close(11)
! how I wrote the input file
! 5.0
! 0.0001
! 1000
phi_old = phi_start
do i=1,max_iter
phi = 1.0d0/phi_old + 1.0d0
if (abs(phi - phi_old) < tol) exit
phi_old = phi
end do
open(12,FILE='goldenfile.out',STATUS='replace')
write(12,100) 'Start value:',phi_start
write(12,100) 'Tolerance:',tol
write(12,'(2(A," ",I11," "))') 'Ended at iteration:', i, 'of', max_iter
write(12,100) 'Final value:',phi
close(12)
print *, 'Output file created'
100 format(A," ",F13.10)
end program golden_ratio
因此,当我尝试打开输入文件&#39; goldenfile.in&#39; (它存在于同一目录中,定义为我在评论部分中写的,显然它不包含注释标记)。 当我放弃使用输入文件时,也会发生这种情况,我尝试覆盖输出文件&#39; goldenfile.out&#39;在第一次获得它之后。使用&#39;替换&#39;或不是不会改变这种情况。 奇怪的是编译顺利。