Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference memory-wise between using Common Blocks and Modules in Fortran 90

I am recently learning Fortran without any guidance, and experimenting with different versions. I have found from this site:

Is a MODULE better than a COMMON block?

Almost always yes. The only reasons to use COMMON blocks are if you expect to use your program on a computer with only a FORTRAN 77 compiler (they still exist), or if it is very important that you control the order in which your data is stored in memory.

Well, using modules is surely syntactically sweeter than using common blocks. But what are the differences in terms memory usage and allocation in both cases? Also does it make a difference in terms performance and access speed? Does that question make sense?

like image 680
osolmaz Avatar asked Feb 13 '23 22:02

osolmaz


2 Answers

M.S.B. has it in his answer, but does not stress it enough in my opinion. The variables in COMMON blocks are laid out in memory exactly in the order in the definition of the block. From this the restriction, that no dynamic memory objects (allocatable, pointer) can be in a COMMON block, immediately follows.

The "sequence association" means you can count on the placement of the variables in such a way that you can, e.g., use two following arrays as a large one.

COMMON blocks have probably no place in modern code, although they are not declared obsolete.

When it comes to speed, if the variable is the same, there shouldn't be any difference in accessing it, whether it is in a module or in a COMMON block.

like image 181
Vladimir F Героям слава Avatar answered Feb 15 '23 11:02

Vladimir F Героям слава


One difference memory-wise is that you can use allocatable arrays in modules, but not in common. (See Fortran common variables, allocatable array). Much more convenient if you have an array that you don't know the size of the array at compile time. The old FORTRAN way was to declare the array at some huge size that was hopefully large enough, but which typically wasted space. With the allocatable array you can allocate the array at the precise size needed.

I never use COMMON in new code. It is more limited and brings in the unnecessary "sequence association", i.e., associating variables by their layout in memory.

like image 38
M. S. B. Avatar answered Feb 15 '23 10:02

M. S. B.