|
1 | 1 | program auto_allocate |
2 | 2 | !! demonstrate Fortran 2003 automatic allocation of arrays |
3 | 3 | !! |
4 | | -!! NOTE: if an array is manually allocated, Fortran 2003 allows auto-reallocation bigger or smaller |
| 4 | +!! NOTE: even if an array allocate(), Fortran 2003 still allows auto-reallocation bigger or smaller |
| 5 | +!! The A(:) syntax preserves previously allocated LHS shape, truncating RHS |
5 | 6 |
|
| 7 | +use, intrinsic :: iso_fortran_env, only : stderr=>error_unit |
6 | 8 | implicit none (type, external) |
7 | 9 |
|
8 | 10 | real, allocatable, dimension(:) :: A, B, C, D, E |
@@ -36,8 +38,21 @@ program auto_allocate |
36 | 38 | D = [1,2] |
37 | 39 | E = [3,4,5,7] |
38 | 40 |
|
39 | | -if (size(D) /= 2) error stop 'auto-allocate smaller' |
40 | | -if (size(E) /= 4) error stop 'auto-allocate bigger' |
| 41 | +if (size(D) /= 2) error stop 'allocate() auto-allocate small' |
| 42 | +if (size(E) /= 4) error stop 'allocate() auto-allocate big' |
| 43 | + |
| 44 | +!> (:) syntax truncates, does not change shape, whether or not allocate() used first |
| 45 | +A(:) = [9,8,7] |
| 46 | +if (size(A) /= 2) error stop '(:) syntax smaller' |
| 47 | +if (any(A /= [9,8])) error stop '(:) assign small' |
| 48 | + |
| 49 | +E(:) = [1,2,3] |
| 50 | +if (size(E) /= 4) error stop 'allocate() (:) syntax small' |
| 51 | +if (any(E /= [1,2,3,7])) error stop 'allocate() (:) assign small' |
| 52 | + |
| 53 | +E(:) = [5,4,3,2,1] |
| 54 | +if (size(E) /= 4) error stop 'allocate() (:) syntax: big' |
| 55 | +if (any(E /= [5,4,3,2])) error stop 'allocate() (:) assign: big' |
41 | 56 |
|
42 | 57 | print *, 'OK: auto-allocate array' |
43 | 58 |
|
|
0 commit comments