I'm trying to come up with the object to store scalars and arrays in one type. It will ultimately allow me to pass arbitrary number of some predefined objects into any function. The code is compiled fine but does seg fault during running.
program TEST IMPLICIT NONE type field class(*), allocatable :: val end type field type List type(field),dimension(:),allocatable :: fields end type List type(List) :: l type(field) :: f1 type(field) :: f2 real :: a,b integer(kind=2) :: n,i real(kind=8),dimension(:),allocatable :: c a=2.5 n=37 b=3.14 allocate(l%fields(10)) allocate(f1%val,source=a) allocate(f2%val,source=n) allocate (l%fields(3)%val,source=b) l%fields(1)=f1 l%fields(2)=f2 allocate(c(10)) !this is what causing to segfault allocate(l%fields(4)%val,mold=c) print *, size(l%fields) do i=1,size(l%fields) print *, "index=",i select type (val => l%fields(i)%val) type is (integer) print '("Value is an integer: ", I0)', val type is (integer(kind=2)) print '("Value is an integer(kind=2): ", I0)', val type is (real) print *, "Value is a real: ", val class default print '("Unknown type for index:", I0 )', i end select end do stop end program TEST
I'm sure that my way of using 'mold' is wrong in this context. But Intel compiler can do better job to catch it during compile time I guess...
Another question is if anyone can suggest any idea how to store multiple types of scalars and arrays in one derived type so that I could pass list of them to the function and be able to extract data out of there?
This is basically an effort to implement 'void' type and arbitrary number of arguments in any given function.
Thanks,
Anar.