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
UBOUND, especially for a multi-dimensional array.
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
ALLOCATEstatement 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
xand the eight elements of
y). If the source-expr is an array, every allocation needs to have the same shape.
COMPLEXobject 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%imhave the values 1 and 2 respectively, and
ca%imare 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
vato zero without affecting the real part.
ALLOCATEstatement 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
Cto be of type Integer (with default kind); unlike
SOURCE=, the values of
Cwill be undefined.
)works. For example, given
CLASS(*),ALLOCATABLE :: xexecution of the assignment statement
x = 43will result in
Xhaving dynamic type Integer (with default kind) and value 43, regardless of whether
Xwas previously unallocated or allocated with any other type (or kind).
REAL,TARGET :: x(100,100) REAL,POINTER :: x1(:) x1(1:Size(x)) => xestablishes
X1as a single-dimensional alias for the whole of