Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
32 Views

Issue in allocating arrays

Hi All,

I am seeing a very strange situation that allocating an array is unexpectly deallocating another array under the same scope. E.g., the code I have is like this:

        Subroutine memalloc
        USE data_mod
        if(.not.allocated(x1)) allocate(x1(100)) 
        if(.not.allocated(x2)) allocate(x2(100))

        ...

        if(.not.allocated(x100)) allocate(x100(100)) 

        END

Within the debugger, I can see that x1, x2,...,x100 are undefined pointer/array. The program executes the first line and allocate the memory for x1 perfectly. However, while executing the second line, x2 is allocated with x1 back to undefined pointer/array.

I am using Intel Parallel Studio XE 2015 Composer. What is more mystery is that I am only seeing it for certain subroutine while the others are working fine. These memory allocate subroutines are generated from a makefile and I really don't find anything particular for those not working.

Hope I can get some help here.

Best,

Yang

0 Kudos
11 Replies
Highlighted
Black Belt
32 Views

It would be best if you can provide test code with which the problem can be replicated.

What are the contents of data_mod? Please show us the declarations of x1, x2, ....

0 Kudos
Highlighted
Valued Contributor III
32 Views

The debugger might be tying to you, it does with some things from time to time. Have you tried using the supposedly unallocated array?

0 Kudos
Highlighted
Beginner
32 Views

mecej4 wrote:

It would be best if you can provide test code with which the problem can be replicated.

What are the contents of data_mod? Please show us the declarations of x1, x2, ....

Thank you for the prompt reply. Unfortunately I don't have a small test code which can reproduce this issue. The data_mod is the module that declares x1, x2, ... in the standard way like this:

integer, DIMENSION(:), ALLOCATABLE :: x1

integer, DIMENSION(:), ALLOCATABLE :: x2

etc.

0 Kudos
Highlighted
Beginner
32 Views

andrew_4619 wrote:

The debugger might be tying to you, it does with some things from time to time. Have you tried using the supposedly unallocated array?

Yes, I have tried to put deallocate but still it didn't work. And the weird part is that it is doing this for variables in some modules but not in the others, while all my modules are generated from a makefile. I have also tried to 'shuffle' the sequence of allocating the variables and it didn't work either.

0 Kudos
Highlighted
Valued Contributor III
32 Views

On allocate I always use STAT to check if it succeeded OK.

 

0 Kudos
Highlighted
Black Belt
32 Views

If an allocate statement fails in a way that the compiler's runtime can detect (such that it could return a non-zero stat if a STAT specifier were present), then the compiler's runtime would have terminated the application with an appropriate message.

As andrew states in #3, don't rely on the debugger to determine the allocation status of a variable - if you think something is going astray, you need to write some actual code to check the state of things (and even then, if memory corruption is going on that may not be sufficient).  Sometimes the debugger gets a little confused as to where things have been laid out in memory.

Subroutine memalloc
  USE data_mod
  if(.not.allocated(x1)) allocate(x1(100)) 
  if(.not.allocated(x2)) then
    allocate(x2(100))
    print "('after allocating x2, allocation status of x1 is ',l1)", allocated(x1)
  end if
  ...

Remember that we cannot see your computer - "it didn't work" doesn't convey useful information.

0 Kudos
Highlighted
Beginner
32 Views

andrew_4619 wrote:

On allocate I always use STAT to check if it succeeded OK.

 

Thank you Andrew. I have put STAT in my code as following:

if(.not.allocated(x1)) allocate(x1(100), stat = ierr) 
if(.not.allocated(x2)) allocate(x2(100), stat = ierr) 
if(.not.allocated(x3)) allocate(x3(100), stat = ierr) 

Executing of first line gives ierr as 0, with both x1 and x2 allocated. The program skips the second line as x2 is already allocated (and no reason it should be allocated when executing line 1). Then it goes to the third line and pass the logic, but ierr is 151 during allocation. My guess is that there is memory corruption during this process but I can't see how.

0 Kudos
Highlighted
Valued Contributor III
32 Views

how are x1,2,3 declared? Is there any illegalily e.g. are they in a COMMON or equivalence or simillar?

0 Kudos
Highlighted
Valued Contributor III
32 Views

logical :: Lall(3)

! check before
Lall(1)  = allocated(x1)
Lall(2)  = allocated(x2)
Lall(3)  = allocated(x3)
if( .not.Lall(1) ) allocate(x1(100),STAT=ierr)
if( ierr /= 0)  do_something
! check again has it changed?
Lall(1)  = allocated(x1)
Lall(2)  = allocated(x2)
Lall(3)  = allocated(x3)

The above might be interesting to know .....

0 Kudos
Highlighted
Beginner
32 Views

Thank you everyone for the help and I have figured out where the problem is. My program is a mixed C/Fortran program. In it, I am creating a head file in C to reference the C variable to the macro generated from the Fortran module. E.g., the variable x1 in module data_mod will have a macro in C as DATA_MOD_mp_X1. I am defining the same variable name x1 in C as DATA_MOD_mp_X1 and seems that this messed up the memory allocation in Fortran. If I removed those header files, the allocation will work but I need to find a different way of linking C and Fortran variables.

0 Kudos
Highlighted
Honored Contributor I
32 Views

Yang F. wrote:

.. reference the C variable to the macro generated from the Fortran module. E.g., the variable x1 in module data_mod will have a macro in C as DATA_MOD_mp_X1. I am defining the same variable name x1 in C as DATA_MOD_mp_X1 and seems that this messed up the memory allocation in Fortran. If I removed those header files, the allocation will work but I need to find a different way of linking C and Fortran variables.

@Yang F.,

Note the Fortran standard says in Note 18.15 of draft text toward Fortran 2015, "An allocatable array or array pointer is never interoperable. Such an array does not meet the requirement of being an explicit-shape or assumed-size array."  Something to keep in mind since you show X1 as haveing the allocatable attribute.

Re: "I need to find a different way of linking C and Fortran variables," instead of linking variables directly, you may want to manage the data only through interoperable procedures in your mixed C/Fortran program.

0 Kudos