1

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 enter image description here

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

and here is the output: enter image description here

Charlie Crown
  • 1,071
  • 2
  • 11
  • 28
  • Which compiler (including version) are you using, and compiling with which options? – francescalus Sep 04 '18 at 20:12
  • I updated the question to include these just now, `gcc/5.4.0` and `gfortran -fdefault-real-8 -o H -fbacktrace -g -fcheck=all pointersToObjects.f90` but I get the same result with `gfortran -fdefault-real-8 -o H pointersToObjects.f90` – Charlie Crown Sep 04 '18 at 20:37
  • 1
    Can you try with a later version of gcc? I used 6 without problem. Also, you could perhaps try using `type(objects)` rather than `class(objects)` as it looks like you aren't needing it polymorphic. – francescalus Sep 04 '18 at 20:43
  • For simplicity I did not add any procedures, but in the real code, they are objects. Also, using compiler version gcc/6.4.0 it worked... That is annoying :( I guess I can ask my actual question now, since this wasn't it :) – Charlie Crown Sep 04 '18 at 20:45
  • 2
    @CharlieCrown, why are you using -fdefault-real-8 option? This option is meant to be used when porting code not as a shortcut to writing valid Fortran. It breaks Fortran's storage association rules, and may have some interesting issues with pointer arithmetic. – Steve Sep 05 '18 at 00:00
  • Well, I am first and foremost a scientist and engineer. My programming knowledge is limited. Even with this limitation, I still don't know enough science. It has always worked for me before, and is something recommended to me years ago. You could very well be right on this. – Charlie Crown Sep 05 '18 at 02:30
  • 1
    OK, I just want to support Steve's statement, and say that flags to change data types sizes should be avoided like the plague, in the long run you are just asking for a life of pain – Ian Bush Sep 05 '18 at 06:29

1 Answers1

2

Francescalus gave the answer in the comments. But to give closing:

Using gcc/6.4.0 rather than gcc/5.4.0 fixes the problem.

HipperThanHop
  • 172
  • 12