Community
cancel
Showing results for 
Search instead for 
Did you mean: 
joerg_kuthe
Novice
349 Views

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

Jump to solution

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
Black Belt Retired Employee
327 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 I
335 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.

Steve_Lionel
Black Belt Retired Employee
328 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

joerg_kuthe
Novice
308 Views

That's it! Thanks a lot Steve.

Jörg

FortranFan
Honored Contributor II
271 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.

Steve_Lionel
Black Belt Retired Employee
267 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.

Reply