Gie Grajo
Gie Grajo

Reputation: 207

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.

Upvotes: 3

Views: 430

Answers (2)

Rob
Rob

Reputation: 3381

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.

Upvotes: 3

Scientist
Scientist

Reputation: 1835

I tried to squeeze everything in a comment. But that did not work. So here is a more extensive answer.

The common block is rarely used in modern Fortran and its usage has long been deprecated. For at least the past 3 decades, modules have been the official proper way of data sharing in Fortran. The utilities of modules in Python and Fortran are almost identical (though Python module organization as a hierarchy of folders is a bit more flexible than what can be done in Fortran). Here is an example

module dataSharing
   real :: exampleModuleVariable = 0.
end module dataSharing

program main
    call print()
end program main

subroutine print()
    use dataSharing, only: exampleModuleVariable
    write(*,*) "exampleModuleVariable before = ", exampleModuleVariable
    exampleModuleVariable = 1.
    write(*,*) "exampleModuleVariable after = ", exampleModuleVariable
end subroutine print

But before Fortran modules, specifically in FORTRAN77, the primary method of data sharing was through areas of storage known as common blocks. A common area of storage (which may be either named or unnamed) could be defined via the following syntax:

program main
    real :: exampleModuleVariable = 0.
    common / dataSharing / exampleModuleVariable
    call print()
end program main

subroutine print()
    common / dataSharing / exampleModuleVariable
    write(*,*) "exampleModuleVariable before = ", exampleModuleVariable
    exampleModuleVariable = 1.
    write(*,*) "exampleModuleVariable after = ", exampleModuleVariable
end subroutine print

The above two codes (one with module and the other with common) are functionally equivalent. But the module style is strongly preferred to the other older F77 deprecated approach. If you follow the Fortran module style, its translation to a Python module should be fairly easy as the concepts are highly similar in both languages, although the syntax is slightly different. I do not think if C has anything comparable to a common block and certainly does not have the concept of modules. However, C++20 has recently added the concept of modules to C++.

One last thing: The Oracle F77 manual is too old to rely on, except for maintaining legacy F77 codes. The Intel, HP/Cray, and IBM Fortran manuals are quite modern and their compilers support all or most of the latest features of modern Fortran (2018/2008/2003). So are GNU, NAG, and NVIDIA Fortran compilers.

Upvotes: 6

Related Questions