问题
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