I ran across something in someone else's code that seems like it should be undefined behavior in the fortran spec. There is an array defined with an explicit upper and lower bound
ALLOCATE(arraysmall(10,5:10))
This array is passed into a subroutine with the an assumed size dummy argument an explicit index range to work over.
SUBROUTINE foo(arraylarge, indexmin, indexmax)
INTEGER, DIMENSION(10,1:20) :: arraylarge
INTEGER, INTENT(in) :: indexmin
INTEGER, INTENT(in) :: indexmax
...
! Do work from indexmin to indexmax
arraylarge(:,indexmin:indexmax) = ...
END SUBROUTINE
When this subroutine then gets called.
CALL foo(smallarray, 1, 5)
This seems like multiple things are wrong here that should have undefined behavior.
- I would expect that passing smaller size array into a larger size assumed array would mean some of the indices inside the subroutine would be invalid memory locations.
- While the
smallarray
and the size of thearraylarge(:,indexmin:indexmax)
have the same have the same size,SIZE(small array, 2) .eq. SIZE(arraylarge(:,indexmin:indexmax),2)
, I would expect that should be accessing unallocated memory sincesmallarray
isn't defined from indices 1:4. Unless it's doing something likearraylarge(:,1:5) = smallarray(:,5:10)
in the background. But that would seem to me to be something that would be compiler dependent.
What should happen when a smaller array is passed into an larger assumed size array? It seems like this should cause a BAD_ACCESS
condition but it isn't.