Cannot remove COMMON block, despite setting global values

As stated in a previous ppost, my old FORTRAN code has a lot of COMMON statements; or did have.

I have successfully removed most of these simply by defining the variables within an included file (will be a module, but not yet). Where possible, I have included these within the parameters of the subroutine, thus reducing the number of globals - which is still insane!

Anyway, I have 1 remaining COMMON block that is used within 2 subroutines…

            common / rates / rpp, rhe3he3, rcno, r3a, r12a, r16a, rcc,  &
     &                       rlip, xpd, xpc, xpn, xpo, xpp23

I have checked and the variables within the common block only appear within the relevant two subroutines and the two common statements within both routines are identical.

I have created the same variables within the include file…

		double precision :: rpp, rhe3he3, rcno, r3a, r12a, r16a, rcc
		double precision :: rlip, xpd, xpc, xpn, xpo, xpp23

Yet, the code now generates incorrect data. The code runs fine but the results are wrong.

So, the variable values are not remaining the same (since the common block is the only part of the system I am ruling out at present).

What am I missing?

Here a few ideas:

  • Have you checked that the variables in the include file (within a module)
    are actually used and have actually the type double precision.

  • Maybe you can create a “reduced” testcase to check what are the conditions that cause the error to happen

Your descriptions are ambiguous and confusing, and providing parts of the real code would be helpful.

If you have variables declared in an include file, and that file is included in two different subprograms, and those variables are not in a common block or modules shared by the two subprograms, those variables are local variables and the two sets of local variables are separate and distinct.

For example, if the file has a single line:

integer ijk

and you have two subroutines subA and subB in the same or separate source files:

subroutine subA()
include ''
end subroutine subA
subroutine subB()
include ''
end subroutine subB

the local variable ijk of subA is distinct from the local variable ijk of subB. Usually, such local variables are allocated dynamically on the stack, and portions of the stack used by a subroutine are reclaimed when the subroutine is exited. Likewise, having the same name does not imply any connection between variables of different program units.

1 Like

Sorry for the ambiguous and confusing nature of my post; I try to provide the essential information and get this wrong on occasion (probably far more than I think I do).

However, you have answered my question!

I realised that the reason for the other systems working is that I have either now parsed the variables through to the subroutines or the common blocks exist within the include files.

I had, indeed, misinterpreted the use of the include file, which might, in part, explain why my attempts at getting the module to work have not borne the fruit I had hoped to.

Thank you.