-   Programming (
-   -   local dynamically allocated array in fortran (

otoomet 10-19-2009 12:01 PM

local dynamically allocated array in fortran

I (my friend) have a fortran program with the code along the following lines:


      REAL, DIMENSION(4000,4000) :: a, b, c

The problem is that the huge matrices are retained in memory even after the subroutine ends. Is there an easy way to declare the arrays in a sort of dynamic way, like they were in the following C example?


void ny(void) {
  double a[4000,4000];

I mean in the C example the memory is released as soon ny() exits.

Thanks in advance,

mostlyharmless 10-19-2009 03:12 PM

Not in *real* (i.e '66) FORTRAN :) Seriously, they made this possible in Fortran 90 , sort of in '77, see the following:

gotten from google "fortran 90 dynamic memeory allocation"

otoomet 10-19-2009 03:27 PM


But isn't there any simpler way? The problem is not so much not knowing the size while initializing the matrices, but the fact that the memory is not freed while exiting from the subroutine. It looks like the matrices were defined as "static double a[4000,4000]" in C. I'd like to have them temporary as simply "double a[4000,4000]".


mostlyharmless 10-19-2009 05:30 PM

Simpler way? I didn't think the example was all that bad, just a bit "C-ish". I don't know if there's any other way, other than coding everything in C, or making a C library subroutine for dynamic matrices, but those are both more complicated ways. Maybe in the next version of Fortran...

otoomet 10-20-2009 03:20 AM

Hmmm... It works in a sense that the variable values are not retained between successive function calls. However, it seems like the memory is not freed after return from the subroutine. The memory usage jumps every time I call a new subroutine, although I now allocate the memory in the way as suggested in the example.

But thanks,

mostlyharmless 10-21-2009 09:28 AM

Maybe that's a compiler implementation issue; I don't know enough about the Fortran 90 specs to say. If you have more than one compiler you can check it with, it'd be worth a try.

All times are GMT -5. The time now is 05:46 AM.