Automatic Objects

Automatic Objects

heres my problem,
When declaring my common statements such as...
common /cmg/ ng,ni(nb),nj(nb),nk(nb), ntsteps(nmax2)
I keep receiving this error...
D:Larryinit.f(10) : Error: A COMMON block data object must not be an automatic object. [NTSTEPS]

I get this error when my values in paranthesis are not hard coded into the program, but user defined (in this case nmax2). How could I declare my matrices in the common statements and still let the user define the value, without receiving this error?

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The error message is telling you that any data you place in a COMMON block must be static, meaning that the size must be known at compile time. Space for a COMMON block is not allocated at runtime. Why does this data need to be in a COMMON block?

I am rewriting a pre-existing program. One parameter that was hard-coded in the program before is now user defined. This parameter sets the size for many arrays, (which is used in many common statements). I just want to know if theres a simple code I can change or if I need to start from scratch. Like, if there is another way i can declare these arrays, where i will not receive this error

You have to get the arrays out of the common block. Once that is done, you can use allocatable arrays to do the job. Whether moving your array out of the common blocks is a big deal depends upon your code, but either you can define a structure to contain the data and pass it around if the number of routines is small, or move these variables to a module and make them public, like global variables, and USE that module in the relevant routines.


How can i declare these structures in a module without a common statement.



module data 
integer max_element 
integer, allocatable :: global_array(:) 
end module data 


program main 
use data 
integer i 
max_element = 1000 
allocate (global_array(max_element)) 
do i = 1, max_element 
  global_array(i) = i 
end do 
call sub 
subroutine sub 
use data 
type *, global_array(max_element/2) 

Have fun.


Just a side warning: if all instances of original common /cmg/ were declared the same (i.e. same types, order & sizes, not necessarily names), James' suggestion will work fine. However, "old masters" sometimes used commons for cheating, i.e. declaring work arrays which were mapped to something else as needed in order to spare memory, kinda:

common /cmg/ ng, ni(nb), nj(nb), nk(nb) 
common /cmg/ iwork(3*nb+1) 
common /cmg/ ng,array(2*nb)

This kind of spaghetti is hard to unwind if that's the case.


Leave a Comment

Please sign in to add a comment. Not a member? Join today