什么是'总线错误?'
以下是我的代码。
非常感谢您的时间!
PROGRAM numbersgen
IMPLICIT NONE
!Variable declaration
INTEGER, DIMENSION(:,:),ALLOCATABLE::numarray
INTEGER, DIMENSION(:),ALLOCATABLE::temparray
INTEGER:: numrolls, numexps
INTEGER:: i=0, j=0
REAL:: avg=0, sdv=0, variance=0, sum=0
INTEGER:: k, min, pos, temp
PRINT*, "Enter the number of experiments to simulate: "
READ*, numexps
PRINT*, "Enter the number of rolls per experiment: "
READ*, numrolls
ALLOCATE(numarray(numexps,numrolls))
-----
ALLOCATE(temparray(numrolls))
-----
END PROGRAM
答案 0 :(得分:4)
看起来temparray
在分配之前就已使用了。
回答你的问题"what is a bus error?":这通常意味着你的程序试图访问一个不存在的地址,或一个没有正确对齐的地址 对于该架构(例如,尝试从未在4个字节的倍数上对齐的地址读取32位值)。
答案 1 :(得分:4)
您temparray
的分配为时已晚。试试这个:
ALLOCATE(numarray(numexps,numrolls), temparray(numrolls))
并删除第二个ALLOCATE
。
下次要自己调试,使用-g
选项进行编译,如下所示:
$ gfortran -g code.f95
并在调试器下运行:
$ gdb a.out
gdb> run
然后它会告诉你崩溃的位置。
答案 2 :(得分:4)
您在此处获得的总线错误是在分配temparray之前通过从numarray复制到temparray而发出的。只需移动线 ALLOCATE(temparray(numrolls)) 在你进入循环之前。
有关总线错误的好评,请参阅Segmentation fault。