tags:

views:

58

answers:

1

I'm responsible for a number of legacy (F77) programs. On occasion one or another may fail due to attempting to exceed a fixed array size. My usual fix is to way over allocate the offending array.

Does anyone have thoughts or experience on changing these fixed arrays to dynamic and what are the ramifications to the rest of the code including subroutine calls?

I'm using a fairly new compiler on OpenVMS so I believe that there will not be compiler issues.

+4  A: 

Can you be a bit clearer about what you currently do and what you propose to do ? You state that you 'way over allocate' arrays, which suggests that you are already using dynamic arrays, then in the next sentence you ask about changing fixed arrays to dynamic.

Perhaps you mean that you define the arrays, at compile time, with more space than you expect to use ? That's one of the ways Fortran programmers have worked for a long time. Since Fortran 90, however, the language has supported, in a standard way, dynamic arrays, ie those whose size is established at run time. The keyword ALLOCATABLE is used to declare such arrays, and they are given space (on the heap generally) with the ALLOCATE procedure. Of course, thereafter the array size is fixed. To dynamically expand an array you generally have to ALLOCATE a larger array, then copy elements across.

If you expect to continue to use your legacy programs then I suggest that the effort of converting to allocatable arrays will be repaid. This is something that I, and I suspect most other Fortran programmers, have done a lot of in the years since Fortran 90 compilers became widely available. One of the ways in which the effort will be repaid is in allowing you to concentrate on other aspects of maintenance. Given the availability of memory on modern computers you need be much less concerned about using such space than the people who wrote the code a generation ago. I expect that the users of the code are trying to tackle much bigger problems than their forefathers too. Allocating arrays will provide some measure of future-proofing.

As for the impact on other parts of the code, give some thought to:

  • DEALLOCATEing when you are finished with the array.
  • Arrays allocated inside subroutines and functions are automatically deallocated on exit, unless you return the array to the calling unit -- but this is only possible (according to the standard, your compiler may differ) in Fortran 2003 and later.
  • If you ALLOCATE an array you can pass it in to, and get it out of, a subprogram just as you can any other array.
  • In FORTRAN77 it was standard practice to pass the dimensions of an array into a sub-program with the array; in Fortran 90 and later you don't have to do this, if you need to know the size there is the SIZE intrinsic. And with whole-array syntax you often don't need to know what size an array is anyway.
  • Rather than rewrite a lot of your array processing code you might find it easier to write wrappers around it.
  • As a matter of good practice you should check the value of the stat option to the ALLOCATE statement.

I expect there's a bunch of stuff I've forgotten.

High Performance Mark
Your third bullet point is, I think, critical to the original question - once you allocate, you can treat it as a normal array, passing it to older subroutines that weren't originally designed with allocatable arrays.
Tim Whitcomb