Common

Statement Purpose

The COMMON statement specifies common blocks and their contents. The contents of these common blocks can then be shared between different program units. This cuts back on the number of variables that need to be passed to functions and subroutines via argument lists and provides universal values for those variables that can be used in those program units. Furthermore, common blocks also provide for the sequential storage of variables inside the computer. This means the second variable declared in a common block will occupy the next memory space after the first variable declared in the common block. If a specific sequential storage of shared variables is not import to you, then you should use Fortran 90 Module program units combined with the USE statement, rather than common blocks.

Rules and examples on the use of Common Blocks

Perhaps the most important rule to be aware of is that in FORTRAN it is permissible to have only one unnamed common block in a program. Although, there can be more than one named common block. The difference between an unnamed and named common are illustrated below, with the unnamed common appearing first.
		common ivar,ierr,int1
		common/stress/sigma,ymod
The name "stress" is just an identifier for a block of information and does not have any type (REAL, INTEGER,...) associated with it. Even though it was stated earlier that there can be only one unnamed common per program, a second occurrence of the blank common declaration in the above example would not lead to a compiler error. Instead, the second declaration would just be treated as an extension to the earlier blank common block. Therefore this common declaration,
		common x,y,z
		common a,b,c
would be exactly the same as,
		common x,y,z,a,b,c
Another thing to be aware of when using common blocks is that the variable lists that follow the declaration of a common block do not have to be the same in different program unit. With common blocks, it is the order in which the variables appear that determines their value in each program unit. For example, if the named common, stress, in the first example was declared in the main program and I wanted to use it a subroutine, I could do the following in the subroutine:
		common/stress/p1,e1
The value in the subroutine of p1 would be exactly equal to that of sigma in the main program and e1 would equal ymod. It is strongly recommended to avoid this when writing programs. The reason is that the lack of a consistent name for a variable can lead to a confusing, sloppy code.

The types of variables that can included inside a common block are not limited and the types can be mixed inside of them as well. However, be very careful if you mix variable types inside of a common block. Most computers want double precision real variables to begin at even multiples of the double precision word length. If "x1" and "y1" are 64 bit real numbers, and "i1" is a 32 bit integer in the example below, the even 64 bit spacing for the real numbers is broken, and trouble can result.
		common/vars/x1,i1,y1
Also, character variables can screw this spacing up as well, since the number of bits they take up is dependant upon their length. To avoid problems associated with the mixing of variable types, try to separate variable types into different common blocks. If that isn't possible ( as in the use of a blank common ) try to list all your real variables in the common block before any integer or character variables.

If you have a need to set initial values for the variables listed inside of a named common block, this can be accomplished by the use of DATA statements within a BLOCK DATA program unit. Otherwise, values can be given to the variables inside of a common block at antime during the execution of the program with normal assignment statements. You can never initialize variables in blank common with DATA statements, because the memory for blank common is not allocated until execution time.

The final thing that you should know when using common blocks is that they can be extended by the use of the equivalence statement. This can be done by declaring an array to be equivalent to the last entry in a common block like in the following example.
		common/vars/a,b
		real c(3)
		equivalence (b,c)
In this example, the first element of the array c would be stored in the same place as the variable b in the original common block. The rest of the elements in c will then be stored sequentially after b in memory.

For more information and examples see

lecture thirty eight

examples fall.f and trapezoid.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu