On 6/19/11 3:49 AM, David Vowles wrote:
> The following program, which utilizes deferred length character
> declaration, compiles and runs successfully with the compiler I am
> using. However, I am suspicious that the code is invalid because S1, S2
> & S3 have not been explicitly allocated. I would be most grateful for
> explanations as to the circumstances of if / when deferred length
> character variables should be explicitly allocated. I would also be
> interested in understanding why - if the following code is valid -
> explicit allocation is not required when explicit allocation of arrays
> is required.
Explicit allocation of an allocatable array before it appears as the
variable in an intrinsic assignment is not required. From 7.2.1.3
Interpretation of intrinsic assignments, for a statement of the form
variable = expr
"If the variable is or becomes an unallocated allocatable variable, it
is then allocated with
• if the variable is polymorphic, the same dynamic type as expr,
• each deferred type parameter equal to the corresponding type parameter
of expr,
• if the variable is an array and expr is scalar, the same bounds as
before, and
• if expr is an array, the shape of expr with each lower bound equal to
the corresponding element of LBOUND (expr)."
The second bullet covers this case. This is part of the
"auto-reallocation" feature in Fortran for assignment to allocatable
objects. In most cases the compiler has to generate additional code to
check the allocation status and other characteristics of the variable
before such assignments, resulting in a performance hit compared to the
legacy Fortran rules. Because of this, some vendors require that a
compile option be used to "enable" this feature.
I would note that, because if the capability illustrated by your test
code, the varying length string facility described in Part 2 of the
standard is essentially obsolete.
Cheers,
Bill
>
> Many thanks,
> David.
>
> PROGRAM TESTDCL
>
> CHARACTER (LEN=:), ALLOCATABLE :: S1,S2,S3
>
> ! Explicit allocation appears to be unnecessary
> ! Is that really the case???
> ! ALLOCATE(CHARACTER(3) :: S1, S2)
> ! ALLOCATE(CHARACTER(6) :: S3)
>
> S1 = 'ABC'
> S2 = 'EFG'
> S3 = S1//S2
>
> WRITE(*,*) S3
>
> END PROGRAM TESTDCL
--
Bill Long [log in to unmask]
Fortran Technical Support & voice: 651-605-9024
Bioinformatics Software Development fax: 651-605-9142
Cray Inc./Cray Plaza, Suite 210/380 Jackson St./St. Paul, MN 55101
|