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

dynamic allocation weirdness

Brian_Murphy
New Contributor II
575 Views

The first time I submitted this post, it didn't appear in the forum, so I'm posting it again. Each time through a do loop I am allocating, calculating, and deallocating a set of arrays. The second time through the loop all is well until deallocating where an exception is thrown. I am checking the ALLOCATED property before deallocating, and it looks ok. But something is not right and the exception is 100% repeatable. All the arrays are local to the subroutine, and are declared with the ALLOCATABLE property. Is there anything special one should do when using a set of arrays in such a fashion? The arrays are small. Just a few hundred elements each. Some are REAL, some are COMPLEX. Here's how I'm doing it for each array. Complex(8), ALLOCATABLE :: EIVEC_COMPLEX(:,:) do i=1,whatever ALLOCATE (EIVEC_COMPLEX(N4,N4)) if( allocated(EIVEC_COMPLEX) ) DEALLOCATE (EIVEC_COMPLEX) end do Thanks, Brian in Austin, Texas

0 Kudos
5 Replies
Michael_Roberts
New Contributor I
575 Views

Hi Brian,

the following code compiles and runs fine for me - is this what you meant? If so are you definently attempting to deallocate the same array as you are checking on each line? Could you post a complete code which exibits the problem?

Cheers,

Michael

[fortan]

PROGRAM CheckAlloc
IMPLICIT NONE

INTEGER :: i
DOUBLE PRECISION, ALLOCATABLE :: array1(:,:)
COMPLEX(8), ALLOCATABLE :: array2(:,:)

DO i = 1, 10
ALLOCATE(array1(10,100))
ALLOCATE(array2(10,100))

! Do some calcs with the arrays
array1 = 1
array2 = 2
array2 = array1 * array2

! Deallocate
IF (ALLOCATED(array1)) DEALLOCATE(array1)
IF (ALLOCATED(array2)) DEALLOCATE(array2)
END DO

END PROGRAM CheckAlloc

[/fortan]

0 Kudos
Les_Neilson
Valued Contributor II
575 Views

I don't know whether it is still the case but I found that I sometimes needed to deallocate the arrays in reverse order (allocate array1 to N, deallocate arrayN to1) although my arrays were quite large (~ MBs) In the end I just got into the habit of doing it that way.

Les

0 Kudos
Brian_Murphy
New Contributor II
575 Views

I just put the dealloc's in reverse order of the alloc's, and it didn't seem to make any difference.  It makes it through the loop fine the first time, and fine the second time untill reaching the dealloc's.

I just put in a GOTO to go straight from the alloc's to the dealloc's and that doesn't crash.  So I'm on to something.  Either I or IMSL must be clobbering something.  Hopefully moving the GOTO will enable me to find where that is.

Brian

0 Kudos
andrew_4619
Honored Contributor III
575 Views

Put stat=istat in the deallocate call and then you can check for  istat.ne.0 to trap the error and get an error number

0 Kudos
Brian_Murphy
New Contributor II
575 Views

I found it. I was merely overstepping array bounds in an array assignment statement. I sure thought I had run-time error checking set to ALL, but it was CUSTOM with everything off. This isn't the first time that's happened. I wonder if VS2012 has a problem retaining project settings.  Anyhow, on with the show.

Brian

0 Kudos
Reply