From: mecej4 on
Someone pointed out a puzzling problem which can be filtered down to the
following:

program tst
character(len=:), allocatable :: S
S='abcdef'
S=S(3:)
write(*,'("|",A,"|")')S
end program tst

Please consider line-4. The expression to the right of the '=' is a
character expression of length 4, which is not equal to the current
allocated length of S -- 6. Therefore, S has to be deallocated and
reallocated with the new length of 4, and then the expression is to be
copied into S. However, if the compiler has not arranged for a temporary
copy of the expression to the right of the '=', part of the memory
currently occupied by the substring has become deallocated _before_ the
copying is done and strange things can happen.

Intel Fortran 11.1 gives the result as ' ' whereas NAG 5.2 gives
' ef'. If the compiler had saved a copy of the substring before doing
the reallocation, as the user intended, the result would have been 'cdef'.

Is the code legal and, if so, what is the expected output? Thanks.

-- mecej4
From: Richard Maine on
mecej4 <mecej4_no_spam(a)operamail.com> wrote:

> program tst
> character(len=:), allocatable :: S
> S='abcdef'
> S=S(3:)
> write(*,'("|",A,"|")')S
> end program tst
....
> Is the code legal and, if so, what is the expected output? Thanks.

Yes. It should output |cdef|. The compiler is supposed to handle the
temporaries as necessary. If it doesn't that would be a bug.

My copy of NAG 5.2 on this Mac, gives ||, by the way.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain