分段错误-fortran中无效的内存引用问题

问题描述 投票:0回答:1

我正在尝试使用fortran从数据计算值。

program signifikansi
implicit none
integer :: N_energi, N_sudut, i, N
double precision,allocatable :: theta1(:), theta_lab1(:), dsig1(:), dsig_lab1(:), pol1(:), dpolxx1(:), dpolxz1(:), phase1(:), t1(:)
double precision,allocatable :: theta2(:), theta_lab2(:), dsig2(:), dsig_lab2(:), pol2(:), dpolxx2(:), dpolxz2(:), phase2(:), t2(:)   
double precision,allocatable :: signif(:) 
double precision :: signif_total,signif2

    write(*,*)"Masukkan jumlah energi"
    read(*,*)N_energi

    write(*,*)"Masukkan jumlah sudut pada setiap energi"
    read(*,*)N_sudut

N=N_sudut*N_energi

    write(*,*)"Total data adalah",N

open(unit=10, file='kminpobs.dat', status="old", action="read")
allocate(theta1(N))
allocate(theta_lab1(N))
allocate(dsig1(N))
allocate(dsig_lab1(N))
allocate(pol1(N))
allocate(dpolxx1(N))
allocate(dpolxz1(N))
allocate(phase1(N))
allocate(t1(N))

do i=1,N
read(10,*) theta1(i), theta_lab1(i), dsig1(i), dsig_lab1(i), pol1(i), dpolxx1(i), dpolxz1(i), phase1(i), t1(i)
end do
close(10)

open(unit=10, file='kminpnoscalar.dat', status="old", action="read")
allocate(theta2(N))
allocate(theta_lab2(N))
allocate(dsig2(N))
allocate(dsig_lab2(N))
allocate(pol2(N))
allocate(dpolxx2(N))
allocate(dpolxz2(N))
allocate(phase2(N))
allocate(t2(N))

do i=1,N
read(10,*) theta2(i), theta_lab2(i), dsig2(i), dsig_lab2(i), pol2(i), dpolxx2(i), dpolxz2(i), phase2(i), t2(i)
end do
close(10)

do i=1,N
signif(i) = (ABS(dsig2(i)-dsig1(i)))/dsig1(i)
end do

signif_total = SUM(signif)

signif2 = signif_total/N

write(*,*)"Hasil signifikansi untuk partikel tersebut adalah",signif2

deallocate(theta1)
deallocate(theta_lab1)
deallocate(dsig1)
deallocate(dsig_lab1)
deallocate(pol1)
deallocate(dpolxx1)
deallocate(dpolxz1)
deallocate(phase1)
deallocate(t1)
deallocate(theta2)
deallocate(theta_lab2)
deallocate(dsig2)
deallocate(dsig_lab2)
deallocate(pol2)
deallocate(dpolxx2)
deallocate(dpolxz2)
deallocate(phase2)
deallocate(t2)

end program signifikansi

这是我的代码,但我得到的只是这样的错误消息

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7F0C2BFC2777
#1  0x7F0C2BFC2D7E
#2  0x7F0C2BC1AD3F
#3  0x403034 in MAIN__ at signifikansi1.f90:?
Segmentation fault

输入的N_ergy为39,N_sudut为120。我在输出中得到了N值,但是我的代码无法计算出进一步的结果。

有人可以帮我吗?谢谢。我的猜测是我使用的数组大小太大。

segmentation-fault fortran gfortran
1个回答
0
投票
我已经运行了Project-Build All。编译代码的方式是使用标志-WAll显示所有警告,这始终是一个好主意。您的Fortran编译器可能有一组不同的标志可供选择。

13:26:49 **** Build of configuration Debug for project TestFortran2 **** make all Building file: ../TestFortran2.f90 Invoking: GNU Fortran Compiler gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -o "TestFortran2.o" "../TestFortran2.f90" ../TestFortran2.f90:52:0: signif(i) = (ABS(dsig2(i)-dsig1(i)))/dsig1(i) Warning: 'signif.offset' may be used uninitialized in this function [-Wmaybe-uninitialized] ../TestFortran2.f90:55:0: signif_total = SUM(signif) Warning: 'signif.offset' may be used uninitialized in this function [-Wmaybe-uninitialized] ../TestFortran2.f90:55:0: Warning: 'signif.dim[0].lbound' may be used uninitialized in this function [-Wmaybe-uninitialized] ../TestFortran2.f90:55:0: Warning: 'signif.dim[0].ubound' may be used uninitialized in this function [-Wmaybe-uninitialized] Finished building: ../TestFortran2.f90 Building target: TestFortran2 Invoking: GNU Fortran Linker gfortran -o "TestFortran2" ./TestFortran2.o Finished building target: TestFortran2 13:26:53 Build Finished. 0 errors, 4 warnings. (took 4s.364ms)

这些是在代码编辑器中显示警告的行,在第51至55行:


do i=1,N 
    signif(i) = (ABS(dsig2(i)-dsig1(i)))/dsig1(i) 
end do

signif_total = SUM(signif)

正如伊恩·布什(Ian Bush)在上面说过的,看来您还没有分配

signif数组。它似乎必须为

N大小。因此,我将其添加到代码中,在其他分配的末尾分配,并将其也添加到取消分配的列表中。

然后我清理了构建,并重新构建了所有构建。结果是没有错误或警告。如前所述,如果没有输入文件,我们将无法做更多的事情,但是在您的示例中,由于N的大小只有39 x 120 = 5,000个元素,因此分配问题不大可能出现。

您始终可以使用Stat关键字检查分配是否成功:

Allocate(Array(Size),Stat=SuccessCode)

其他几件事。拥有一个良好的IDE是值得的,因为这将使编码更加容易。另外,在编写代码时,缩进代码并不是必需的,但是这样做确实使以后阅读起来容易得多。

© www.soinside.com 2019 - 2024. All rights reserved.