本文介绍了我有一个应该给出分段错误的 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

推荐答案

如何打开编译的边界检查选项,然后向学生解释当额外开销时越界访问的常见结果是段错误for 边界检查没有到位?

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 程序,但它没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:02
查看更多