1

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?

4
  • It could be a compiler bug if it works with 11.1.072 but not with an earlier build of 11.1. Commented Jul 10, 2012 at 8:52
  • It also worked with 11.0. But usually I have more trust in guys from compiler business than in my own abilities :). Commented Jul 10, 2012 at 9:13
  • 1
    I always report suspected compiler problems to Intel tech support (well, always when it's an Intel compiler that is). I find them even better than SO at identifying bugs in their products. Of course in this case they may well respond 'yes, that was a bug in that version of the compiler, now fixed'. Commented Jul 10, 2012 at 10:03
  • Always use tag fortran and only add the version when necessary to distinguish that your question is specific. For example that you cannot use Fortran 2008 but only Fortran 90. Commented Dec 17, 2015 at 11:48

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I was already thinking about it. I should definitely give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.