[J3] Concerned out paper 20-122r1: Edits for US14 (auto-allocate characters)
Bill Long
longb at cray.com
Tue Mar 10 20:58:24 UTC 2020
> On Mar 7, 2020, at 7:13 PM, Vipul Parekh via J3 <j3 at mailman.j3-fortran.org> wrote:
>
> Dan, Steve:
>
> As noted by Peter Klausler at the GitHub site for Fortran issues (https://github.com/j3-fortran/fortran_proposals/issues/160#issuecomment-593650274), paper 20-122r1 titled "Edits for US14 (auto-allocate characters)" which has a Status of "Passed" and which is dated Friday 28th Feb 2020 in the meeting documents for 221 is of concern from 2 aspects pertaining to an already allocated scalar deferred length character variable in a WRITE statement to an internal file.
>
> Consider the following code for which I expect a conforming processor to produce an output of 'len(s) = 20':
> character(len=:), allocatable :: s
> allocate( character(len=20) :: s )
> write (s, fmt="(g0)") "Hello World!"
> print *, "len(s) = ", len(s)
> end
>
> Note 19-252r2 gave the edit for 12.4 Internal Files [216:12-14]p2 as
> "A record of the internal file becomes defined by writing the record.
> When the record is an allocatable character scalar declared
> with deferred length and is allocated sufficiently long, or is not
> an allocatable character scalar declared with deferred length,
> if the number of characters written in a record
> is less than the length of the record, the remaining portion
> of the record is filled with blanks. The number of characters
> to be written shall not exceed the length of the record.
> Otherwise, the character variable is allocated with the
> length needed to contain the record."
>
> and based on this, I expect no change to the output mentioned above.
>
> However 20-122r1 lists the Edit for [216:12-14]p2 as
> "A record of the internal file becomes defined by writing the record.
> - If the internal file is an allocatable, deferred-length character
> scalar variable, it becomes defined as if by intrinsic assignment of
> the record.
By this requirement (as if by intrinsic assignment), the character variable gets reallocated if the length is different, even if it is already allocated. For example:
character(len=:),allocatable :: m
allocate (character(len=20) :: m)
print*, "length before assignment is ", len(m)
m = "smaller"
print*, "length after assignment is ", len(m)
end program
> ./a.out
length before assignment is 20
length after assignment is 7
I think the intention was allocation to the “correct” length. Independent of the current allocation status. This is a rule that the programmer can depend on without having to trace through the code to determine if the variable was allocated or not at the point of the assignment.
Cheers,
Bill
> - Otherwise, if the number of characters written in a record is less
> than the length of the record, the remaining portion of the record is
> filled with blanks; the number of characters to be written shall not
> exceed the length of the record."
>
> which would change the output of the program shown above to 'len(s) = 12'.
>
> So what got passed at 20-122r1 is of concern in terms of:
>
> a) Consistency with paper 19-252r2 which was passed at 220 and to which 20-122r1 refers, and
>
> b) Backward compatibility with the current standard.
>
> On the other hand, should the Edit in question in 20-122r1 be modified by introducing one more word 'unallocated' it would immediately alleviate both the concerns:
> "A record of the internal file becomes defined by writing the record.
> - If the internal file is an unallocated allocatable, deferred-length character scalar variable, it becomes defined as if by intrinsic assignment of the record.
> - Otherwise, if the number of characters written in a record is less than the length of the record, the remaining portion of the record is filled with blanks; the number of characters to be written shall not exceed the length of the record."
>
> Please note the above concerns actually are present with all the situations involving an allocated scalar deferred length character variable that are being addressed in US14. With the following example code,
>
> character(len=:), allocatable :: msg
> integer :: lun, istat
> allocate( character(len=256) :: msg )
> open( newunit=lun, file="https://j3-fortran.org/", iostat=istat, iomsg=msg )
> print *, "len(msg) = ", len(msg)
> end
>
> I expect a conforming processor to output 'len(s) = 256' whereas with 20-122r1, the output is indeterminate.
>
> Can you please look into this and follow up with J3?
>
> Thank you,
> Vipul Parekh
Bill Long longb at cray.com
Principal Engineer, Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9143
Cray, a Hewlett Packard Enterprise company/ 2131 Lindau Lane/ Suite 1000/ Bloomington, MN 55425
More information about the J3
mailing list