In the following code, I am trying to allocate an empty array of size 0 and add more elements afterward by using automatic reallocation:
integer, allocatable :: a(:)
allocate( a(0) ) ! Line 1
print *, size( a )
print *, "a(:) = ", a
a = [ a, 1 ]
print *, "a(:) = ", a
a = [ a, 2 ]
print *, "a(:) = ", a
!! Error
! a = []
! a = [ integer :: ]
This code gives the expected result (e.g., with gfortran or ifort -assume realloc_lhs)
0
a(:) =
a(:) = 1
a(:) = 1 2
Here I have three questions:
- First of all, is it OK to allocate zero-sized arrays such as
allocate( a( 0 ) )
? - If we omit such an explicit allocation, will
a(:)
be automatically initialized to a zero-sized array? (Indeed, the code seems to work even if I comment out Line 1.) - Is it no problem to include zero-sized arrays in an array constructor like
a = [a, 1]
? (I also tried using an empty array constructor likea = []
ora = [integer::]
, but they did not compile and so seem to be not allowed.)
Edit
If I uncomment a = []
in the above code, gfortran5.3 gives the error message:
Error: Empty array constructor at (1) is not allowed
but if I uncomment only the line a = [ integer :: ]
, it worked with no problem! Because I initially uncommented both lines at the same time, I misunderstood that the both ways are illegal, but actually the latter seems OK (please see @francescalus answer).