012. Fortran has allocation-on-assignment#

topic: Allocation

Fortran has allocation on assignment, but you cannot use the allocate statement for an array that is already allocated.

alloc-on-assign.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
integer, allocatable :: v(:)

v = [4, 9]

deallocate(v)  ! necessary for line below to work
allocate(v, source=[4, 9])  ! same result as above

end
Output1#



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags