I have a Fortran program that should give segmentation fault but it doesn't

二次信任 提交于 2021-01-28 21:04:39

问题


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.

The code is this:

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 would expect this to crash at i=3, but it doesn't. Compiled with both ifort and gfortran with -O0 to -O3


回答1:


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?



来源:https://stackoverflow.com/questions/58186674/i-have-a-fortran-program-that-should-give-segmentation-fault-but-it-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!