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

ALLOCATE(CHARACTER(N) :: cStr); LEN(cStr) returns wrong value after assignment

joerg_kuthe
Novice
923 Views

Hello!

I run into the following problem with LEN() function returning a wrong value for an allocated CHARACTER string after an assignment. Short Example:

CHARACTER(:), ALLOCATABLE :: cStr
INTEGER :: N = 32, iLen
ALLOCATE(CHARACTER(N) :: cStr)
iLen = LEN(cStr) ! = 32 (OK)
! 123456789x123456
cStr = '13 characters'
iLen = LEN(cStr) ! = 13 (NOT OK)

I have tested this under Win7 IFORT 15.0.4.221 and Win10 with IFORT 19.0.8.324, on both platforms IA-32 and x64 .

To me, this appears to be a bug. Am I wrong?

Joerg Kuthe
(working for Intel Elite Resellers qtsoftware.de and polyhedron.com)

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
901 Views

That option does not apply to allocatable deferred-length character, which was new in F2008. The standard-conforming behavior is the only one available.

You can avoid the reallocation with:

cStr(:) = '13 characters' 

 

View solution in original post

5 Replies
andrew_4619
Honored Contributor II
909 Views

allocate LHS (left hand side) came in at Fortran 2003(?) so your assignment reallocates the string to 13 chars.  If you want the old standard behaviour there is a compiler option for that.

0 Kudos
Steve_Lionel
Honored Contributor III
902 Views

That option does not apply to allocatable deferred-length character, which was new in F2008. The standard-conforming behavior is the only one available.

You can avoid the reallocation with:

cStr(:) = '13 characters' 

 

joerg_kuthe
Novice
882 Views

That's it! Thanks a lot Steve.

Jörg

0 Kudos
FortranFan
Honored Contributor II
845 Views

@Steve_Lionel ,

Re: your comment upthread, "That option does not apply to allocatable deferred-length character, which was new in F2008" - I do not think that is accurate.

Fortran 2003 introduced support for both deferred length objects (including scalar) of CHARACTER intrinsic type and the intrinsic assignment semantics of allocation (or reallocation) of variable to conform to 'expr' including that for deferred length scalar objects of CHARACTER intrinsic type.

Readers can look up Note 7.35 in section 7.4.1.3 Interpretation of intrinsic assignments in the 2003 standard document.

0 Kudos
Steve_Lionel
Honored Contributor III
841 Views

@FortranFan , yes, you are correct. This is indeed a F2003 feature. Nonetheless, I am also correct that the ifort option /assume:realloc_lhs does not apply to deferred-length character. It did in an early beta at one time, but I successfully argued at the time that this was a new feature and didn't warrant a "do it the old way" compatibility option.

0 Kudos
Reply