- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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.
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.
- Etiquetas:
- Intel® Fortran Compiler
Enlace copiado
1 Responder
- Marcar como nuevo
- Favorito
- Suscribir
- Silenciar
- Suscribirse a un feed RSS
- Resaltar
- Imprimir
- Informe de contenido inapropiado
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.
(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.
Responder
Opciones de temas
- Suscribirse a un feed RSS
- Marcar tema como nuevo
- Marcar tema como leído
- Flotar este Tema para el usuario actual
- Favorito
- Suscribir
- Página de impresión sencilla