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

Allocatable array already allocated runtime error on calling type bound procedure

potaman
Einsteiger
537Aufrufe
I have a pointer to a user defined type within another user defined type. When I call a subroutine belonging to the pointer , I get an allocatable array already allocated error. Any clue? This is completely non-intuitive, the pointer is associated and all the fields have been properly initialized.

This is roughly the structure of the code.
ype B
   type(A),pointer :: obj
 contains
    procedure :: do_something
 end type B
type A 
    integer,allocatable:: long_array(:) 
  contains
    procedure :: do_something_to_long_array
end type A

 program main
  type(B) :: obj1
  type(A) ,target :: obj2

  allocate(obj2%long_array(10))
  obj1%obj=>obj2
  call obj1%obj%do_something_to_long_array()
 end program main

The runtime error happens at the final subroutine call. There is no allocation within the subroutine. it just reads a fixed number of values into a smaller array for use.

 

 module test
      type A
      type(B),pointer :: bpoint=>null()
      contains
      procedure :: test_point
      end type A

      type B
      integer,allocatable :: long_array(:)
      contains
      procedure :: read_section_of_long_array
      end type B
      contains
      subroutine test_point(this)

      class(A) :: this
      integer :: a(3)
      call this%bpoint%read_section_of_long_array(a,3)
      write(*,*) a
      end subroutine test_point

      subroutine read_section_of_long_array(this,a)
      class(B) :: this
      integer:: a(:)
      integer ::l,i
      l = shape(a)
      write(*,*) shape(a),shape(this%long_array)
      do i=1,l
         write(*,*) i
         write(*,*)  this%long_array(i)
         a(i) = this%long_array(i)
      end do

     end subroutine read_section_of_long_array
     end module test
 
     program main
     use test

     type(A) :: aobj
     type(B),target :: bobj
     integer :: i


     allocate(bobj%long_array(10))

     do i=1,10
        bobj%long_array(i) = i+1
        write(*,*) bobj%long_array(i)
     end do
     write(*,*) "here"

     nullify(aobj%bpoint)
     aobj%bpoint=> bobj

     write(*,*) "here"
     call aobj%test_point() 
     end program main



This approximates what I am trying to do. but this gives me a segmentation fault. I can't see why as no unallocated memory is being accessed.
0 Kudos
1 Antworten
Udit_P_Intel
Mitarbeiter
537Aufrufe
Do you have a small reproducer for this? That would help a lot.
(off the top of my head) If there is an assignment in the subroutine, then try -assume realloc_lhs . That will deallocate and reallocate (to correct size) an allocatable.
Antworten