DEALLOCATE a F95 array with SAVE attribute


javier_garcia-p
04-30-2010, 01:30 AM
Hi all;
I have a SUBROUTINE with a local allocatable array:

REAL(DP), ALLOCATABLE, DIMENSION(:), SAVE :: tmp

It seems to me this array cannot de DEALLOCATED within the subroutine

This SUBROUTINE is designed to work within a program that usually does one run for a number of timesteps , and the array is allocated at the first time step (it is a formal argument to the subroutine).

IF (it == 1) THEN
ALLOCATE(tmp(nsteps), STAT=status)
END IF

However, if I am going to conduct a MonteCarlo analysis, where the calling program does not terminate and this subrutine is going to be called thousands of times with the condition it == 1 being TRUE. The following crashes at compilation:

IF ( it == nsteps) THEN
DEALLOCATE(tmp, STAT=status)
error = error + status
END IF

So it seems deallocation of "SAVE" arrays cannot be done. Is this right?

Thanks and best regards,
Javier
---

davekw7x
05-01-2010, 12:03 AM
...It seems to me this array cannot de DEALLOCATED within the subroutine ...
I'm not real sure what you are trying to do that you can't do.

The following works for me (GNU gfortran Version 4.1.2)


SUBROUTINE foo(it, nsteps) !Probably some additional parameters

IMPLICIT none

INTEGER it, nsteps

DOUBLE PRECISION,DIMENSION(:),SAVE,ALLOCATABLE :: tmp
INTEGER status

IF (it == 1) THEN
IF (ALLOCATED(tmp)) THEN
DEALLOCATE(tmp,STAT=status)
! Do something if status not zero?
END IF
ALLOCATE(tmp(nsteps),STAT=status)
! Do something if status not zero?
END IF

!
! Do stuff
!

IF (it == nsteps) THEN
if (ALLOCATED(tmp)) THEN
DEALLOCATE(tmp,STAT=status)
! Do something if status not zero?
END IF
END IF

END SUBROUTINE foo


If this doesn't help, then I suggest that, if you want to get more, you have to give more: A complete (small) example that illustrates the problem might give us a clue.

Regards,

Dave