Search code examples
arraysfortrangfortranfortran-common-block

Gfortran COMMON block with array size


I am having some trouble with old fortan code that I want to compile on gfortran.

 INTEGER NEQ
 DOUBLE PRECISION RE
 COMMON /DM18J/ RE,NEQ
 CALL FUNC(NEQ,RE)

The problem with the code is that the function assumes that RE is an array of size NEQ. However, I cannot specify beforehand the size of the array because it comes from the common block.

As the code is quite lengthy I was hoping to fix yhis without resorting to module variables. A thing that I might want to try is to insert NEQ as an argument instead of getting it via the common block.


Solution

  • If RE is an array, then it should be declared as such. Say, for argument, it is of size 100

    parameter (maxre = 100)
    integer neq
    double precision re(maxre)
    common /dm18j/re, neq
    

    When calling your routine, you need to specify the size of the array the function needs to operate on. This is not necessarily the size of the array. Your array could have 10000 elements but if you only wish to operate on the first 2, just set NEQ to 2.

    ! sanity check
    if (neq .gt. maxre) then
        print *, 'increase the size of maxre to at least ', neq
        stop
    end if
    
    call func(re, neq)