Reputation: 11
I have one question and one problem (I think they are related).
Question: Can I use in Fortran 90 array of pointer arrays defined as pointer?
type string
character, pointer :: str(:)
end type
type(string), pointer :: arr(:)
Problem: I have code (see below) for which ifort 11.1 gives segfault in the last write. Other compilers, such as gfortran 4.6.3, ifort 11.1.072, 12.0.1, ..., worked without a problem
You can think about the code as simulation of Python append function.
program test
implicit none
type string
integer, pointer :: key
character, pointer :: str(:)
end type
type(string), pointer :: arr(:), tmp(:)
allocate(arr(1))
allocate(arr(1)%str(1))
arr(1)%str(1) = 'A'
write(6,*) arr(1)%str(1)
! --------------------
tmp => arr
write(6,*) tmp(1)%str(1)
! --------------------
nullify(arr)
allocate(arr(2))
arr(1)%str => tmp(1)%str
write(6,*) arr(1)%str(1)
end program
Expected result is:
A
A
A
ifort 11.1 gives segmentation fault on the last line. Now comes the weird thing. If you comment out declaration of integer in type string you will get the expected result also with ifort 11.1.
Is this compiler problem or am I using non-standard Fortran 90 structures?
Upvotes: 1
Views: 561
Reputation: 78316
Your code compiles and executes without a problem using Intel Fortran 13.0.something. Together with your experience with other compilers and compiler versions this does suggest a compiler bug, albeit one that is now corrected.
I'm not entirely sure what the Python append function does, but if you are looking for a Fortran way to make strings whose contents (and lengths) can change dynamically you should seriously consider using the Fortran 2003 automatic allocation features. For example, you could define a string like this:
character(len=:), allocatable :: string1
give it a value like this:
string1 = 'this is string 1'
and give it another value like this:
string1 = 'this is'
or like this
string1 = string1//' more text'
Observe the complete lack of statements allocating the size of string1
or reallocating it.
Upvotes: 2