Why does accessing an index of 0 length array not throw an out of bounds access violation error?

80 Views Asked by At

Using intel fortran oneapi version 2023.0.0.25839. I've turned on all run-time checks using /check:all.

If the following throws a "forrtl: severe (408): fort: (2): subscript #1 of the array A has a value 2 which is greater than the upper bound of 1" error:

Program Main

    implicit none

    real, dimension(:), allocatable :: a
    allocate(a(1))

    print *, a(2)

End Program Main

then why doesn't this code also throw the error:

Program Main

    implicit none

    real, dimension(:), allocatable :: a
    allocate(a(0))

    print *, a(2)

End Program Main

It obviously gives undefined behavior, but it doesn't throw an error.

Edit: Full command line argument list as requested in comments

/nologo /debug:full /Od /Warn:interfaces /module:"Debug\\" /object:"Debug\\" /Fd"Debug\vc160.db" /traceback /check:all /libs:dll /threads /dbglibs /c
0

There are 0 best solutions below