NULL()
.
ALLOCATE
statement, if SOURCE=
or MOLD=
is present and its expression is an array, the array can take
its shape directly from the expression.
This is a lot more concise than using SIZE
or UBOUND
, especially
for a multi-dimensional array.
For example,
SUBROUTINE s(x,mask) REAL x(:,:,:) LOGICAL mask(:,:,:) REAL,ALLOCATABLE :: y(:,:,:) ALLOCATE(y,MOLD=x) WHERE (mask) y = 1/x ELSEWHERE y = HUGE(x) END WHERE ! ... END SUBROUTINE
ALLOCATE
statement with the SOURCE=
clause is permitted to
have more than one allocation.
The source-expr is assigned to every variable allocated in the statement.
For example,
PROGRAM multi_alloc INTEGER,ALLOCATABLE :: x(:),y(:,:) ALLOCATE(x(3),y(2,4),SOURCE=42) PRINT *,x,y END PROGRAMwill print the value “42” eleven times (the three elements of
x
and the eight elements of y
).
If the source-expr is an array, every allocation needs to have the
same shape.
COMPLEX
object can be accessed using
the complex part designators ‘%RE
’ and ‘%IM
’.
For example, given
COMPLEX,PARAMETER :: c = (1,2), ca(2) = [ (3,4),(5,6) ]the designators
c%re
and c%im
have the values 1 and 2
respectively, and ca%re
and ca%im
are arrays with the values
[ 3,5 ]
and [ 4,6 ]
respectively.
In the case of variables, for example
COMPLEX :: v, va(10)the real and imaginary parts can also be assigned to directly; the statement
va%im = 0will set the imaginary part of each element of
va
to zero without
affecting the real part.
ALLOCATE
statement for one or more variables, the MOLD=
clause can be used to give the variable(s) the dynamic type and type parameters
(and optionally shape) of an expression.
The expression in MOLD=
must be type-compatible with each
allocate-object, and if the expression is a variable (e.g. MOLD=X
), the
variable need not be defined.
Note that the MOLD=
clause may appear even if the type, type parameters
and shape of the variable(s) being allocated are not mutable.
For example,
CLASS(*),POINTER :: a,b,c ALLOCATE(a,b,c,MOLD=125)will allocate the unlimited polymorphic pointers
A
, B
and
C
to be of type Integer (with default kind); unlike SOURCE=
, the
values of A
, B
and C
will be undefined.
ALLOCATE(
variable,SOURCE=
expr)
works.
For example, given
CLASS(*),ALLOCATABLE :: xexecution of the assignment statement
x = 43will result in
X
having dynamic type Integer (with default kind) and
value 43, regardless of whether X
was previously unallocated or
allocated with any other type (or kind).
REAL,TARGET :: x(100,100) REAL,POINTER :: x1(:) x1(1:Size(x)) => xestablishes
X1
as a single-dimensional alias for the whole of X
.