Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

What happens with this Allocate?

dajum81
New Contributor I
341 Views
SUBROUTINE STRCPY(STR1,STR2,N)
C
C @(#)strcpy.F 3.2 11/21/89
C
C Purpose:
C COPIES UP TO N CHAR OF STR2 TO STR1
C
C Argument List Definitions:
C STR1   - String to copy into
C STR2   - String to copy from
C N      - Max char to copy
C
C---------END OF DESCRIPTION -------
C
USE CSTR_MOD 
 
CHARACTER*(*) STR1,STR2
CHARACTER(len=:), allocatable :: dest
 
include 'cdefsu.inc'
 
I = MIN(N,LEN(STR2))
 
IF(use_cstr) THEN
allocate(character(len=I) :: dest)
call cstrcpy(dest, STR2, I)
STR1 = dest
RETURN
END IF
 
C
 
STR1 = ' '
STR1(1:I) = STR2(1:I)
RETURN
END
 
So what happens with this subroutine local variable being allocated every call, but never deallocated?  It doesn't crash with a message saying it was already allocated on the subsequent calls.  It seems to work fine most of the time.  But I have a case where I get a crash, but if I add a deallocate it doesn't crash.  So I suspect memory corruption, but I'm not sure how the compiler handles this situation because it is just a subroutine local variable.
 
Thanks,
Dave
0 Kudos
4 Replies
Steve_Lionel
Honored Contributor III
322 Views

Local variables with the ALLOCATABLE attribute but not SAVE get automatically deallocated when the procedure returns.

dajum81
New Contributor I
321 Views

Thanks Steve, any thoughts on why adding a deallocate statement causes my code to not crash?  

0 Kudos
dajum81
New Contributor I
273 Views

I think I can answer my own question.  I think the automatic deallocation checks the heap which was corrupted by code in another routine, but not checked there.  

Steve_Lionel
Honored Contributor III
235 Views

Glad to hear you got it sorted out.

0 Kudos
Reply