Print

Print


David Byrne writes:
 > I have a memory management question regarding the function attached below.
 > When an allocatable array is allocated within a function, if I fail
 > to deallocate the array before exiting the function, is this a problem?
 > I'm worried that each time I call this function, the machine assigns
 > another section of memory to the array, and over time eats up all the
 > memory I have available.

The answer is *GREATLY* different between f90 and f95.  The treatment
of allocatable arrays in cases like this in f90 was considered to be
bad enough that it was fixed in f95 (in a compatable way - legal f90
programs remain legal f95 ones, but there is a large class of programs
that were illegal in f90, but legal in f95).

Note also that the answer depends on whether or not the array has the
SAVE attribute.  Yours doesn't, but be aware that if you have an array
with the SAVE attribute, the answer is different.  If the array has
the SAVE attribute, then it stays allocated when the function...um I
mean subroutine (you called it a function, but I see it is really a
subroutine - the answer is the same for either) returns.  The
subsequent allocation will then be illegal; it is illegal to allocate
an array that is already allocated.

Without the SAVE attribute in f95, the array is automatically
deallocated when the subroutine returns.

Without the SAVE attribute in f90, the array become unusable if you
ever return without deallocating it.  You can never again do anything
with the array.  It's allocation status is undefined - neither
allocated nor unallocated.  You can't subsequently allocate it,
because you can only allocate arrays that are unallocated.  You
can't deallocate it because you can only deallocate arrays that are
allocated.  There is no way to ever legally use the array again.
(Yes, pretty much everyone agrees this was a mess - fixed in f95).

 > Thanks for any advice.

Well, my advice is to always deallocate what you allocate.  You
can get by without it here in f95, but I advise doing it anyway.
I just consider it a better programming style,  And if you intend
your code to be usable on an f90 compiler that hasn't been upgraded
to f95, then it is required.  It looks easy enough to do here.

Um.  Say.  Looking at the code for a few seconds more, I have
a different piece of advice for this one.  I still think it best
to deallocate what you allocate, but here I'd do neither.  I'd
use an automatic array here instead of an allocatable one.
You aren't taking advantage of any of the flexibility that
allocatable arrays have (error handling, sizes given by expressions
that aren't known on subroutine entry).  So I'd replace

         integer,dimension(:),allocatable :: temp

with

         integer,dimension(size(prod)),allocatable :: temp

and then get rid of the allocate statement.  The compiler will take
care of both the allocation and the deallocation automatically
(thus the term "automatic array") for you.

--
Richard Maine                |  Good judgment comes from experience;
[log in to unmask]   |  experience comes from bad judgment.
                             |        -- Mark Twain