I have written a fortran code which is failing in a way that I do not understand. I have tried to explain the scenario clearly below but please ask me to clarify if it is not clear.
The code includes a subroutine
SUBROUTINE TMGP(NSYM,NOB,NFT,DEN,ncod,PR,np,lden,NPMX,nuccen,mdel)
IMPLICIT NONE
...
INTEGER :: NINTS
REAL(KIND=wp), ALLOCATABLE :: XBUF(:)
...
print *,"xbuf allocated ?",allocated(xbuf)
print *,"xbuf not allocated ?",.not.allocated(xbuf)
if (allocated(xbuf)) then
DEALLOCATE(xbuf)
end if
if (.not.allocated(xbuf)) then
allocate (xbuf(nints))
end if
...
RETURN
END SUBROUTINE TMGP
However when I run this program it fails on the line:
DEALLOCATE(xbuf)
With the error
forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated
With the output:
xbuf allocated ? T
xbuf not allocated ? F
If I change the code to (and make NO other changes than adding this line):
print *,"xbuf allocated ?",allocated(xbuf)
print *,"xbuf not allocated ?",.not.allocated(xbuf)
print *,"nints = ",nints
if (allocated(xbuf)) then
DEALLOCATE(xbuf)
end if
if (.not.allocated(xbuf)) then
allocate (xbuf(nints))
end if
and run this program it fails on the line:
allocate (xbuf(nints))
and I get the message:
forrtl: severe (151): allocatable array is already allocated
With the output:
xbuf allocated ? F
xbuf not allocated ? T
nints = 1
I am quite baffled by this as it seems to me the if statements I have used should make this kind of issue impossible and following the logic of the code these errors should not be possible.
I am using ifort compiler, any ideas on what could be causing this issue or suggestions on how to fix it would be very much appreciated.
Please let me know if any additional information would be useful.
James