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

Allocatable component of variable with intent(out) does not get deallocated on entry to sub.

alexandross
Beginner
514 Views
Hello,

When the code below is compiled and run I get the following error:

$ ifort -V
Intel Fortran Intel 64 Compiler XE for applications running on Intel 64, Version 12.1.1.256 Build 20111011

$ ifort -g -traceback -assume realloc_lhs test_module.f90 test_program.f90
$ ./a.out
forrtl: severe (151): allocatable array is already allocated
Image PC Routine Line Source
a.out 00000000004672CA Unknown Unknown Unknown
a.out 0000000000465DC6 Unknown Unknown Unknown
a.out 000000000043E860 Unknown Unknown Unknown
a.out 000000000042375E Unknown Unknown Unknown
a.out 00000000004099FA Unknown Unknown Unknown
a.out 0000000000402DF1 my_module_mp_arra 27 test_module.f90
a.out 0000000000404935 my_module_mp_mt_c 56 test_module.f90
a.out 0000000000404EED my_module_mp_mt_c 70 test_module.f90
a.out 00000000004051F3 MAIN__ 11 test_program.f90
a.out 0000000000402A9C Unknown Unknown Unknown
libc.so.6 00007FDB213D7C4D Unknown Unknown Unknown
a.out 0000000000402999 Unknown Unknown Unknown

The problem is that on entry to subroutine "array_to_mt", mt_var%c does not get deallocated although mt_var has intent(out).

The funny thing is that if I comment out the whole of subroutine "mt_transpose" (which is not used anywhere by the program) then the program works fine!

$ ./a.out
1.0000 0.0000 0.0000
2.5000 -3.0000 -4.0000
2.0000 0.0000 0.0000

It looks like a bug, but maybe since the code is a bit complex you can pinpoint some bad programming practice of my behalf which causes the error.

Thanks a lot,
Alexandros


[fortran]module my_module

implicit none

private
public :: my_type

type my_type
    real, allocatable, dimension(:,:) :: c
    integer, dimension(2) :: n = 0
  contains
    procedure :: compact => mt_compact
    procedure :: copy => mt_copy
    procedure, private :: array_to_mt
    generic :: assignment(=) => array_to_mt
end type my_type

contains


subroutine array_to_mt (mt_var, carray)
    class(my_type), intent(out) :: mt_var
    real, dimension(:,:), intent(in) :: carray
    allocate(mt_var%c(0:size(carray,1)-1, 0:size(carray,2)-1))
    mt_var%c = carray
    mt_var%n = shape(carray) - 1
end subroutine array_to_mt


type(my_type) function mt_transpose (mt_var) class(my_type), intent(in) :: mt_var mt_transpose = transpose(mt_var%c) end function mt_transpose recursive subroutine mt_compact (mt_var) class(my_type), intent(inout) :: mt_var integer :: n1, n2 real, allocatable, dimension(:,:) :: carray n1 = mt_var%n(1) n2 = mt_var%n(2) if ( n1 > 0 .and. all( mt_var%c(n1,:) == 0. ) ) then carray = mt_var%c(0:n1-1,:) mt_var = carray call mt_compact(mt_var) else if ( n2 > 0 .and. all( mt_var%c(:,n2) == 0. ) ) then carray = mt_var%c(:,0:n2-1) mt_var = carray call mt_compact(mt_var) end if end subroutine mt_compact type(my_type) function mt_copy (mt_var) class(my_type), intent(in) :: mt_var integer :: i, j mt_copy = mt_var ! actually some processing would take place here instead of a simple copying call mt_copy%compact() end function mt_copy end module my_module[/fortran]


[fortran]program test_program
use my_module
implicit none

type(my_type) :: p, q
real, dimension(3,4) :: a1 = reshape( real([ 1., 2.5, 2., 0., -3., 0., 0., -4., 0., 0., 0., 0. ]), [3, 4] )
integer :: i

p = a1
q = p%copy()

do i = 0, q%n(1)
    print "(8f15.4)", q%c(i,:)
end do

end program test_program[/fortran]

0 Kudos
1 Solution
Steven_L_Intel1
Employee
514 Views
Thanks for the test case. It's strange - we fixed a bug that looks just like this for 12.1, but the test program for that one now passes yet this doesn't. I have escalated this as issue DPD200176328.

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
515 Views
Thanks for the test case. It's strange - we fixed a bug that looks just like this for 12.1, but the test program for that one now passes yet this doesn't. I have escalated this as issue DPD200176328.
0 Kudos
Steven_L_Intel1
Employee
514 Views
This has been fixed for a future version.
0 Kudos
Reply