I’ve inherited a Fortran module that has a named common sized differently in several different routines within the module.
For example, subroutine 1 has it defined:
common /mycommon/ x(100),y(100)
while subroutine 2 has it defined as:
common /mycommon/ x(500),y(1000),z(2000)
and so on in several different subroutines.
I’m afraid this situation could result in unwanted overwriting of data.
What does the compiler do with this? Does is analyze the different common definitions and allocate enough storage to make room for them all? Does it take the first or last reference as the proper size?
What does the compiler do with this? Does is analyze the different common definitions and allocate enough storage to make room for them all? Does it take the first or last reference as the proper size?
This is called “sequence association”. What happens is that the compiler will create a storage area big enough to handle the largest block. Then it’s up to each subroutine on how the data is accesses.
In your example, sub1’s “y” is equivalent to sub2’s x(200). The names don’t matter, only the relative position within the common block.
What happens when named common blocks are of different sizes is not part of the standard. Though, it’s common to do this across the various compiler vendors.