fortrangfortranlapack

Type mismatch in LAPACK seemingly resolves itself


I am going through some old Fortran code and there are frequent calls to the LAPACK routine dsyevd, which takes an argument IWORK, expected to be an integer array. In this code, IWORK is often declared as a double precision array, and seemingly this has always worked. Indeed if I compare the outputs from dsyevd for IWORK as integer or double precision array the output is equivalent.

I wonder how this can be, given that dsyevd.f as defined here explicitly declares IWORK as an integer array. Hence I would expect gfortran to give some sort of type mismatch error, but this doesn't happen, and as I indicated the routine works as expected. Of course I will use the proper integer array going forward with this code, but can someone explain to me why this "wrong" code has always worked?

Thanks!

Edit: Some further testing reveals that gfortran 10 does actually report a type mismatch error upon compilation. However gfortran 9 and ifort both compile without issue and seemingly give correct output.


Solution

  • Legacy Fortran codes, and this includes the LAPACK routines, have only implicit interfaces. That is, the compiler did not (and mostly could not) check the arguments upon a function or subroutine call: it was entirely up to the programmer to make sure he was providing the right arguments.

    Now, why is it working fine anyway when the iwork actual argument is declared as double precision instead of integer? Under the hood, what the compiler passes to the routine is just the address of iwork in memory, and the routine uses the memory area to temporarily store some integer values.

    This could go wrong because of two reasons:

    The use of implicit interfaces is still possible in modern Fortran, but the recommendation is to use explicit interfaces (available since Fortran 90).