Search code examples
fortranglobal-variables

Are there similar implementations of "COMMON", a referencing environment from Fortran, in other languages?


Fortran language has a referencing environment called COMMON.

As defined in the website below, the COMMON statement defines a block of main memory storage so that different program units can share the same data without using arguments.

https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn7v/index.html

Sample implementation looks like this:

enter image description here

I wonder if there are similar implementations of this kind of environment in other languages like C, Python, or Java and how it differs from the Global environment.


Solution

  • COMMON is just global memory, most computer languages have some form of global memory. What makes Fortran's COMMON a little strange is that it has to be declared by every subroutine that want to use it and those declarations can vary. That declarations can vary is quite unusual but you to remember that Fortran is very old. Features which seem odd in 2021 may have seemed reasonable forty years ago.

    Fortran-66 and Fortran-77 programs were written back in the day when 64 kilobytes was a lot of memory! Every byte mattered and if you could use some of it for multiple uses then all the better. Sharing a common block with different memory layouts was a good way of sharing memory. Does it present issues: you bet it does! Sharing memory like this can lead to all sorts of bugs if you are not careful. It is a means to an end.

    It is also worth mentioning that Fortran has another way to map memory into different types, EQUIVALENCE: https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn9b/index.html.

    It is easy to simulate in C or C++ by having different types of structs point to the same location - or you could do it with a union.