Fortran question: I successfully made an array of pointers, which point to elements in an array of objects
Compiler is gcc/5.4.0
NOTE:
Using gcc/6.4.0
this problem is solved.
options are:
>>gfortran -fdefault-real-8 -o H -fbacktrace -g -fcheck=all pointersToObjects.f90
>>./H
here is a picture of my problem
I based my array of pointers on the answer to Arrays of pointers
I successfully create the array, and if I call any particular element in it, it gives the correct result. However, if I try to loop through the array of pointers, I get a segmentation fault once it reaches values that point to the second object.
This is strange because if I explicitly call for the value stored in the array of pointers from object 2 or 3 etc, it outputs the correct value. It only fails if I try to loop through all values.
here is the code:
program pointers
type objects
real, allocatable :: array(:)
character(10) :: misc1=""
end type objects
type ptr
real, pointer :: p
end type ptr
class(objects), allocatable, target :: objectArray(:)
integer :: i, j, elem
type(ptr), allocatable :: pointy(:)
allocate(objectArray(3))
do i = 1,3
allocate(objectArray(i)%array(i+1)) ! arbitrary array length in each object,
enddo
allocate(pointy(9)) ! this is 2 + 3 + 4, dimeneions of each objectArray%array
elem = 0 ! dummy counter variable
do i = 1,3
do j = 1,size(objectArray(i)%array)
elem = elem + 1
! give dummy values to objectArray, then point to them with pointy
objectArray(i)%array(j) = rand()
pointy(elem)%p => objectArray(i)%array(j)
print*,i,j, 'obj: ', objectArray(i)%array(j), 'pointer: ', pointy(elem)%p
enddo
enddo
print*, 'size: ', size(pointy), elem, pointy(9)%p
print*, '========================='
do i = 1,size(pointy)
print*, i, pointy(i)%p
enddo
end program pointers