1

I do not understand why this code does not produce any segmentation fault. I was expecting some errors while compiling the code or in run-time, but it runs apparently correctly.

program alloc

implicit none

real, dimension(:,:), allocatable :: myVec

allocate(myVec(3,2))    

myVec=1.

myVec(100,3)=10.

write(*,*) myVec(1,1)

return
end

Could somebody shed some light on this behaviour?

alie
  • 63
  • 1
  • 9

1 Answers1

1

With gfortran, you can use the -fbounds-check compiler flag (or -C with ifort) in order to look for this kind of errors. When you compile your example with this option, the program produces:

Fortran runtime error: Index '3' of dimension 2 of array 'myvec' above upper bound of 2

As to why the program does not crash immediately, this question contains more details.

ewcz
  • 12,819
  • 1
  • 25
  • 47