Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Legal Fortran?

lklawrie1
Beginner
853 Views
I've had a couple of people on my development team do this, now, and I can't believe it's legal but not only don't the compilers (CVF, IVF, Lahey) complain about it -- it seems to work!
Imagine:
Global variable: SomeNumber (integer, in a data only module)
in another module...
subroutine x(parameters)
...
Logical MyArray(SomeNumber)
....
use MyArray in a variety of statements
end subroutine.
Note that SomeNumber will be set prior to this subroutine call but it is a variable, not a parameter where it is declared.
I prefer that they allocate and deallocate in this situation. But is the above even legal?
Comments appreciated.
Linda
0 Kudos
4 Replies
nijhuis
Beginner
853 Views
I was very surprised. I never noticed that such a construction works. But when I read the Language Reference Manual about "Specification expressions" I must admit the correctness of the array declaration.
In the following::
Code:
subroutine sub()
use mod1
integer arr(n)
...

end
n is an specification expression. That is a restricted expression of type integer and filled with a scalar value. n should be made accessible for instance by use association (via the modulemod1 in our example). So if n is preset in the module mod1 or set in another routine prior to the call to sub, then the array allocates n integers.
As I remark, it was new for me. I also don't know when this feature is introduced in Fortran. Is it specific Fortran 90 or 95?
Guus
0 Kudos
Steven_L_Intel1
Employee
853 Views
There's a couple of different features here. First, automatic arrays, local variables whose bounds are not known untiil the routine is entered, are new in F90. Similarly, use and host association are new in F90. F77 (and F66) had the concept of adjustable arrays as arguments, where you could say:
subroutine sub (a,j)
real a(j)
You knew that. But you could also have this:
subroutine sub (a)
common j
real a(j)
This is also legal in F77. Use and Host association just extend the ways that the value of the bound can be communicated.
0 Kudos
lklawrie1
Beginner
853 Views
Ewwww. I probably knew it but put it out of my mind! I'm not sure I want to tell others in my team of developers about it -- seems like it could be different on different compilers. I already have enough of those headaches!
Thanks for the reply.
Linda
0 Kudos
Steven_L_Intel1
Employee
853 Views
I would not expect it to be different on different compilers, unless there's a bug. But this isn't an area I've seen bugs in.
0 Kudos
Reply