dynamic allocation weirdness

dynamic allocation weirdness

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

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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?




PROGRAM CheckAlloc

COMPLEX(8), ALLOCATABLE :: array2(:,:)

DO i = 1, 10

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

! Deallocate



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.


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.


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

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.


Leave a Comment

Please sign in to add a comment. Not a member? Join today