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

va_list Fortran implementation: Compiles fine but gives runtime errors

Anar_Yusifov
Beginner
764 Views

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.

0 Kudos
4 Replies
FortranFan
Honored Contributor III
764 Views

With line 34 in your code in mind, try this:

program p

   implicit none

   class(*), allocatable :: val
   real(kind=8),dimension(:),allocatable :: c

   allocate (val, source=c)

   stop

end program p

Anar Yusifov wrote:

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?

Take a look at this link which refers to the book example from Metcalf et al.'s Modern Fortran Explained for a derived type to hold lists of any scalar value.  If this is what you are looking for, then you can try to create another any type to hold vectors of multiple types:

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/518263

This is basically an effort to implement 'void' type and arbitrary number of arguments in any given function.

Not sure how the derived type you have in mind helps with the arbitrary number of arguments case.

0 Kudos
Anar_Yusifov
Beginner
764 Views

I should of mention that sourcing would not work due to mismatch in rank.

Here is the example code:

program p

   implicit none

   class(*), allocatable :: val
   real(kind=8),dimension(:),allocatable :: c

   allocate (c(10))
   allocate (val, source=c)

   select type (v=>val)
   type is (real(kind=8))
	print *, "Test: ",v
   class default
	print *, "Unknown"
   end select
   stop

end program p

Just try to compile it.

Thanks.

0 Kudos
FortranFan
Honored Contributor III
764 Views

Anar Yusifov wrote:

I should of mention that sourcing would not work due to mismatch in rank. ..

That's what I meant.  It's a compiler bug that it fails to catch the mismatch in rank in the ALLOCATE statement on line 34.

0 Kudos
Anar_Yusifov
Beginner
764 Views

Regarding to variable arguments list: it's basically limited to few things.

  • Any argument is scalar or multidimensional array (up to 9 dimensions) of few kinds (i1,i2,i4,i8,f4,f8) of type integer and real (complex is not considered right now)
  • In addition to the value I store in the argument information about the type, shape, rank and size of the argument

Right now I know how to store the scalars of any type. And looking for a way to store multidimensional arrays of any type.

Thanks,

Anar.

0 Kudos
Reply