我有一个应该给出细分错误的Fortran程序,但它没有 [英] I have a Fortran program that should give segmentation fault but it doesn't
本文介绍了我有一个应该给出细分错误的Fortran程序,但它没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与标题一样简单.我有一个学生出现细分错误,我试图向他证明为什么会发生这种情况.相反,我最终想知道为什么不这样做.
As simple as the title. I have a student who got a segmentation fault and I was trying to prove him why does this happen. Instead, I ended up wondering why it doesn't.
代码是这样的:
program main
implicit none
real*8, allocatable:: u(:)
integer :: i
allocate(u(2))
do i=0, 1000
u(i) = i
print *, u(i)
enddo
end program main
我希望它在 i = 3
时崩溃,但事实并非如此.与ifort和gfortran一起使用-O0到-O3编译
I would expect this to crash at i=3
, but it doesn't. Compiled with both ifort and gfortran with -O0 to -O3
推荐答案
打开边界检查选项进行编译,然后向学生解释,如果额外的开销会导致段错误是越界访问的常见结果,那该怎么办?边界检查不到位?
What about turning on the bounds checking option for compilation and then explaining to the student that a seg fault is a common result for out of bounds access when the extra overhead for bounds checking is not in place?
这篇关于我有一个应该给出细分错误的Fortran程序,但它没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文